• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1647
  • Last Modified:

RegOpenKeyEx REG_DWORD REG_SZ REG_BINARY

I know how to use RegOpenKeyEx.

The question is that all the examples I have seen seem to return data to character buffer. But if I have a key that stores REG_DWORD or REG_BINARY, why am I using a character buffer?

For instance, if I want a key called fileSize = 5000 stored as REG_DWORD, I would need to read it into a char buffer and then convert to a number, say using atoi?

So what is the point of REG_DWORD and REG_BINARY?

0
Paullkha
Asked:
Paullkha
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
to read a DWORD:

DWORD dwData = 0;
DWORD bufSize = sizeof(DWORD);
               
RegQueryValueEx(main_key, "some entry", NULL, NULL, (LPBYTE)&dwData , &bufSize);
     
0
 
Jaime OlivaresSoftware ArchitectCommented:
To read a binary is almost the same to read a string, with exception that no NULL character is added at the end of string.
0
 
jkrCommented:
>>if I want a key called fileSize = 5000 stored as REG_DWORD, I would need to read it into a char buffer and then
>>convert to a number, say using atoi?

No, you'd use

DWORD ReadDWORD() {

    DWORD dwSample = 0; // 0 is the default
    HKEY hKey = NULL;
    REGSAM sam = KEY_READ;
    char* pszKey = "Software\\Blubb";

    if ( ERROR_SUCCESS != RegOpenKeyEx ( HKEY_LOCAL_MACHINE, pszKey, 0, sam, &hKey)) {

       return dwSample; // assume "default" if key is not present or readable

    } else {

        DWORD dwType = REG_DWORD;
        DWORD dwSize = sizeof ( DWORD);

        if ( ERROR_SUCCESS != RegQueryValueEx ( hKey, "ValueNAme", NULL, &dwType, ( LPBYTE) &dwSample, &dwSize)) {

            // error
        }
    }

    RegCloseKey ( hKey);

    return dwSample;
}

The same for 'REG_BINARY'.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jkrCommented:
Here's the code for 'binary':

    BYTE abSample [256];
    HKEY hKey = NULL;
    REGSAM sam = KEY_READ;
    char* pszKey = "Software\\Blubb";

    if ( ERROR_SUCCESS != RegOpenKeyEx ( HKEY_LOCAL_MACHINE, pszKey, 0, sam, &hKey)) {

       // error

    } else {

        DWORD dwType = REG_BINARY;
        DWORD dwSize = sizeof ( abSample);

        if ( ERROR_SUCCESS != RegQueryValueEx ( hKey, "ValueNAme", NULL, &dwType, ( LPBYTE) abSample, &dwSize)) {

            // error
        }
    }

    RegCloseKey ( hKey);
0
 
itsmeandnobodyelseCommented:
FYI:

RegQueryValueEx uses an unsigned char buffer (LPBYTE) to return values that could be text or binary data. When returning a DWORD it is returned as a binary number (32 bits) and *not* as text containing a number like "123456".

jkr and jaime both are casting the address of the output variable to LPBYTE, what is the usual way. However, you also could provide a byte buffer and copy the data in a second step, thus avoiding casts.

       unsigned char buf[32] = { '\0' };
       DWORD dwTyp = REG_DWORD;
       DWORD dwVal  = 0;
       DWORD dwSiz = sizeof(buf);

        if (RegQueryValueEx ( hKey, "Name", NULL, &dwTyp, buf, &dwSiz) == ERROR_SUCCESS &&
            dwSiz == sizeof(DWORD))
        {
             memcpy(&dwVal, buf, sizeof(DWORD));
        }

Regards, Alex
0
 
PaullkhaAuthor Commented:
p.s. This is what made it all click:
DWORD dwSize = sizeof ( abSample);
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Tackle projects and never again get stuck behind a technical roadblock.
Join Now