JavaScript:没有64位重定向读取注册表?

时间:2014-07-15 11:54:52

标签: javascript registry jscript wsh wow64

我的64位系统上有以下注册表项:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\CCleaner\DisplayVersion

我尝试使用32位JS应用程序阅读它,但我会自动重定向到:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\CCleaner\DisplayVersion

(不存在)

这是我的代码:

var WshShell = new ActiveXObject("WScript.Shell");
var installedVersion = WshShell.RegRead("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\CCleaner\\DisplayVersion");

那么如何在64位操作系统上禁用此重定向(对于RegRead,RegWrite和RegDelete)?

1 个答案:

答案 0 :(得分:1)

您可以通过两种方式执行此操作:

  1. 在32位Windows脚本宿主(%windir%\SysWOW64\wscript.exe)下运行脚本。在脚本代码中,使用键名而不使用Wow6432Node

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\CCleaner\DisplayVersion
    
  2. 使用WMI StdRegProv类读取注册表,并使用__ProviderArchitecture标志强制使用32位模式。这是一个例子:

    var HKEY_LOCAL_MACHINE = 0x80000002;
    var sValue = ReadRegStr(HKEY_LOCAL_MACHINE,
                            "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\CCleaner",
                            "DisplayVersion",
                            32);  // <------------
    WScript.Echo(sValue);
    
    // Reads a REG_SZ value from the local computer's registry using WMI.
    // Parameters:
    //   RootKey - The registry hive (see http://msdn.microsoft.com/en-us/library/aa390788.aspx for possible values).
    //   Key - The key that contains the needed value.
    //   Value - The value to get.
    //   RegType - The registry bitness: 32 or 64.
    function ReadRegStr (RootKey, KeyName, ValueName, RegType)
    {
      var oCtx = new ActiveXObject("WbemScripting.SWbemNamedValueSet");
      oCtx.Add("__ProviderArchitecture", RegType);
    
      var oLocator = new ActiveXObject("WbemScripting.SWbemLocator");
      var oWMI = oLocator.ConnectServer("", "root\\default", "", "", "", "", 0, oCtx);
      var oReg = oWMI.Get("StdRegProv");
    
      var oInParams = oReg.Methods_("GetStringValue").Inparameters;
      oInParams.Hdefkey = RootKey;
      oInParams.Ssubkeyname = KeyName;
      oInParams.Svaluename = ValueName;
    
      var oOutParams = oReg.ExecMethod_("GetStringValue", oInParams, 0, oCtx);
      return oOutParams.SValue;
    }