我无法理解为什么我的源代码只返回注册表DWORD的LOWORD部分。

以下源代码主要基于this MSDN example。我添加了一个部分来输出注册表项的值(数据)。

#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);
}


源将按预期输出键值对,但是对于某些键(例如ScreenBufferSize和WindowSize),仅会打印LOWORD部分。

但是,当运行测试代码时,一切都是完美的。

#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;
}


如何打印完整的DWORD值?

最佳答案

LPBYTE data;
...
(DWORD)*data


这将执行以下操作:从BYTE中获取data,然后将其扩展到DWORD

相反,您想从内存位置获取DWORD

* ((DWORD*) data)

关于c - RegEnumValue仅返回LOWORD,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17906786/

10-12 23:40