C++ 无法读取注册表值数据

C++ Can't read registry value data

最近在为客户工作,需要从注册表中读取值。所以我想从尝试一些简单的事情开始,从注册表中读取系统 Guid。这是我正在使用的代码,但我无法弄清楚如何正确读取某些数据。我发现了如何从 here 中读取 DWORD,但这不适用于从注册表中读取系统 Guid。另外,我正在为 64 位编译。这是我一直在使用的代码

#include <iostream>
#include <string>
#include <Windows.h>
#include <processthreadsapi.h>
#include <tchar.h>
#include <cstring>
int main()
{
    DWORD val;
    DWORD dataSize = sizeof(val);
    if (ERROR_SUCCESS == RegGetValueA(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Cryptography", "MachineGuid", RRF_RT_ANY, nullptr, &val, &dataSize))
    {
        printf("Value is %i\n", val);
    }
    else
    {
        printf("Read Error");
    };
    system("pause");
    return 1;
};

似乎无论我尝试什么,我总是收到读取错误。在过去的一小时十五分钟里,我一直在尝试新事物并在线阅读文章。决定制作一个 post 看看是否有人可以帮助我。任何帮助表示赞赏! (此外,如果您出于任何原因需要知道,我使用了 Visual Studio)。提前致谢!

在我的环境中,键 SOFTWARE\Microsoft\Cryptography 中的值 MachineGuid 是一个长度超过 4 个字符的字符串,而不是 DWORD 值。

您必须分配足够的空间来读取该值。否则,ERROR_MORE_DATA 将返回为 documented

#include <cstdio>
#include <cstdlib>
#include <Windows.h>
int main()
{
    char val[128];
    DWORD dataSize = sizeof(val);
    if (ERROR_SUCCESS == RegGetValueA(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Cryptography", "MachineGuid", RRF_RT_ANY, nullptr, &val, &dataSize))
    {
        printf("Value is %.*s\n", (int)dataSize, val);
    }
    else
    {
        printf("Read Error");
    };
    system("pause");
    return 1;
}