14

I have a Visual Studio installer that is creating some registry keys:

HKEY_LOCAL_MACHINE\SOFTWARE\MyApp

but the registry keys it is creating are automatically appearing under Wow6432Node:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MyApp

How do I ignore the Wow6432Node when creating registry keys in my C# code being executed by the msi?

James Newton-King
  • 48,174
  • 24
  • 109
  • 130
  • I guess, since your application is 32-bit, Windows make sure that the changes come under the `Wow6432Node`. To get it out of that node and place the entries under normal paths, your application should be 64-bit. – legends2k Feb 20 '10 at 01:15

3 Answers3

23

Just FYI, .NET 4.0 supports this natively. Example:

RegistryBase = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);

You can then use that RegistryBase variable to access anything in the 64-bit view of HKLM. Conversely, Registry32 will let a 64-bit application access the 32-bit view of the registry.

CXL
  • 1,094
  • 2
  • 15
  • 38
2

Since there is very little documentation about OpenBaseKey, I'll expand on shifuimam's answer and provide a solution for the OP:

Private Sub Foo()
    Dim myAppIs64Bit = Environment.Is64BitProcess
    Dim baseKey As RegistryKey
    If (myAppIs64Bit) Then
        baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
    Else
        baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)
    End If
    Dim myAppKey As RegistryKey = baseKey.OpenSubKey("SOFTWARE\MyApp")
End Sub

If the app is 32-bit, myAppKey points to HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MyApp. If 64-bit, it points to HKEY_LOCAL_MACHINE\SOFTWARE\MyApp.

The advantage of OpenBaseKey is that it eliminates the need to reference Wow6432 in your code.

mr_plum
  • 2,448
  • 1
  • 18
  • 31
1

Take a look at http://www.pinvoke.net/default.aspx/advapi32/regopenkeyex.html. You'll need to use the registry redirector and pass in the proper value for the access mask. Unfortunately you'll need pInvoke.

nithins
  • 3,172
  • 19
  • 21
  • Oh fantastic. Is there any nice .NET API around that someone has written to wrap the pinvoke calls and make it not so terrible? – James Newton-King Feb 04 '10 at 03:40
  • I wouldn't say it's that terrible really. A bit inconvenient perhaps. Just add the DllImports and define KEY_WOW64_64KEY. A more concise example (for deletion) can be found on http://geekswithblogs.net/derekf/archive/2007/06/26/113485.aspx. – nithins Feb 04 '10 at 03:53