问题
I'm trying to get a string value from the registry, but I'm only getting the first letter.
HKEY hKey;
char gamePath[MAX_PATH];
if(RegOpenKeyEx(HKEY_CURRENT_USER,L"Software\\Blizzard Entertainment\\Warcraft III",0,KEY_READ,&hKey) == ERROR_SUCCESS)
{
DWORD type=REG_SZ, size=MAX_PATH;
int error = RegQueryValueEx(hKey,L"GamePath",NULL,&type,(LPBYTE)&gamePath,&size);
if(error != ERROR_SUCCESS)
cout << "Failed to read registry value: " << error << endl;
RegCloseKey(hKey);
}
else cout << "Failed to read registry key." << endl;
cout << gamePath << endl; //output: C
What am I doing wrong? Thanks.
回答1:
You have an array of char
but RegQueryValueEx
requires an array of wchar_t
in a wide-character build. Most programs today will be wide-character applications, but you can use the Microsoft-defined constant TCHAR
instead of char
or wchar_t
if you're unsure.
The reason you only get a single character is because the first character has a value between 1 and 255. The high byte of such a 16-bit character code is zero, so in memory it looks like a string terminator.
来源:https://stackoverflow.com/questions/52192146/c-getting-registry-value-only-gives-first-character