How to open a WOW64 registry key from a 64-bit .NET application
Asked Answered
D

4

29

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?

Dish answered 2/7, 2009 at 13:42 Comment(0)
E
13

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);
}
Ellerd answered 2/7, 2009 at 13:47 Comment(4)
Warning: MS say that this approach (hardcoding the "Wow6432Node") is not supported. See msdn.microsoft.com/en-us/library/aa384232(VS.85).aspxCollect
-1: This behavior breaks in Windows 7/Windows Server 2008 R2, as they use Shared Registry Keys instead: msdn.microsoft.com/en-us/library/aa384253(VS.85).aspxIneslta
@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?Ellerd
@Ellerd 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.Mcalpin
C
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);
}
Ceroplastic answered 1/6, 2010 at 18:23 Comment(4)
This solution work very with with my Win7 64bit + unit testing. Thanks.Consolation
This 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.Sympathizer
What if you can't change to 4.x? Use the method above?Marketable
an incorrect answer got 54 votes? these code always access arch-specific native key like HKLM\Software not HKLM\Software\Wow6432Node even on 64-bit os through a 32-bit program.Zeculon
C
17

The correct way would be to call the native registry api and passing the KEY_WOW64_32KEY flag to RegOpenKeyEx/RegCreateKeyEx

Covenanter answered 3/7, 2009 at 18:3 Comment(2)
It should be noted that this solution is only valid when pinvoking or using C++ code.Allstar
@Zenox - It is perfectly valid solution even when using a language like C# of course you would be silly to use it.Thrasonical
E
13

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);
}
Ellerd answered 2/7, 2009 at 13:47 Comment(4)
Warning: MS say that this approach (hardcoding the "Wow6432Node") is not supported. See msdn.microsoft.com/en-us/library/aa384232(VS.85).aspxCollect
-1: This behavior breaks in Windows 7/Windows Server 2008 R2, as they use Shared Registry Keys instead: msdn.microsoft.com/en-us/library/aa384253(VS.85).aspxIneslta
@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?Ellerd
@Ellerd 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.Mcalpin
W
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.

Watering answered 13/8, 2009 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.