C++: Getting registry value only gives first character [duplicate]

谁都会走 提交于 2021-02-05 12:20:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!