Home > Backend Development > C++ > How to Access 64-Bit and 32-Bit Registry Keys from a 32-Bit Application?

How to Access 64-Bit and 32-Bit Registry Keys from a 32-Bit Application?

Linda Hamilton
Release: 2025-01-16 19:07:10
Original
541 people have browsed it

How to Access 64-Bit and 32-Bit Registry Keys from a 32-Bit Application?

Accessing the 64-bit Registry from a 32-bit Application

Thanks to WOW64 (Windows on Windows 64-bit), 32-bit applications can access the 64-bit registry. This subsystem facilitates 64-bit access within the 32-bit environment.

Accessing the 64-Bit Registry Key

Use RegistryView.Registry64 to access the 64-bit registry:

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

string value64 = string.Empty;
RegistryKey localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
localKey = localKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
if (localKey != null)
{
    value64 = localKey.GetValue("RegisteredOrganization").ToString();
    localKey.Close();
}
Console.WriteLine($"RegisteredOrganization [64-bit]: {value64}");</code>
Copy after login

Accessing the 32-Bit Registry Key

Use RegistryView.Registry32 to access the 32-bit registry:

<code class="language-csharp">string value32 = string.Empty;
RegistryKey localKey32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
localKey32 = localKey32.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
if (localKey32 != null)
{
    value32 = localKey32.GetValue("RegisteredOrganization").ToString();
    localKey32.Close();
}
Console.WriteLine($"RegisteredOrganization [32-bit]: {value32}");</code>
Copy after login

Important Considerations: WOW64, Registry Paths, and Key Queries

  • On 64-bit Windows, HKEY_LOCAL_MACHINESoftwareWow6432Node holds values used by 32-bit apps.
  • Both 32-bit and 64-bit applications access HKEY_LOCAL_MACHINESoftware as expected due to WOW64 redirection.
  • HKEY_LOCAL_MACHINESoftwareWow6432Node is absent in older Windows versions and 32-bit Windows 7.

To retrieve values from both 64-bit and 32-bit registries (useful for scenarios like retrieving SQL instance names), a union query is recommended:

<code class="language-csharp">public static IEnumerable<string> GetAllRegValueNames(string regPath, RegistryHive hive = RegistryHive.LocalMachine)
{
    var reg64 = GetRegValueNames(RegistryView.Registry64, regPath, hive);
    var reg32 = GetRegValueNames(RegistryView.Registry32, regPath, hive);
    var result = (reg64 != null && reg32 != null) ? reg64.Union(reg32) : (reg64 ?? reg32);
    return (result ?? new List<string>().AsEnumerable()).OrderBy(x => x);
}</code>
Copy after login

This provides a consolidated view of registry values, regardless of the application's architecture.

The above is the detailed content of How to Access 64-Bit and 32-Bit Registry Keys from a 32-Bit Application?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template