7
votes

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

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

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

2
I don't recall the specific key names, but x86 software on Win64 resides under HKEY_LOCAL_MACHINE\Software\Wow64 or similar named sub keysehe

2 Answers

12
votes

You are falling foul of registry redirection.

The best solution is to open a 64 bit view of the registry, like this:

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 and 64 bit machines then you'll need to code some switching between registry views.

Note: The capability of accessing 64 bit views from 32 bit processes was only added to the .net libraries in .net 4. It seems that prior to that you needed to use native APIs, e.g. with P/Invoke.

3
votes

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

All registry keys will be under HKEY_LOCAL_MACHINE\Software\WOW6432Node

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

The program still "thinks" it's writing to the same path.

I'd suggest you fix your setup (dispatch) program to write the initial settings in the right place.

Edit, Some "information" from MSDN:

Registry Keys Affected by WOW64