Home >Backend Development >C++ >How to Access 64-Bit and 32-Bit Registry Keys 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.
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>
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>
HKEY_LOCAL_MACHINESoftwareWow6432Node
holds values used by 32-bit apps.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>
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!