Home >Backend Development >C++ >Why Does OpenSubKey() Fail to Retrieve the Expected Registry Subkey on a 64-bit System?

Why Does OpenSubKey() Fail to Retrieve the Expected Registry Subkey on a 64-bit System?

DDD
DDDOriginal
2025-01-05 11:07:39840browse

Why Does OpenSubKey() Fail to Retrieve the Expected Registry Subkey on a 64-bit System?

OpenSubKey() Fails to Retrieve Expected Subkey from Registry

When attempting to retrieve all subkey display names within the following registry path:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

using the provided code, a specific subkey is not accessible:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}

This subkey should display the name "Microsoft Visual C 2010 x64 Redistributable - 10.0.30319." However, the GetSubKeyNames() method retrieves a different subkey:

{DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}.KB2151757

Cause

A 32-bit application on a 64-bit OS will read from the HKLMSoftwareWow6432Node node by default. To address this and access the 64-bit version of the key, the RegistryView must be specified:

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
}

This API was introduced in .NET 4.0. For .NET 3.5, P/Invoke is required:

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 Does OpenSubKey() Fail to Retrieve the Expected Registry Subkey on a 64-bit System?. 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