C ++-从注册表读取的值中获取空值 [英] C++ - getting null values in value read from registry
问题描述
我的应用程序正确读取和写入注册表.现在,我需要从以下位置读取注册表值:
My application properly reads and writes to the registry. Now, I need to read a registry value from:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\MachineGuid
这是我的代码:
bool GetWindowsID(string &winID)
{
HKEY hKey = 0, hKeyType = HKEY_LOCAL_MACHINE;
bool status = false;
DWORD dwType = 0;
DWORD dwBufSize = 256;
char value[256] = "\0";
if (RegOpenKeyEx(hKeyType, L"SOFTWARE\\Microsoft\\Cryptography", NULL, KEY_QUERY_VALUE|KEY_WOW64_64KEY, &hKey) == ERROR_SUCCESS)
{
dwType = REG_SZ;
if (RegQueryValueEx(hKey, L"MachineGuid", NULL, &dwType, (LPBYTE)value, &dwBufSize) == ERROR_SUCCESS)
status = true;
RegCloseKey(hKey);
}
winID.assign(value);
return status;
}
我得到 guid
,但是在每个字符后的 value
数组中,它们是一个"\ 0"值,因为只有数组的第一个字符才分配给字符串.这太奇怪了!
I get the guid
but in value
array after each character their is a "\0" value due to which only first character of array gets assigned to string. This is wierd!
推荐答案
您已经建立了针对Unicode的代码,因此注册表API返回了UTF-16 Unicode文本.请记住,每个 wchar_t
元素都是2个字节宽,而不是 char
.
You have built targeting Unicode and so the registry API is returning UTF-16 Unicode text. Instead of char
use wchar_t
and remember that each wchar_t
element is 2 bytes wide.
还请确保按照文档中的说明,说明返回的字符串不是以空值结尾的.您必须考虑 dwBufSize
中返回的值.
Do also make sure that you account for the returned string not being null-terminated, as described in the documentation. You must take account of the value returned in dwBufSize
.
这篇关于C ++-从注册表读取的值中获取空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!