Home > Backend Development > C++ > Why Does OpenSubKey() Return Null for a Registry Key Visible in Regedit?

Why Does OpenSubKey() Return Null for a Registry Key Visible in Regedit?

Mary-Kate Olsen
Release: 2025-01-05 18:39:40
Original
529 people have browsed it

Why Does OpenSubKey() Return Null for a Registry Key Visible in Regedit?

OpenSubKey() Returns Null for Registry Key Visible in Regedit.exe

When attempting to retrieve subkey display names within the registry key HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionUninstall, users may encounter an issue where the OpenSubKey() method returns null for a specific subkey visible in the Registry Editor (regedit.exe).

The underlying cause lies in the default registry view used by 32-bit applications on 64-bit operating systems. By default, these applications access the HKLMSoftwareWow6432Node node, which contains 32-bit registry keys. To read the 64-bit version of the key, it is necessary to specify the RegistryView when opening the base key.

To resolve this issue, utilize the following code:

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

This code ensures that the registry key is opened using the 64-bit view, allowing access to the desired subkey and its display name.

Alternatively, for .NET 3.5 users, it is necessary to resort to P/Invoke to access 64-bit keys, as described in the resource provided in the original answer.

The above is the detailed content of Why Does OpenSubKey() Return Null for a Registry Key Visible in Regedit?. 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