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

Mar*_*ark 4 c# registry wmi 64-bit 32-bit

我认为这个问题真的总结了我正在努力做的事情.这是我正在使用的代码.除非我的应用程序在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";
      }
 }
Run Code Online (Sandbox Code Playgroud)

Gui*_*ros 7

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

myScope.Options.Context.Add("__ProviderArchitecture", 64);
myScope.Options.Context.Add("__RequiredArchitecture", true);
Run Code Online (Sandbox Code Playgroud)

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