Create 64 bit registry key (non-WOW64) from a 32 bit application

北城余情 提交于 2019-11-30 05:51:49

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2197086/create-64-bit-registry-key-non-wow64-from-a-32-bit-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!