Reputation: 1359
I am testing the following code and receiving the error: "RegOpenKeyEx failed with error 6: The handle is invalid", what am I doing wrong? I am using WinXP, MS VS 2010, compiling in Unicode.
HKEY hKey;
if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS, &hKey)) {
ErrorExit(TEXT("RegOpenKeyEx"));
}
Upvotes: 1
Views: 2483
Reputation: 941545
You have a bug in your error handling. As posted, this code can never generate a proper error message. RegOpenKeyEx() is different from the majority of winapi functions, it returns the error code directly, you do not use GetLastError().
It needs to be rewritten to something like:
HKEY hKey;
LONG err = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS, &hKey);
if (err != ERROR_SUCCESS) {
ErrorExit2(err, TEXT("RegOpenKeyEx"));
}
Upvotes: 1