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?
Asked
Active
Viewed 2.9k times
4 Answers
54
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);
}
-
1This solution work very with with my Win7 64bit + unit testing. Thanks. – Jirapong Nov 17 '10 at 03:49
-
2This is the way to read the registry. WoW6432Node shouldn't be read directly as Microsoft handles this through their API and have said it could change in the future. – Jason Aug 08 '13 at 05:42
-
What if you can't change to 4.x? Use the method above? – Maury Markowitz Aug 29 '16 at 17:42
16
The correct way would be to call the native registry api and passing the KEY_WOW64_32KEY
flag to RegOpenKeyEx/RegCreateKeyEx

Anders
- 97,548
- 12
- 110
- 164
-
It should be noted that this solution is only valid when pinvoking or using C++ code. – Kyle Dec 16 '10 at 13:25
-
@Zenox - It is perfectly valid solution even when using a language like C# of course you would be silly to use it. – Security Hound Feb 16 '12 at 18:03
12
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
- 733,204
- 149
- 1,241
- 1,454
-
6Warning: MS say that this approach (hardcoding the "Wow6432Node") is not supported. See http://msdn.microsoft.com/en-us/library/aa384232(VS.85).aspx – Richard Jul 30 '09 at 17:23
-
6-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 – Powerlord Aug 24 '09 at 13:31
-
8@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 Aug 24 '09 at 13:36
-
2@JaredPar I think it's reasonable to downvote answers that lost their usefulness over time. And that rating should eventually reflect an answer's general usefulness, not its history and pedigree. – Tim Lovell-Smith Aug 15 '12 at 17:35
6
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
- 59,778
- 26
- 187
- 249