我想在使用C++的Windows註冊表中創建一個新的註冊表項。這裏是我的代碼至今:使用C++創建一個新的Windows註冊表項
HKEY hKey;
LPCTSTR sk = TEXT("SOFTWARE\\OtherTestSoftware");
LONG openRes = RegCreateKeyEx(
HKEY_LOCAL_MACHINE,
sk,
0,
NULL,
REG_OPTION_BACKUP_RESTORE,
KEY_ALL_ACCESS,
NULL,
&hKey,
NULL);
if (openRes==ERROR_SUCCESS) {
printf("Success creating key.");
} else {
printf("Error creating key.");
}
LPCTSTR value = TEXT("OtherTestSoftwareKey");
LPCTSTR data = "OtherTestData\0";
LONG setRes = RegSetValueEx (hKey, value, 0, REG_SZ, (LPBYTE)data, strlen(data)+1);
if (setRes == ERROR_SUCCESS) {
printf("Success writing to Registry.");
} else {
printf("Error writing to Registry.");
}
//RegDeleteKey(hKey, sk);
LONG closeOut = RegCloseKey(hKey);
if (closeOut == ERROR_SUCCESS) {
printf("Success closing key.");
} else {
printf("Error closing key.");
}
我能夠成功地打開使用非常相似的代碼片段現有的密鑰(基本上與RegOpenKeyEx取代RegCreateKeyEx)。我會想象一下,我傳遞給RegCreateKeyEx的一個或多個參數會導致這個問題。我真的不確定哪裏的事情可能會被玷污,因爲我所陷入的所有錯誤代碼都顯示出成功。作爲參考,這裏是RegCreateKeyEx函數簽名:
/*
* LONG WINAPI RegCreateKeyEx(
__in HKEY hKey,
__in LPCTSTR lpSubKey,
__reserved DWORD Reserved,
__in_opt LPTSTR lpClass,
__in DWORD dwOptions,
__in REGSAM samDesired,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
__out PHKEY phkResult,
__out_opt LPDWORD lpdwDisposition
);
*/
任何想法將是巨大的!
感謝, 布賴恩
你的錯誤發生在哪裏(我假設在RegCreateKeyEx()調用中,但我們不應該猜測),以及GetLastError()返回的是什麼? – 2009-02-03 19:52:34
您是否打算嘗試刪除剛剛創建的密鑰? – 2009-02-03 19:54:58
而不是REG_OPTION_BACKUP_RESTORE您應該通過REG_OPTION_NON_VOLATILE(= 0)。您可以參考MSDN上的這些選項的行爲http://msdn.microsoft.com/en-us/library/ms724844(VS.85).aspx – Ismael 2009-02-03 20:01:55