Home >Backend Development >C++ >How Can a 32-bit Application Access the 64-bit Windows Registry?

How Can a 32-bit Application Access the 64-bit Windows Registry?

Barbara Streisand
Barbara StreisandOriginal
2025-01-16 19:12:13665browse

How Can a 32-bit Application Access the 64-bit Windows Registry?

Read 64-bit registry from 32-bit application

When a 32-bit application is running on a 64-bit Windows system, it can only access the registry key under HKEY_LOCAL_MACHINESoftwareWow6432Node. However, in some cases, access to the 64-bit registry is required.

Solution:

Using .NET Framework 4.x and above:

.NET Framework 4.x and later provides the RegistryView enumeration, allowing direct access to 64-bit and 32-bit registry keys. Here’s how to do it:

Access 64-bit registry:

<code class="language-csharp">using Microsoft.Win32;

RegistryKey localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
// 对64位注册表执行操作</code>

Access 32-bit registry:

<code class="language-csharp">RegistryKey localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
// 对32位注册表执行操作</code>

Alternative:

For situations where you need to access both 64-bit and 32-bit registry keys, you can use the following method:

<code class="language-csharp">// 获取64位和32位节点的所有注册表值
var mergedValues = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).GetValues()
    .Concat(RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).GetValues());

// 获取64位和32位节点的所有注册表项
var mergedKeys = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).GetSubKeyNames()
    .Concat(RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).GetSubKeyNames());</code>

Note:

  • Reading the 64-bit registry is possible thanks to Windows' WOW64 subsystem (which emulates 32-bit operations on 64-bit systems).
  • In Windows 7, there is a bug where the 32-bit code version always returns "Microsoft" for RegisteredOrganization regardless of the actual value.
  • In 32-bit versions of Windows, the WOW6432Node subtree does not exist.

The above is the detailed content of How Can a 32-bit Application Access the 64-bit Windows Registry?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn