如何从 64 位 .NET 应用程序打开 WOW64 注册表项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1074411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:56:45  来源:igfitidea点击:

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

.netregistrywow64

提问by marijne

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?

我的 .NET 应用程序(任何 CPU)需要读取由 32 位程序创建的注册表值。在 64 位 Windows 上,这位于注册表中的 Wow6432Node 项下。我已经读到您不应该对 Wow6432Node 进行硬编码,那么使用 .NET 访问它的正确方法是什么?

采纳答案by JaredPar

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.

如果您明确需要在 64 位程序中读取 32 位程序写入的值,则可以对其进行硬编码。仅仅因为真的没有其他选择。

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);
}

回答by woany

If you can change the target .Net version to v4, then you can use the new OpenBaseKey function e.g.

如果您可以将目标 .Net 版本更改为 v4,那么您可以使用新的 OpenBaseKey 函数,例如

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);
}

回答by Anders

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

正确的方法是调用本机注册表 api 并将KEY_WOW64_32KEY标志传递给 RegOpenKeyEx/RegCreateKeyEx

回答by Ruben Bartelink

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.

扩展安德斯的答案,在 Shahar Prish 的博客上有一个很好的例子,将生成的句柄包装在 .NET RegistryKey 对象中- 不过一定要阅读评论。

Note that unvarnished use of the pinvoke.net wrapper of RegOpenKeyExis fraught with issues.

请注意,未经修饰地使用RegOpenKeyEx 的 pinvoke.net 包装器充满了问题。