Click to See Complete Forum and Search --> : Reading Registry (REG_BINARY)


XTJoeyTX
January 23rd, 2008, 06:44 AM
I've been trying to figure out how to read registry with types 'reg_binary' (since reg_sz seems to work fine.)

Only problem is, it doesn't seem to do much. So anyway, I was trying to read the registry key and grab the hex-data of the binary.


if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
BYTE buffer[1024];
DWORD dwType = REG_BINARY, dwSize = sizeof(buffer);
RegQueryValueExA(hKey, "DigitalProductId", NULL, &dwType, buffer, &dwSize);
printf("T: %li -> %X -> %i\n", dwSize, buffer, sizeof(buffer));
RegCloseKey(hKey);
}

0xC0000005
January 23rd, 2008, 07:24 AM
What are you expecting to see with your printf() statement? Parameters 2 and 3 (address and size of buffer) seem to be relatively useless.

XTJoeyTX
January 23rd, 2008, 07:45 AM
What are you expecting to see with your printf() statement? Parameters 2 and 3 (address and size of buffer) seem to be relatively useless.

I was debugging, I'm just trying to print the entire buffer in hex.

0xC0000005
January 23rd, 2008, 07:52 AM
for( int i=0; i<dwSize; ++i )
printf("%02X%c",buffer[i],(i%16==15)?'\n':' '); // 16 BYTES per line

printf("\n");

XTJoeyTX
January 23rd, 2008, 08:08 AM
for( int i=0; i<dwSize; ++i )
printf("%02X%c",buffer[i],(i%16==15)?'\n':' '); // 16 BYTES per line

printf("\n");


Thanks!

Only problem now is that some of the hex data's don't match whats shown in the registry.

For example,:

In Registry: A4 00 00 00 03 00 00 00 37 36 35 38 38 2D 33 30
In Program: A4 00 00 00 08 00 00 00 75 55 32 31 34 2D 38 34

hm? :o

kirants
January 23rd, 2008, 08:17 AM
That is very strange. Are you sure you are looking at the right key ?
Also, does the dwSize return you the right size ?

0xC0000005
January 23rd, 2008, 08:22 AM
I don't know what to tell you. I just tried the code and the hex values in the program agree exactly with those in the registry.

XTJoeyTX
January 23rd, 2008, 05:41 PM
My Exact Code:


HKEY hKey;

if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
BYTE buffer[1024];
DWORD dwType = REG_BINARY, dwSize = sizeof(buffer);
RegQueryValueExA(hKey, "DigitalProductId", NULL, &dwType, buffer, &dwSize);
RegCloseKey(hKey);

for (int i = 0; i < (int)dwSize; i++)
printf("%02X%c", buffer[i], (i%16==15)?'\n':' ');
printf("\n");
}

srelu
January 23rd, 2008, 11:45 PM
I don't know how you check what you have in the registry.
If you copied the content of the registry key somewhere and compare it against the results you get with your software, the data will not be the same if you moved to work at another computer or if you started another operating system on a multiboot computer.

If you just copied the data, open the registry editor again to check if your copy is still valid.

If you have the registry editor already opened, press F5 to refresh the displayed data (you never know, it may be something, even your app, that modifies it.)

Check the return value of RegQueryValueEx.
Do you have full rights to access the registry ?

XTJoeyTX
January 24th, 2008, 12:55 AM
Actually it seems i'm totally stupid :(.

I used a program earlier to alter my DigitalProductId. The Reg-Edit seems to not reflect it (and it does restore to the correct value on restart of computer).

Anyway, I used the program again to change it and I seen it changed to the correct value that the program changed it to as well as what my program says.

Thanks for the help guys, i feel embarrassed :(.

kirants
January 24th, 2008, 01:00 AM
Thanks for the help guys, i feel embarrassed :(.
Mistakes do happen, so take it easy :wave: After all, all's well that ends well ;)

XTJoeyTX
January 26th, 2008, 05:49 AM
Sorry to 'bump' the topic, but does anyone know a way to combine two characters into a hex-integer.

I've been trying by messing with sprintf, but with no luck :(

And thanks so far for everybody who helped me :)

kirants
January 26th, 2008, 10:45 AM
example please. Data sample and what your expected result is.

XTJoeyTX
January 26th, 2008, 02:13 PM
data would be every two-characters -> 1 character.

68656c6c6f -> "hello"

0xC0000005
January 26th, 2008, 02:44 PM
Your requirements are still not clear. "hello" already is a an array of hex values. It's just a matter of how you work with the data.

char str[6] = { 'h', 'e', 'l', 'l', 'o', '\0 }; // "hello"

is equivalent to:

char str[6] = { 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00 }; // "hello"

If you print each element of either array formatted as a char (%c) you will see "hello." If you print each element of either array formatted as hex (%02x) you will see 68, 65, 6c...

XTJoeyTX
January 27th, 2008, 12:28 AM
Your requirements are still not clear. "hello" already is a an array of hex values. It's just a matter of how you work with the data.

char str[6] = { 'h', 'e', 'l', 'l', 'o', '\0 }; // "hello"

is equivalent to:

char str[6] = { 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00 }; // "hello"

If you print each element of either array formatted as a char (%c) you will see "hello." If you print each element of either array formatted as hex (%02x) you will see 68, 65, 6c...

yes but the string is already in hex-form -- i'd need to get that back into text.

0xC0000005
January 28th, 2008, 01:13 PM
Is this what you are trying to do?

std::string HexStringToText(const char *pHex)
{
// Number of hex character pairs in pHex
const int length = strlen(pHex) / 2;
// Declare string and set number of characters - including one extra for '\0'
std::string text;
text.resize(length+1);
// Copy each hex pair to the text string...
for( int i=0; i<length; ++i ){
// Scan hex pair into integer
int c;
sscanf(pHex+2*i,"%02x",&c);
// Copy to string
text[i] = c;
}
// Add final '\0'
text[length] = '\0';
// Return text string to caller
return text;
}

const std::string text = HexStringToText("68656c6c6f"); // text = "hello"

If not, then you still need to clarify. There are many ways of accomplishing a task like this so my solution is not the only one - and maybe not even the simplest one.