繁体   English   中英

如何使用WMI通过C#在64位计算机(WOW)上以32位模式运行的应用程序访问64位注册表配置单元信息

[英]How can I access 64 bit registry hive information from a application running in 32 bit mode on a 64 bit machine(WOW) using WMI through C#

我认为这个问题真的总结了我正在努力做的事情。 这是我正在使用的代码。 除非我的应用程序在64位计算机上以32位模式运行,否则它适用于所有方案。 无论我如何玩__ProviderArchitecture和__RequiredArchitecture标志,我总是只能访问蜂巢的32位部分(WOW6432Node)

uint LOCAL_MACHINE = 0x80000002;
string results = "";
ConnectionOptions options = new ConnectionOptions();
options.Impersonation = ImpersonationLevel.Impersonate;
options.EnablePrivileges = true;
options.Username = this.txtUser.Text;
options.Password = this.txtPassword.Text;

ManagementScope myScope = new ManagementScope("\\\\" + this.txtMachine.Text + "\\root\\default", options);
ManagementPath mypath = new ManagementPath("StdRegProv");
ManagementClass mc = new ManagementClass(myScope, mypath, null);

ManagementBaseObject inParams = mc.GetMethodParameters("EnumKey");
inParams["hDefKey"] = LOCAL_MACHINE;
inParams["sSubKeyName"] = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";

ManagementNamedValueCollection objCtx = new ManagementNamedValueCollection();
objCtx.Add("__ProviderArchitecture", 64);
objCtx.Add("__RequiredArchitecture", true);


InvokeMethodOptions invokeOptions = new InvokeMethodOptions();
invokeOptions.Context = objCtx;
ManagementBaseObject outParams = mc.InvokeMethod("EnumKey", inParams, invokeOptions);

inParams = mc.GetMethodParameters("GetStringValue");
inParams["hDefKey"] = LOCAL_MACHINE;

foreach(string name in (string[])outParams["sNames"])
{
      inParams["sSubKeyName"] = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" + "\\" + name;
      inParams["sValueName"] = "DisplayName";
      outParams = mc.InvokeMethod("GetStringValue", inParams, invokeOptions);

      if (!string.IsNullOrEmpty(((string)outParams["sValue"])))
      {
          results += outParams["sValue"] + "\t";
      }
 }

我认为没有必要启动一个单独的流程。 我能够从32位进程访问远程计算机的64位注册表。 在上面的示例中,我已将选项直接添加到作用域,而不是在调用选项中设置它们。

myScope.Options.Context.Add("__ProviderArchitecture", 64);
myScope.Options.Context.Add("__RequiredArchitecture", true);

还需要注意的是,在.net 4中,OpenRemoteBaseKey函数现在有一个参数(RegistryView),请参见msdn 这里

您需要使用KEY_WOW64_64KEY标志设置打开键。 MSDN文档很好地介绍了这一点。

请特别注意您仍然只需要HKLM /软件或类似软件。 你不能试图通过WoW6432Node重定向器,否则你将陷入循环! 这里有关于该主题的更多细节

我已经通过启动以原生64位运行的单独进程解决了这个问题。 此过程可以轻松访问两个配置单元。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM