使用RegGetValue显示值的数据

时间:2016-07-27 21:42:27

标签: c winapi

我试图了解如何在WinApi中使用PVOID数据类型,所以我尝试使用RegGetValue函数显示一个名为MyValue的值(它是一个DWORD值),但它对我不起作用。

这是我的代码:

int wmain()
{
    LONG openKey;
    HKEY hKey = HKEY_CURRENT_USER;
    LPCWSTR subKey = L"WinSide\\Config";
    DWORD options = 0;
    REGSAM samDesired = KEY_READ | KEY_WRITE;

    //Allocating memory for a HKEy value.
    PHKEY pkOpenResult = (PHKEY)malloc(sizeof(HKEY));

    //GetValue
    LONG getValue;
    LPCWSTR pValue = L"MyValue";
    DWORD flags = RRF_RT_ANY;

    //Allocationg memory for a DWORD value.
    LPDWORD dataType = (LPDWORD)malloc(sizeof(DWORD));

    WCHAR value[255];

    PVOID pvData = (PVOID)value; //No idea if this is the rigth way.
    DWORD size = 8192;
    LPDWORD pcbData = &size;

    openKey = RegOpenKeyEx(hKey, subKey, options,
                            samDesired, pkOpenResult);

    if (openKey != ERROR_SUCCESS)
    {
        wprintf(L"The %s subkey could not be opened. Error code: %li\n", subKey, openKey);
        free(pkOpenResult);
    }
    else
    {
        wprintf(L"Subkey opened!\n");
        getValue = RegGetValue(*pkOpenResult, NULL, pValue, flags, dataType, pvData, pcbData);

        if (getValue != ERROR_SUCCESS)
        {
            wprintf(L"Error getting value. Code: %li\n", getValue);
            free(pkOpenResult);
        }
        else
        {
            wprintf(L"Value data: %s\n", pvData);
            free(pkOpenResult);
        }
    }


    return 0;   

}

这是命令提示符显示的内容:

  

子键打开了!价值数据:?

不知道我应该如何声明和使用pvData值来显示rigth内容。

你能帮帮我吗?

还有其他类似的帖子,但我不明白他们的代码,所以决定展示自己的代码。

非常感谢。

1 个答案:

答案 0 :(得分:0)

您必须根据dataType

选择wprintf格式(%x,%s,..)
  

你能告诉我我有什么错误,所以我可以纠正它们吗?我只是   试图学习

尝试开始这样的代码:

int wmain()
{
    HKEY hKey = HKEY_CURRENT_USER;
    LPCWSTR subKey = L"WinSide\\Config";
    DWORD options = 0;
    REGSAM samDesired = KEY_READ;// | KEY_WRITE - need ?;

    HKEY OpenResult;

    LPCWSTR pValue = L"MyValue";
    DWORD flags = RRF_RT_ANY;

    //Allocationg memory for a DWORD value.
    DWORD dataType;

    WCHAR value[255];
    PVOID pvData = value;

    DWORD size = sizeof(value);// not 8192;

    LONG err = RegOpenKeyEx(hKey, subKey, options, samDesired, &OpenResult);

    if (err != ERROR_SUCCESS)
    {
        wprintf(L"The %s subkey could not be opened. Error code: %x\n", subKey, err);
    }
    else
    {
        wprintf(L"Subkey opened!\n");
        err = RegGetValue(OpenResult, NULL, pValue, flags, &dataType, pvData, &size);

        if (err != ERROR_SUCCESS)
        {
            wprintf(L"Error getting value. Code: %x\n", err);
        }
        else
        {
            switch (dataType)
            {
            case REG_DWORD:
                wprintf(L"Value data: %x\n", *(DWORD*)pvData);
                break;
            case REG_SZ:
                //if ( !*((PWSTR)((PBYTE)pvData + size)-1) )
                wprintf(L"Value data: %s\n", (PWSTR)pvData);
                break;
                //...
            }
        }
        RegCloseKey(OpenResult);// don't forget !
    }

    return err;   
}

尺寸!= 8192;

RegCloseKey - 在哪里?

   //Allocationg memory for a DWORD value.
    LPDWORD dataType = (LPDWORD)malloc(sizeof(DWORD));

恐怖