Search code examples
javascriptregistryjscriptwshwow64

JavaScript: Read registry without 64bit redirect?


I have the following registry entry on my 64bit system:

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

And I'm trying to read it with a 32bit JS application, but I get automatically redirected to:

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

(which does not exist)

Here is my code:

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

So how can I disable this redirect on a 64bit OS (for RegRead, RegWrite and RegDelete)?


Solution

  • You can do this in two ways:

    1. Run your script under 32-bit Windows Script Host (%windir%\SysWOW64\wscript.exe). In the script code, use the key name without Wow6432Node:

      HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\CCleaner\DisplayVersion
      
    2. Read the registry using the WMI StdRegProv class and force the 32-bit mode using the __ProviderArchitecture flag. Here's an example:

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