简体   繁体   English

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

[英]How to easy convert LPBYTE to char[256] (used to read text from windows registry)

How I can convert LPBYTE to char [256]? 如何将LPBYTE转换为char [256]?

When I read from Windows registry value: 当我从Windows注册表值读取时:

blah REG_SZ "blah some text" 等等REG_SZ“等等”

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;

after cout this it shows (screenshot): 提示后显示(截图):

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

(normal text + some signs) (普通文字+一些标志)

I must compare value from registry: 我必须比较注册表中的值:

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

How I can convert this LPBYTE value to char[256] ? 如何将LPBYTE值转换为char [256]?

From MSDN : 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. 如果数据具有REG_SZ,REG_MULTI_SZ或REG_EXPAND_SZ类型,则该字符串可能没有以正确的终止空字符存储。 Therefore, even if the function returns ERROR_SUCCESS, the application should ensure that the string is properly terminated before using it; 因此,即使函数返回ERROR_SUCCESS,应用程序也应确保在使用字符串之前已正确终止该字符串。 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. (请注意,REG_MULTI_SZ字符串应具有两个终止的空字符。)应用程序可以确保正确终止字符串的一种方法是使用RegGetValue,如果需要,该方法将添加终止的空字符。

After querying the value, you should set value[dwCount-1] to '\\0' to ensure it is null terminated. 查询该值后,应将value [dwCount-1]设置为'\\ 0'以确保它以null终止。

Or just use RegGetValue which removes a lot of the oddness in the registry API - guarantees null terminated strings, allows you to specify the expected data type and fail if it is otherwise, automatically expands REG_EXPAND_SZ's, etc. 或仅使用RegGetValue即可消除注册表API中的许多怪异之处-保证以null结尾的字符串,允许您指定期望的数据类型,否则将失败,自动扩展REG_EXPAND_SZ等。

After the RegQueryValueEx call, you need to set the NUL -byte at the end of your string, using the value written by the function in dwCount : RegQueryValueEx调用之后,您需要使用dwCount函数写入的值在字符串末尾设置NUL字节:

value[dwCount] = 0;

Note that you can't compare two strings using == as they are pointers, use the strcmp function to compare them : 请注意,您不能使用==比较两个字符串,因为它们是指针,请使用strcmp函数进行比较:

if (strcmp(value, "blah") == 0)
    puts("Strings are equal");

( strcmp can be found in the string.h header) strcmp可以在string.h标头中找到)

I tried to implement RegGetVAlue function from Michaels answer, but I got Advapi32.dll error after compiling. 我试图从Michaels的答案中实现RegGetVAlue函数,但是编译后出现Advapi32.dll错误。 When I set value[dwCount] and compare string with code from delroth answer, everything works fine :) Thank you Michael, delroth :) 当我设置value [dwCount]并将字符串与delroth答案中的代码进行比较时,一切正常:)谢谢Michael,delroth :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM