Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is OpenSubKey() returning null on my Windows 7 64-bit system?

Do OpenSubKey() and other Microsoft.Win32 registry functions return null on 64-bit systems when 32-bit registry keys are under Wow6432node in the registry?

I'm working on a unit testing framework that makes a call to OpenSubKey() from the .NET library.

My development system is a Windows 7 64-bit environment with Visual Studio 2008 SP1 and the Windows 7 SDK installed.

The application we're unit testing is a 32-bit application, so the registry is virtualized under HKLM\Software\Wow6432node. When we call:

Registry.LocalMachine.OpenSubKey( @"Software\MyCompany\MyApp\" );

Null is returned, however explicitly stating to look here works:

Registry.LocalMachine.OpenSubKey( @"Software\Wow6432node\MyCompany\MyApp\" );

From what I understand this function should be agnostic to 32-bit or 64-bit environments and should know to jump to the virtual node.

Even stranger is the fact that the exact same call inside a compiled and installed version of our application is running just fine on the same system and is getting the registry keys necessary to run; which are also being placed in HKLM\Software\Wow6432node.

What should I do?

like image 778
BrMcMullin Avatar asked Mar 17 '10 17:03

BrMcMullin


3 Answers

It sounds like your unit testing project compiles to 64 bit. In the Compile settings of your unit testing project, set the "Target CPU" to x86 (instead of AnyCPU).

like image 95
Heinzi Avatar answered Oct 18 '22 09:10

Heinzi


If you really need a 32 bit application, you can access the 64 bit registry like this:

RegistryKey localMachine64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey regKey = localMachine64.OpenSubKey(@"Software\MyCompany\MyApp\", false);
like image 11
nmat Avatar answered Oct 18 '22 09:10

nmat


Yes, I also have the same issue with Windows 7 64-bit and Visual Studio 2008 SP1. But my solution is the opposite, which is to change from "x86" to either "Any CPU" or "x64".

like image 3
Rudi Suwanto Avatar answered Oct 18 '22 08:10

Rudi Suwanto