C ++:获取注册表值仅给出第一个字符 [英] C++: Getting registry value only gives first character

查看:52
本文介绍了C ++:获取注册表值仅给出第一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从注册表中获取一个字符串值,但是我只得到了第一个字母.

  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错误= RegQueryValueEx(hKey,L"GamePath",NULL,& type,(LPBYTE)& gamePath,& size);如果(错误!= ERROR_SUCCESS)cout<<无法读取注册表值:"<<错误<<恩德尔RegCloseKey(hKey);}否则cout<<无法读取注册表项."<<恩德尔cout<<gamePath<<恩德尔//输出:C 

我做错了什么?谢谢.

解决方案

您有一个 char 数组,但是 RegQueryValueEx 需要一个 wchar_t 具有宽字符集.今天的大多数程序将是宽字符应用程序,但是如果您使用的是Microsoft定义的常量 TCHAR 而不是 char wchar_t ,则可以使用不确定.

之所以只得到一个字符,是因为第一个字符的值在1到255之间.这样的16位字符代码的高字节为零,因此在内存中看起来像是字符串终止符.

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.

解决方案

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.

这篇关于C ++:获取注册表值仅给出第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆