gpt4 book ai didi

c++ - 如何轻松地将 LPBYTE 转换为 char[256](用于从 Windows 注册表中读取文本)

转载 作者:行者123 更新时间:2023-11-30 01:32:33 27 4
gpt4 key购买 nike

如何将 LPBYTE 转换为 char [256]?

当我从 Windows 注册表值读取时:

blah REG_SZ "blah some text"

char value[256];
DWORD keytype = REG_SZ;
DWORD dwCount = sizeof(value);
RegQueryValueEx((HKEY)key, "blah", 0, &keytype, (LPBYTE)&value, &count);
cout << "Read text from registry: " << value << endl;

cout 后显示(截图):

http://i33.tinypic.com/dnja4i.jpg

(普通文本+一些符号)

我必须比较注册表中的值:

 if("blah some text" == value)
cout << "Kk, good read from registry\n";

如何将这个 LPBYTE 值转换为 char[256]?

最佳答案

来自 MSDN :

If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, the string may not have been stored with the proper terminating null characters. Therefore, even if the function returns ERROR_SUCCESS, the application should ensure that the string is properly terminated before using it; otherwise, it may overwrite a buffer. (Note that REG_MULTI_SZ strings should have two terminating null characters.) One way an application can ensure that the string is properly terminated is to use RegGetValue, which adds terminating null characters if needed.

查询值后,您应该将值[dwCount-1]设置为'\0'以确保它是空终止的。

或者只使用 RegGetValue它消除了注册表 API 中的许多奇怪之处 - 保证以空字符结尾的字符串,允许您指定预期的数据类型,否则会失败,自动扩展 REG_EXPAND_SZ 等。

关于c++ - 如何轻松地将 LPBYTE 转换为 char[256](用于从 Windows 注册表中读取文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1499622/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com