int CShellCommandSetValue::Execute(CConsole &rConsole, CArgumentParser& rArguments) { LONG nError; rArguments.ResetArgumentIteration(); TCHAR *pszCommandItself = rArguments.GetNextArgument(); TCHAR *pszParameter; TCHAR *pszValueFull = NULL; TCHAR *pszValueData = NULL; BOOL blnBadParameter = FALSE; BOOL blnHelp = FALSE; DWORD dwValueSize = 0; DWORD dwType = REG_NONE; BYTE *pDataBuffer = NULL; if ((_tcsnicmp(pszCommandItself,SET_VALUE_CMD _T(".."),SET_VALUE_CMD_LENGTH+2*sizeof(TCHAR)) == 0)|| (_tcsnicmp(pszCommandItself,SET_VALUE_CMD _T("\\"),SET_VALUE_CMD_LENGTH+1*sizeof(TCHAR)) == 0)) { pszValueFull = pszCommandItself + SET_VALUE_CMD_LENGTH; } else if (_tcsnicmp(pszCommandItself,SET_VALUE_CMD _T("/"),SET_VALUE_CMD_LENGTH+1*sizeof(TCHAR)) == 0) { pszParameter = pszCommandItself + SET_VALUE_CMD_LENGTH; goto CheckValueArgument; } while((pszParameter = rArguments.GetNextArgument()) != NULL) { CheckValueArgument: blnBadParameter = FALSE; if (((*pszParameter == _T('/'))||(*pszParameter == _T('-'))) &&(*(pszParameter+1) == _T('?'))) { blnHelp = TRUE; } else if (dwType == REG_NONE) { if (_tcsicmp(pszParameter,_T("b")) == 0) { dwType = REG_BINARY; } else if (_tcsicmp(pszParameter,_T("dw")) == 0) { dwType = REG_DWORD; } else if (_tcsicmp(pszParameter,_T("dwle")) == 0) { dwType = REG_DWORD_LITTLE_ENDIAN; } else if (_tcsicmp(pszParameter,_T("dwbe")) == 0) { dwType = REG_DWORD_BIG_ENDIAN; } else if (_tcsicmp(pszParameter,_T("sz")) == 0) { dwType = REG_SZ; } else if (_tcsicmp(pszParameter,_T("esz")) == 0) { dwType = REG_EXPAND_SZ; } else { blnBadParameter = TRUE; } } else if (pszValueData == NULL) { pszValueData = pszParameter; } else if (!pszValueFull) { pszValueFull = pszParameter; } else { blnBadParameter = TRUE; } if (blnBadParameter) { rConsole.Write(_T("Bad parameter: ")); rConsole.Write(pszParameter); rConsole.Write(_T("\n")); } } if (!pszValueData) blnHelp = TRUE; CRegistryKey Key; TCHAR *pszValueName; const TCHAR *pszEmpty = _T(""); const TCHAR *pszPath; if (blnHelp) { rConsole.Write(GetHelpString()); if (pDataBuffer) delete pDataBuffer; return 0; } if (pszValueFull) { if (_tcscmp(pszValueFull,_T("\\")) == 0) goto CommandNAonRoot; TCHAR *pchSep = _tcsrchr(pszValueFull,_T('\\')); pszValueName = pchSep?(pchSep+1):(pszValueFull); pszPath = pchSep?pszValueFull:_T("."); //if (_tcsrchr(pszValueName,_T('.'))) //{ // pszValueName = _T(""); // pszPath = pszValueFull; //} //else if (pchSep) *pchSep = 0; } else { pszValueName = (TCHAR*)pszEmpty; pszPath = _T("."); } if (!m_rTree.GetKey(pszPath,KEY_SET_VALUE,Key)) { rConsole.Write(m_rTree.GetLastErrorDescription()); goto SkipCommand; } if (Key.IsRoot()) goto CommandNAonRoot; switch (dwType) { case REG_BINARY: { HANDLE hFile; DWORD dwBytesReaded; hFile = CreateFile(pszValueData,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL); if (hFile == INVALID_HANDLE_VALUE) { rConsole.Write(_T("Cannot open file ")); rConsole.Write(pszValueData); rConsole.Write(_T("\n")); goto SkipCommand; } dwValueSize = GetFileSize(hFile,NULL); if (dwValueSize == (DWORD)-1) // ok, that's right, we compare signed with unsigned here. // GetFileSize is documented and declared to return DWORD. // Error is indicated by checking if return is -1. Design->documentation bug ??? { rConsole.Write(_T("Cannot get size of file ")); rConsole.Write(pszValueData); rConsole.Write(_T("\n")); VERIFY(CloseHandle(hFile)); goto SkipCommand; } pDataBuffer = new BYTE [dwValueSize]; if (!pDataBuffer) { rConsole.Write(_T("Cannot load file into memory. Out of memory.\n")); VERIFY(CloseHandle(hFile)); goto SkipCommand; } if (!ReadFile(hFile,pDataBuffer,dwValueSize,&dwBytesReaded,NULL)) { rConsole.Write(_T("Cannot load file into memory. Error reading file.\n")); VERIFY(CloseHandle(hFile)); goto SkipCommand; } VERIFY(CloseHandle(hFile)); ASSERT(dwBytesReaded == dwValueSize); } break; case REG_DWORD_LITTLE_ENDIAN: case REG_DWORD_BIG_ENDIAN: dwValueSize = 4; pDataBuffer = (BYTE *) new BYTE [dwValueSize]; if (!StringToDWORD(*(DWORD *)pDataBuffer,pszValueData)) { rConsole.Write(_T("Cannot convert ")); rConsole.Write(pszValueData); rConsole.Write(_T(" to DWORD \n")); goto SkipCommand; } if (dwType == REG_DWORD_BIG_ENDIAN) { unsigned char nByte; nByte = *pDataBuffer; *pDataBuffer = *(pDataBuffer+3); *(pDataBuffer+3) = nByte; nByte = *(pDataBuffer+1); *(pDataBuffer+1) = *(pDataBuffer+2); *(pDataBuffer+2) = nByte; } break; case REG_SZ: case REG_EXPAND_SZ: dwValueSize = _tcslen(pszValueData)+1; if (*pszValueData == _T('\"')) { dwValueSize -= 2; *(pszValueData+dwValueSize) = 0; pszValueData++; } dwValueSize *= sizeof(TCHAR); pDataBuffer = (BYTE *) new BYTE [dwValueSize]; { const TCHAR *pchSrc = pszValueData; TCHAR *pchDest = (TCHAR *)pDataBuffer; while(*pchSrc) { if (pchSrc[0] == _T('^')) { if (pchSrc[1] == _T('a')) *pchDest = _T('\a'); else if (pchSrc[1] == _T('b')) *pchDest = _T('\b'); else if (pchSrc[1] == _T('f')) *pchDest = _T('\f'); else if (pchSrc[1] == _T('n')) *pchDest = _T('\n'); else if (pchSrc[1] == _T('r')) *pchDest = _T('\r'); else if (pchSrc[1] == _T('t')) *pchDest = _T('\t'); else *pchDest = pchSrc[1]; pchSrc +=2; pchDest++; dwValueSize--; } else { *pchDest = *pchSrc; pchSrc++; pchDest++; } } *pchDest = _T('\0'); } break; default: ASSERT(FALSE); } { size_t s = _tcslen(pszValueName); if (s && (pszValueName[0] == _T('\"'))&&(pszValueName[s-1] == _T('\"'))) { pszValueName[s-1] = 0; pszValueName++; } } nError = Key.SetValue(pszValueName,dwType,pDataBuffer,dwValueSize); if (nError != ERROR_SUCCESS) { char Buffer[254]; _stprintf(Buffer,_T("Cannot set value. Error is %u\n"),(unsigned int)nError); rConsole.Write(Buffer); } else { InvalidateCompletion(); } SkipCommand: if (pDataBuffer) delete[] pDataBuffer; return 0; CommandNAonRoot: rConsole.Write(SET_VALUE_CMD COMMAND_NA_ON_ROOT); return 0; }
BOOL TestStringToDWORD() { CString str ; DWORD dwResult = 0 ; DWORD dwValue = 0x1234567 ; str.Format(_T("%x"), dwValue) ; dwResult = StringToDWORD(str) ; if (dwValue != dwResult) { AfxMessageBox(_T("dwValue = 0x1234567 Error")) ; return FALSE ; } dwValue = 0x1 ; str.Format(_T("%x"), dwValue) ; dwResult = StringToDWORD(str) ; if (dwValue != dwResult) { AfxMessageBox(_T("dwValue = 0x1 Error")) ; return FALSE ; } dwValue = 0x2 ; str.Format(_T("%x"), dwValue) ; dwResult = StringToDWORD(str) ; if (dwValue != dwResult) { AfxMessageBox(_T("dwValue = 0x0 Error")) ; return FALSE ; } dwValue = 0xff ; str.Format(_T("%x"), dwValue) ; dwResult = StringToDWORD(str) ; if (dwValue != dwResult) { AfxMessageBox(_T("dwValue = 0xff Error")) ; return FALSE ; } dwValue = 0xfe ; str.Format(_T("%x"), dwValue) ; dwResult = StringToDWORD(str) ; if (dwValue != dwResult) { AfxMessageBox(_T("dwValue = 0xfe Error")) ; return FALSE ; } dwValue = 0xcc ; str.Format(_T("%x"), dwValue) ; dwResult = StringToDWORD(str) ; if (dwValue != dwResult) { AfxMessageBox(_T("dwValue = 0xcc Error")) ; return FALSE ; } OutputDebugString(_T("TestStringToDWORD pass !\r\n")) ; return TRUE ; }