RegQueryValueEx gets a weird value
I am trying to retrieve some values from the registry. Here is the full path: [HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes] "ThemeChangesMousePointers"=0x00000001 (1)
And here is my code:
HKEY hKey;
DWORD dwDisp = REG_DWORD;
DWORD dwType;
DWORD dwSize = sizeof(DWORD);
DWORD dwValue = 0;
DWORD dwReturn;
char buffer[255] = {0};
//char*开发者_高级运维 buffer;
//DWORD buffer = 0;
DWORD dwBufSize = sizeof(buffer);
if( RegOpenKey(HKEY_LOCAL_MACHINE,
_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes")
,&hKey) == ERROR_SUCCESS)
{
dwType = REG_DWORD;
if( RegQueryValueEx(hKey,_T("ThemeChangesMousePointers"),0, &dwType, (LPBYTE) buffer, &dwBufSize) == ERROR_SUCCESS)
{
printf("Key value is: %d \n", buffer);
printf("GetLastError reports %d \n", GetLastError());
}
else
{
printf("Cannot query for key value \n");
}
}
I have tried debugging and it seems like my buffer output variable is giving me a weird output:
Key value is: 2554292 Error is 0
Every time I run the code again, I get different values. How can I get the real value and not a different one?
EDIT: Post above has been edited to ask a different question.
Also, if this isn't enough code, I can add more.
RegQueryValueEx does not call SetLastError
, it returns it's error code directly.
Return Value
If the function succeeds, the return value is ERROR_SUCCESS.
If the function fails, the return value is a system error code.
If the lpData buffer is too small to receive the data, the function returns ERROR_MORE_DATA.
If the lpValueName registry value does not exist, the function returns ERROR_FILE_NOT_FOUND.
EDIT IN RESPPONSE TO COMMENT:
I initially answered your specific question, bit with regards to the code you posted, there are still several remaining problems:
- You're calling the deprecated method RegOpenKey rather than RegOpenKeyEx.
- You're trying to read an integer but you're reading it as a char * string. This is the most likely cause of the problems you're getting... the function's returning an integer but you're printing it as a string.
- Your IF conditional is backwards
HKEY hKey;
DWORD dwDisp = REG_DWORD;
DWORD dwSize = sizeof(DWORD);
DWORD dwValue = 0;
DWORD dwReturn;
DWORD dwBufSize = sizeof(DWORD);
if( RegOpenKeyExW(HKEY_LOCAL_MACHINE,
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes"
,0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
{
DWORD error = RegQueryValueExW(hKey,L"ThemeChangesMousePointers",0,0, reinterpret_cast<LPBYTE>(&dwReturn), &dwBufSize);
if(error == ERROR_SUCCESS)
{
printf("Key value is: %d \n", dwReturn);
}
else
{
printf("Cannot query for key value; Error is: %d\n", error);
}
}
RegCloseKey(hKey);
精彩评论