Reputation:
How I can convert LPBYTE to char [256]?
When I read from Windows registry value:
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;
after cout this it shows (screenshot):
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] ?
Upvotes: 1
Views: 6807
Reputation:
I tried to implement RegGetVAlue function from Michaels answer, but I got Advapi32.dll error after compiling. When I set value[dwCount] and compare string with code from delroth answer, everything works fine :) Thank you Michael, delroth :)
Upvotes: 0
Reputation: 10880
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
:
value[dwCount] = 0;
Note that you can't compare two strings using ==
as they are pointers, use the strcmp
function to compare them :
if (strcmp(value, "blah") == 0)
puts("Strings are equal");
(strcmp
can be found in the string.h
header)
Upvotes: 2
Reputation: 55425
From 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.
After querying the value, you should set value[dwCount-1] to '\0' to ensure it is null terminated.
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.
Upvotes: 3