Access to the registry with C # and "BUILD x86" on a 64-bit machine

I have an application (written in C #) that runs on Windows Server 2008 (64 bit). In this application, I have to check some registry keys against IIS. Among others, I want to access the HKEY_LOCAL_MACHINE \ Software \ Microsoft \ InetStp \ Components \ WMICompatibility key to check if IIS 6 compatibility mode is enabled or not. For this, I use Registry.GetValue Microsoft.Win32 .

For some reason, the solution must be compiled using x86 . The consequence of this is that it is no longer possible to access HKEY_LOCAL_MACHINE \ Software \ Microsoft \ InetStp \ Components , but you can still read the key from HKEY_LOCAL_MACHINE \ Software \ Microsoft \ InetStp . When compiling with AnyCPU "- the registry access flag works fine.

So what is the reason for this behavior? Is there a solution or workaround for this problem?

+7
source share
2 answers

You are in a foul of registry redirection .

The best solution is to open a 64-bit registry view, for example:

using Microsoft.Win32; ... RegistryKey registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64). OpenSubKey(@"Software\Microsoft\InetStp\Components"); object value = registryKey.GetValue(@"WMICompatibility"); 

If you want your code to work on both 32-bit and 64-bit machines, you need to encode some switching between registry types.

Note The ability to access 64-bit representations from 32-bit processes was added only to .net libraries in .net 4. It seems that before that you had to use your own APIs, for example. with P / Invoke.

+12
source

Windows x64 has a separate node program for x86 (not the brightest idea)

All registry keys will be under HKEY_LOCAL_MACHINE \ Software \ WOW6432Node

More on this issue:
How to view the registry using 64-bit versions of Windows

The program still β€œthinks” that it is writing on the same path.

I would advise you to correct your setup (sending) program to record the initial settings in the right place.

Change Some "information" from MSDN:

Registry Keys Affected by WOW64

+3
source

All Articles