My .NET application (any-CPU) needs to read a registry value created by a 32-bit program. On 64-bit Windows this goes under the Wow6432Node key in the registry. I have read that you shouldn't hard-code to the Wow6432Node, so what's the right way to access it with .NET?
+3
A:
In the case where you explicitly need to read a value written by a 32 bit program in a 64 bit program, it's OK to hard code it. Simply because there really is no other option.
I would of course abstract it out to a helper function. For example
public RegistryKey GetSoftwareRoot() {
var path = 8 == IntPtr.Size
? @"Software\Wow6432Node"
: @"Software";
return Registry.CurrentUser.OpenSubKey(path);
}
JaredPar
2009-07-02 13:47:49
Fair enough - thanks Jared!
marijne
2009-07-02 14:01:41
Warning: MS say that this approach (hardcoding the "Wow6432Node") is not supported. See http://msdn.microsoft.com/en-us/library/aa384232(VS.85).aspx
Richard
2009-07-30 17:23:32
-1: This behavior breaks in Windows 7/Windows Server 2008 R2, as they use Shared Registry Keys instead: http://msdn.microsoft.com/en-us/library/aa384253(VS.85).aspx
R. Bemrose
2009-08-24 13:31:53
@R. Bemrose, hmm a -1 downvote for an answer which was given before the breaking change was released in RTM. Why not just edit the answer and add a footnote?
JaredPar
2009-08-24 13:36:25
+10
A:
The correct way would be to call the native registry api and passing the KEY_WOW64_32KEY
flag to RegOpenKeyEx/RegCreateKeyEx
Anders
2009-07-03 18:03:04
+4
A:
Extending Anders's answer, there's a good example of wrapping the resulting handle in a .NET RegistryKey object on Shahar Prish's blog - be sure to read the comments too though.
Note that unvarnished use of the pinvoke.net wrapper of RegOpenKeyEx is fraught with issues.
Ruben Bartelink
2009-08-13 08:50:40
A:
If you can change the target .Net version to v4, then you can use the new OpenBaseKey function e.g.
RegistryKey registryKey;
if (Environment.Is64BitOperatingSystem == true)
{
registryKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
}
else
{
registryKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
}
woany
2010-06-01 18:23:44