-2

i am working on to extract registry value of type REG_SZ and use it as a character array i have tried taking input in byte* buffer but further not able to get it in char array .

BYTE* buffer = new BYTE[cbMaxValueData];
ZeroMemory(buffer, cbMaxValueData);
buffer[0] = '\0';

LONG dwRes = RegQueryValueEx(hKey, oem_name, 0, NULL, buffer, &lpData);

_tprintf(TEXT("(%d)  %s: %s\n"), i+1, oem_name, buffer);

what i want is to extract each character of buffer but i dont find any way : PS: value in my registry key is of REG_SZ type

basically if someone could help me in converting BYTE* buffer to char* var or string str then too it will solve my issue

sshikhar
  • 1
  • 2
  • 1
    Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Feb 11 '17 at 03:33
  • Show us all the details of the particular registry value you're trying to work with and the code you've tried so far to access it. Then someone may be able to give you better help on what you need to do. – TheUndeadFish Feb 11 '17 at 05:47

1 Answers1

0

The code you posted is incomplete and does not compile and you are not checking the return value from RegQueryValueEx!

The difference between BYTE* and char* is not the problem and you could just cast to char* because they are both just byte sized arrays and you don't care about the sign.

You don't usually allocate a BYTE array when reading from the registry, you allocate the type you are reading and just cast when calling the function:

TCHAR buffer[100];
DWORD size = sizeof(buffer);
LONG result = RegQueryValueEx(hKey, oem_name, 0, NULL, (BYTE*) buffer, &size);
if (ERROR_SUCCESS == result) _tprintf(....); else printf("Error %u\n", result);

but code like this with a fixed size buffer is often not enough. If you are reading unknown data then you should call RegQueryValueEx in a loop. First with a NULL buffer to get the size, then allocate and call again. You need a loop because someone else might increase the size of the data in between the two calls to RegQueryValueEx!

Even if you do all that you still have issues to deal with because the string you read might not be \0 terminated. You must manually terminate or use RegGetValue instead.

Anders
  • 97,548
  • 12
  • 110
  • 164