C++ 从 64 位应用程序读取 SOFTWARE\WOW6432 中的注册表项

标签 c++ winapi registry

我有一些 32 位 C++ 代码来从注册表读取键/值对,如下所示:

    // Get a handle to the required key
    HKEY    hKey;
    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\MyKey", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
    {
        // Look up the value for the key
        bOK = (RegQueryValueEx(hKey, szKey, NULL, NULL, (PBYTE)szValue, &dwMax) == ERROR_SUCCESS);
        
        // Release the handle
        RegCloseKey(hKey);
    }

由于它是一个 32 位应用程序,因此它从 SOFTWARE\WOW6432Node\MyKey 读取

在我将应用程序移植到 64 位之前,它工作得很好,所以它现在从 SOFTWARE\MyKey 读取。

我的安装程序不允许我将条目同时放入注册表的 32 位和 64 位部分,因此我需要 64 位应用程序继续从 WOW6432Node 获取其数据。

有人知道怎么做吗?

非常感谢 托尼·雷诺兹

最佳答案

32-bit and 64-bit Application Data in the Registry 下所述:

The KEY_WOW64_64KEY and KEY_WOW64_32KEY flags enable explicit access to the 64-bit registry view and the 32-bit view, respectively. For more information, see Accessing an Alternate Registry View.

后一个链接解释了这一点

These flags can be specified in the samDesired parameter of the following registry functions:

以下代码可以完成您的要求:

// Get a handle to the required key
HKEY    hKey;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\MyKey", 0, KEY_READ | KEY_WOW64_32KEY, &hKey) == ERROR_SUCCESS)
{
    // ...
}

关于C++ 从 64 位应用程序读取 SOFTWARE\WOW6432 中的注册表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66283273/

相关文章:

使用 WinAPI WriteConsole 的控制台输出

c++ - 发送键盘命令

windows - 如何在批处理或 VBScript 中使用通配符搜索注册表中的键?

windows - 神秘的 native "A"注册表项,路径为 : Registry\A

c# - 如何在 Windows (8.1) 中将自定义应用程序注册为 Web 浏览器?

c++ - 多个文件的 makefile 不起作用

c++ - 何时使用 "identity"tmp 技巧?

c++ - 为什么数据成员在 C++ 中默认是私有(private)的?

c++ - 将窗口固定到桌面/将窗口粘附到桌面/"Always-on-bottom"窗口

c - 带有 TreeView 和面板内存对话框的首选项对话框