Home > Backend Development > C++ > Why Can't My 32-bit Application Access the 64-bit Registry Key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall?

Why Can't My 32-bit Application Access the 64-bit Registry Key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall?

DDD
Release: 2025-01-05 16:19:41
Original
178 people have browsed it

Why Can't My 32-bit Application Access the 64-bit Registry Key HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionUninstall?

Unable to Open Registry Key in OpenSubKey()

When attempting to retrieve display names for subkeys within the registry key HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionUninstall, using the code:

RegistryKey newKey;
string val;

string KeyPath64Bit = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
RegistryKey mainKey = Registry.LocalMachine.OpenSubKey(KeyPath64Bit);

string[] RegKeys64Bits = Registry.LocalMachine.OpenSubKey(KeyPath64Bit).GetSubKeyNames();

foreach (string s in RegKeys64Bits)
{
   newKey = mainKey.OpenSubKey(s);
   val = newKey.GetValue("DisplayName", -1, RegistryValueOptions.None).ToString();
   if (val != "-1")
      file64.WriteLine(val);
}
Copy after login

One specific subkey, {DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}, remains elusive. Instead, GetSubKeyNames() returns {DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}.KB2151757, a subkey that lacks a display name.

Cause:

On a 64-bit operating system, a 32-bit application by default accesses the HKLMSoftwareWow6432Node registry key. To retrieve the 64-bit version of the key containing the desired subkey, you need to specify RegistryView.Registry64.

Solution:

In .NET 4.0 and above, you can open the 64-bit registry key using the RegistryView property:

using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
using (var key = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"))
{
   // key now points to the 64-bit key
}
Copy after login

For .NET 3.5, you can utilize P/Invoke to access 64-bit keys. Refer to the documentation at the following link for detailed instructions:

http://www.rhyous.com/2011/01/24/how-read-the-64-bit-registry-from-a-32-bit-application-or-vice-versa/

The above is the detailed content of Why Can't My 32-bit Application Access the 64-bit Registry Key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template