/* store data of any type */ bool RegSaveVal (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, const RegVal *pValData) { ASSERT (pValData); HKEY hSubKey = pszSubKey ? RegCreate (hKey, pszSubKey, KEY_WRITE) : hKey; if (hSubKey) { LONG lResult; if (pValData->dwType == REG_DWORD) { lResult = RegSetValueEx (hSubKey, pszValName, 0, pValData->dwType, (LPBYTE) &pValData->dwNumber, sizeof (DWORD)); } else if (pValData->dwType == REG_SZ || pValData->dwType == REG_EXPAND_SZ || pValData->dwType == REG_LINK || pValData->dwType == REG_MULTI_SZ || pValData->dwType == REG_BINARY) { lResult = RegSetValueEx (hSubKey, pszValName, 0, pValData->dwType, pValData->pbyteData, pValData->dwSize); } else { lResult = ERROR_BAD_FORMAT; } if (lResult == ERROR_SUCCESS) { if (hSubKey != hKey) RegClose (hSubKey); return true; } if (hSubKey != hKey) RegClose (hSubKey); } return false; }
void CfgTest::SetARP(LPCWSTR wzKeyName, LPCWSTR wzDisplayName, LPCWSTR wzInstallLocation, LPCWSTR wzUninstallString) { HRESULT hr = S_OK; HKEY hkArp = NULL; HKEY hkNew = NULL; hr = RegOpen(HKEY_CURRENT_USER, ARP_REG_KEY, KEY_WOW64_32KEY | KEY_ALL_ACCESS, &hkArp); ExitOnFailure1(hr, "Failed to open fake ARP regkey: %ls", ARP_REG_KEY); hr = RegDelete(hkArp, wzKeyName, REG_KEY_32BIT, TRUE); if (E_FILENOTFOUND == hr) { hr = S_OK; } ExitOnFailure1(hr, "Failed to delete subkey: %ls", wzKeyName); if (NULL != wzDisplayName) { hr = RegCreate(hkArp, wzKeyName, KEY_WOW64_32KEY | KEY_ALL_ACCESS, &hkNew); ExitOnFailure1(hr, "Failed to create subkey: %ls", wzKeyName); hr = RegWriteString(hkNew, L"DisplayName", wzDisplayName); ExitOnFailure(hr, "Failed to write DisplayName to registry"); hr = RegWriteString(hkNew, L"UninstallString", wzUninstallString); ExitOnFailure(hr, "Failed to write UninstallString to registry"); hr = RegWriteString(hkNew, L"InstallLocation", wzInstallLocation); ExitOnFailure(hr, "Failed to write InstallLocation to registry"); } LExit: ReleaseRegKey(hkArp); ReleaseRegKey(hkNew); }
void CfgTest::TestInitialize() { HRESULT hr = S_OK; HKEY hk = NULL; hr = RegInitialize(); ExitOnFailure(hr, "Failed to initialize regutil"); // Override Arp regkey path hr = RegDelete(HKEY_CURRENT_USER, ARP_REG_KEY, REG_KEY_32BIT, TRUE); if (E_FILENOTFOUND == hr) { hr = S_OK; } ExitOnFailure1(hr, "Failed to delete fake ARP regkey: %ls", ARP_REG_KEY); hr = RegCreate(HKEY_CURRENT_USER, ARP_REG_KEY, REG_KEY_32BIT, &hk); ExitOnFailure1(hr, "Failed to create fake ARP regkey: %ls", ARP_REG_KEY); hr = TestHookOverrideArpPath(ARP_REG_KEY); ExitOnFailure(hr, "Failed to override ARP path for test"); // Override Applications regkey path hr = RegDelete(HKEY_CURRENT_USER, APPLICATIONS_REG_KEY, REG_KEY_32BIT, TRUE); if (E_FILENOTFOUND == hr) { hr = S_OK; } ExitOnFailure1(hr, "Failed to delete fake Applications regkey: %ls", APPLICATIONS_REG_KEY); hr = RegCreate(HKEY_CURRENT_USER, APPLICATIONS_REG_KEY, REG_KEY_32BIT, &hk); ExitOnFailure1(hr, "Failed to create fake Applications regkey: %ls", APPLICATIONS_REG_KEY); hr = TestHookOverrideApplicationsPath(APPLICATIONS_REG_KEY); ExitOnFailure(hr, "Failed to override Applications path for test"); RedirectDatabases(); LExit: ReleaseRegKey(hk); }
/* store a string */ bool RegSaveString (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, LPCTSTR pszString) { HKEY hSubKey = pszSubKey ? RegCreate (hKey, pszSubKey, KEY_WRITE) : hKey; if (hSubKey) { if (RegSetValueEx (hSubKey, pszValName, 0, REG_SZ, (LPBYTE) pszString, (DWORD)_tcslen (pszString) + 1) == ERROR_SUCCESS) { if (hSubKey != hKey) RegClose (hSubKey); return true; } if (hSubKey != hKey) RegClose (hSubKey); } return false; }
/* store binary data */ bool RegSaveBinary (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, const LPBYTE pbyteData, DWORD dwSize) { HKEY hSubKey = pszSubKey ? RegCreate (hKey, pszSubKey, KEY_WRITE) : hKey; if (hSubKey) { if (RegSetValueEx (hSubKey, pszValName, 0, REG_BINARY, pbyteData, dwSize) == ERROR_SUCCESS) { if (hSubKey != hKey) RegClose (hSubKey); return true; } if (hSubKey != hKey) RegClose (hSubKey); } return false; }
/* store a number */ bool RegSaveNumber (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, DWORD dwNumber) { HKEY hSubKey = pszSubKey ? RegCreate (hKey, pszSubKey, KEY_WRITE) : hKey; if (hSubKey) { if (RegSetValueEx (hSubKey, pszValName, 0, REG_DWORD, (LPBYTE) &dwNumber, sizeof (DWORD)) == ERROR_SUCCESS) { if (hSubKey != hKey) RegClose (hSubKey); return true; } if (hSubKey != hKey) RegClose (hSubKey); } return false; }
void CfgTest::SetApplication(LPCWSTR wzFileName, LPCWSTR wzFilePath) { HRESULT hr = S_OK; HKEY hk = NULL; LPWSTR sczFullPath = NULL; LPWSTR sczQuotedCommand = NULL; hr = StrAllocFormatted(&sczFullPath, L"%ls\\%ls\\shell\\open\\command", APPLICATIONS_REG_KEY, wzFileName); ExitOnFailure(hr, "Failed to format string to full shell\\open\\command path"); hr = RegCreate(HKEY_CURRENT_USER, sczFullPath, KEY_WOW64_32KEY | KEY_ALL_ACCESS, &hk); ExitOnFailure1(hr, "Failed to create key: %ls", sczFullPath); hr = StrAllocFormatted(&sczQuotedCommand, L"\"%ls\" \"%%1\"", wzFilePath); ExitOnFailure(hr, "Failed to format quoted command string"); hr = RegWriteString(hk, NULL, sczQuotedCommand); ExitOnFailure(hr, "Failed to write quoted command to registry"); LExit: ReleaseRegKey(hk); ReleaseStr(sczFullPath); ReleaseStr(sczQuotedCommand); }
/* create computer registry */ HKEY CReg::Create (HKEY hNewKey, LPCTSTR pszSubKey, DWORD dwRights) { return hKey = RegCreate (hNewKey, pszSubKey, dwRights); }
extern "C" HRESULT RegDefaultWriteValue( __in LEGACY_PRODUCT *pProduct, __in_z LPCWSTR wzName, __in const CONFIG_VALUE *pcvValue, __out BOOL *pfHandled ) { HRESULT hr = S_OK; LPWSTR sczValue = NULL; LPWSTR sczRegKey = NULL; LPWSTR sczRegValueName = NULL; BYTE *pbBuffer = NULL; SIZE_T cbBuffer = 0; BOOL fReleaseBuffer = FALSE; DWORD dwRoot = DWORD_MAX; HKEY hk = NULL; hr = MapCfgNameToRegValue(pProduct, wzName, &dwRoot, &sczRegKey, &sczRegValueName); if (E_INVALIDARG == hr) { *pfHandled = FALSE; // Not a regkey, so just ignore ExitFunction1(hr = S_OK); } ExitOnFailure(hr, "Failed to convert value name to registry information"); *pfHandled = TRUE; hr = RegOpen(ManifestConvertToRootKey(dwRoot), sczRegKey, KEY_SET_VALUE, &hk); if (E_FILENOTFOUND == hr) { hr = S_OK; // The key doesn't exist, so no need to proceed with deleting the value if (VALUE_DELETED == pcvValue->cvType) { ExitFunction1(hr = S_OK); } hr = RegCreate(ManifestConvertToRootKey(dwRoot), sczRegKey, KEY_SET_VALUE, &hk); ExitOnFailure(hr, "Failed to create regkey: %ls", sczRegKey); } ExitOnFailure(hr, "Failed to open regkey: %ls", sczRegKey); switch (pcvValue->cvType) { case VALUE_DELETED: hr = RegWriteString(hk, sczRegValueName, NULL); ExitOnFailure(hr, "Failed to delete existing value"); break; case VALUE_BLOB: switch (pcvValue->blob.cbType) { case CFG_BLOB_POINTER: pbBuffer = const_cast<BYTE *>(pcvValue->blob.pointer.pbValue); cbBuffer = pcvValue->blob.cbValue; break; case CFG_BLOB_DB_STREAM: fReleaseBuffer = TRUE; hr = StreamRead(pcvValue->blob.dbstream.pcdb, pcvValue->blob.dbstream.dwContentID, NULL, &pbBuffer, &cbBuffer); ExitOnFailure(hr, "Failed to read stream from database while writing binary to the registry"); break; default: hr = E_INVALIDARG; ExitOnFailure(hr, "Invalid blob type encountered"); break; } hr = RegWriteBinary(hk, sczRegValueName, pbBuffer, cbBuffer); ExitOnFailure(hr, "Failed to write binary value to registry"); break; case VALUE_STRING: hr = RegWriteString(hk, sczRegValueName, pcvValue->string.sczValue); ExitOnFailure(hr, "Failed to write string to registry"); break; case VALUE_DWORD: hr = RegWriteNumber(hk, sczRegValueName, pcvValue->dword.dwValue); ExitOnFailure(hr, "Failed to write dword to registry"); break; case VALUE_QWORD: hr = RegWriteQword(hk, sczRegValueName, pcvValue->qword.qwValue); ExitOnFailure(hr, "Failed to write qword to registry"); break; default: ExitFunction1(hr = E_INVALIDARG); } LExit: ReleaseRegKey(hk); ReleaseStr(sczValue); ReleaseStr(sczRegKey); ReleaseStr(sczRegValueName); if (fReleaseBuffer) { ReleaseMem(pbBuffer); } return hr; }