tags:

views:

1239

answers:

4

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] ?

+2  A: 

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.

Michael
When I add (LPBYTE)value it shows me text like in image: http://i33.tinypic.com/dnja4i.jpg
@Lisa ignore my initial answer - I knew it was wrong the moment I submitted it.
Michael
Ty Michael I will try that and write about it :)
A: 

It shows me same text like in image: http://i33.tinypic.com/dnja4i.jpg

+1  A: 

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)

Pierre Bourdon
A: 

I tried to implement RegGetVAlue funtion 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 :)

Please don't respond with another answer -- edit your original question instead. When you get more rep, you can then use comments as well.
Adam Rosenfield