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

标签 c++ char lpbyte

如何将 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/

相关文章:

c++ - 使用定时器队列的调度器

c - 赋值从指针生成整数,无需强制转换(C 语言。)

c - 如何修复 "Debug Error!, Stack around the variable ' x' 已损坏”?

c++ - 将 LPBYTE 转换为字符串

c++ - 如何将 LPBYTE 转换为字符串

c - 努力将 LPBYTE 转换为 char* 或 C 中的类似内容(中文输出?)

c++ - QSignalMapper 和原始 Sender()

java - 构建/新关键字的更好方法

c++ - 执行策略之间的差异以及何时使用它们

c++ - C++ 中的段错误很可能在 strtok 操作中