使用C#和“BUILD x86”进行注册表访问在64位机器上 [英] Registry access with C# and "BUILD x86" on a 64bit machine

查看:146
本文介绍了使用C#和“BUILD x86”进行注册表访问在64位机器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序(用C#编写),它运行在Windows Server 2008(64位)上。在此应用程序中,我必须检查有关IIS的一些注册表项。其中我想访问密钥 HKEY_LOCAL_MACHINE \Software\Microsoft\InetStp \ Components \WMICompatibility
来检查是否启用了IIS 6兼容模式。我使用 Microsoft.Win32 Registry.GetValue

I have an application (written in C#), which runs on an Windows Server 2008 (64bit). In this application I must check some registry keys regarding the IIS. Among others I want to access the key HKEY_LOCAL_MACHINE\Software\Microsoft\InetStp\Components\WMICompatibility" to check if the IIS 6 compatibility mode is enabled or not. For this I use Registry.GetValue of Microsoft.Win32.

由于某些原因,必须使用<编译解决方案strong> x86 。结果是,不再可以访问 HKEY_LOCAL_MACHINE \Software \ Microsoft /\\ InetStp \ Components ,但仍然可以从 HKEY_LOCAL_MACHINE\Software\Microsoft\InetStp 。使用 AnyCPU 进行编译时 - 标记注册表访问工作正常。

For some reasons the solution must be compiled with x86. The consequence is, that it is no longer possible to access HKEY_LOCAL_MACHINE\Software\Microsoft\InetStp\Components but it is still possible to read key from HKEY_LOCAL_MACHINE\Software\Microsoft\InetStp. When compiling it with "AnyCPU"-flag the registry-access works fine.

那么这种行为的原因是什么?这个问题是否有解决方案或解决方法?

So what is the reason for this behavior? Is there a solution or workaround for this problem?

推荐答案

你正在犯罪注册表重定向

最好的解决方案是打开注册表的64位视图,如下所示:

The best solution is to open a 64 bit view of the registry, like this:

using Microsoft.Win32;
...
RegistryKey registryKey = 
    RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).
    OpenSubKey(@"Software\Microsoft\InetStp\Components");
object value = registryKey.GetValue(@"WMICompatibility");

如果您希望代码在32位和64位计算机上运行,​​那么您需要编码一些在注册表视图之间切换。

If you want your code to work on both 32 and 64 bit machines then you'll need to code some switching between registry views.

注意 :从32位进程访问64位视图的能力只是添加到.net 4中的.net库中。在此之前,您需要使用本机API,例如使用P / Invoke。

Note: The capability of accessing 64 bit views from 32 bit processes was only added to the .net libraries in .net 4. It seems that prior to that you needed to use native APIs, e.g. with P/Invoke.

这篇关于使用C#和“BUILD x86”进行注册表访问在64位机器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆