Question

I'm having trouble understanding why my source is only returning the LOWORD part of a registry DWORD.

The following source code is largely based on this MSDN example. I've added in a section to output the value (data) of a registry key.

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383

void QueryKey(HKEY hKey) 
{ 
    TCHAR    achClass[MAX_PATH] = TEXT("");  // buffer for class name 
    DWORD    cchClassName = MAX_PATH;  // size of class string 
    DWORD    cSubKeys=0;               // number of subkeys 
    DWORD    cbMaxSubKey;              // longest subkey size 
    DWORD    cchMaxClass;              // longest class string 
    DWORD    cValues;              // number of values for key 
    DWORD    cchMaxValue;          // longest value name 
    DWORD    cbMaxValueData;       // longest value data 
    DWORD    cbSecurityDescriptor; // size of security descriptor 
    FILETIME ftLastWriteTime;      // last write time 

    DWORD i, retCode; 

    TCHAR  achValue[MAX_VALUE_NAME];
    DWORD cchValue = MAX_VALUE_NAME;
    DWORD dataType = 0, dataSize;
    LPBYTE data = (LPBYTE)malloc(512);

    // Get the class name and the value count. 
    retCode = RegQueryInfoKey(
      hKey,                    // key handle 
      achClass,                // buffer for class name 
      &cchClassName,           // size of class string 
      NULL,                    // reserved 
      &cSubKeys,               // number of subkeys 
      &cbMaxSubKey,            // longest subkey size 
      &cchMaxClass,            // longest class string 
      &cValues,                // number of values for this key 
      &cchMaxValue,            // longest value name 
      &cbMaxValueData,         // longest value data 
      &cbSecurityDescriptor,   // security descriptor 
      &ftLastWriteTime);       // last write time 

    // Enumerate the key values. 

    if (cValues) 
    {
        printf( "\nNumber of values: %d\n", cValues);

        for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++) 
        { 
            cchValue = MAX_VALUE_NAME; 
            achValue[0] = '\0'; 
            retCode = RegEnumValue(hKey, i, achValue, &cchValue, 
                             NULL, &dataType, data, &dataSize);

            if (retCode == ERROR_SUCCESS && dataType == REG_DWORD ) 
            {
                _tprintf(TEXT("(%d) %s: 0x%08X\n"), i+1, achValue, (DWORD)*data);
            } 
        }
    }
}

void __cdecl _tmain(void)
{
    HKEY hTestKey;

    if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Console"), 0, KEY_READ, &hTestKey) == ERROR_SUCCESS)
    {
        QueryKey(hTestKey);
    }

    RegCloseKey(hTestKey);
}

The source outputs the key-value pair as expected, but for certain keys (eg. ScreenBufferSize and WindowSize) only the LOWORD part is printed.

But when test code is run, everything is perfect.

#include <windows.h>
#include <stdio.h>

int main(void)
{
    DWORD d = 0x00190050;

    printf("DWORD: %08x\n", d);
    printf("HIWORD: %04x, LOWORD: %04x\n", HIWORD(d), LOWORD(d));

    return 0;
}

How can I print the full DWORD value?

Was it helpful?

Solution

LPBYTE data;
...
(DWORD)*data

this does the following: it takes a BYTE from data and then extends it to DWORD.

Instead you want to take DWORD from the memory location:

* ((DWORD*) data)

OTHER TIPS

You need to set dataSize to the size of data before each call to RegEnumValue(), like:

dataSize = 512; // size of data
retCode = RegEnumValue(hKey, i, achValue, &cchValue, NULL, &dataType, data, &dataSize);

See the description for lpcbData in the MSDN documentation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top