简体   繁体   English

无法转换 RegGetValueA 返回的 void 指针

[英]Can't cast void pointer returned by RegGetValueA

I'm trying to access the registry using the RegGetValueA function, but I can't cast the void pointer passed to the function.我正在尝试使用 RegGetValueA function 访问注册表,但我无法将传递给 function 的 void 指针强制转换。 I just get the (value?) of the pointer itself.我只是得到指针本身的(值?)。

Here's my code:这是我的代码:

    LSTATUS res;
    LPCSTR lpSubKey = "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{071c9b48-7c32-4621-a0ac-3f809523288f}";
    LPCSTR lpValue = "InstallSource";
    PVOID pvData = new PVOID;
    LPDWORD pcbData = new DWORD;

    res = RegGetValueA(
        HKEY_LOCAL_MACHINE,
        lpSubKey,
        lpValue,
        RRF_RT_ANY,
        NULL,
        pvData,
        pcbData);

    string* data = static_cast<string*>(pvData);
    cout << data << "\n";
    cout << pvData;

output: output:

000000000045A240
000000000045A240

Any help would be much appreciated.任何帮助将非常感激。

link to documentation: https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-reggetvaluea文档链接: https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-reggetvaluea

You are using the function incorrectly.您错误地使用了 function。

First off, you shouldn't be accessing Wow6432Node directly at all.首先,您根本不应该直接访问Wow6432Node The function has flags for accessing 32bit and 64bit keys when dealing with WOW64. function 在处理 WOW64 时具有访问 32 位和 64 位密钥的标志。

More importantly, you are giving the function a void* pointer that doesn't point at valid memory for your purpose.更重要的是,您为 function 提供了一个void*指针,该指针不指向有效的 memory 用于您的目的。 When reading a string value from the Registry, you must pre-allocate a character buffer of sufficient size, then pass the address of that buffer to the function.从注册表读取字符串值时,您必须预先分配足够大小的字符缓冲区,然后将该缓冲区的地址传递给 function。 You can ask the function for the necessary buffer size.您可以向 function 询问必要的缓冲区大小。

The code should look more like the following instead:代码应该更像下面这样:

LSTATUS res;
LPCSTR lpSubKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{071c9b48-7c32-4621-a0ac-3f809523288f}";
LPCSTR lpValue = "InstallSource";
char *pszData = NULL;
DWORD cbData = 0;

res = RegGetValueA( HKEY_LOCAL_MACHINE, lpSubKey, lpValue, RRF_RT_REG_SZ | RRF_SUBKEY_WOW6432KEY, NULL, NULL, &cbData);
if (res != ERROR_SUCCESS) ...

pszData = new char[cbData];
res = RegGetValueA( HKEY_LOCAL_MACHINE, lpSubKey, lpValue, RRF_RT_REG_SZ | RRF_SUBKEY_WOW6432KEY, NULL, pszData, &cbData);
if (res != ERROR_SUCCESS) ...

cout << pszData << "\n";
delete[] pszData;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM