gpt4 book ai didi

c - 使用 RegGetValue 显示值的数据

转载 作者:太空宇宙 更新时间:2023-11-04 04:28:53 25 4
gpt4 key购买 nike

我试图了解如何在 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;

}

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

Subkey opened! Value data: ?

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

你能帮帮我吗?

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

非常感谢。

最佳答案

您必须根据数据类型选择 wprintf 格式(%x、%s、..)

Can you tell me what mistakes I have, so I can correct them? I'm just trying to learn

尝试像这样开始代码:

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

恐怖

关于c - 使用 RegGetValue 显示值的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38623690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com