views:

1760

answers:

2

I'm running a VBScript that may run under x64 Windows. I need to read a registry key from the 32-bit part of the registry. For that I use path HKLM\Software\Wow6432Node\xyz instead of HKLM\Software\xyz. How can I check if the script is executed under x64?

+2  A: 

I'm not sure you need to check if the script is executing under x64.

Try to read from HKLM\Software\Wow6432Node\xyz, if that fails, try to read from HKLM\Software\xyz, if that fails, your registry key doesn't exist, take whatever action is appropriate.

Of course, if your design is more complicated (for example, you write a value into that registry key if it doesn't exist) then that suggestion won't work.

Here is a VBScript for examining the operating system. You'll probably also need explanation of the Properties available from the Win32_OperatingSystem Class

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")        

Set colOperatingSystems = objWMIService.ExecQuery _
 ("Select * from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems
 msg = objOperatingSystem.Caption & " " & _
   objOperatingSystem.Version & " " & _
    objOperatingSystem.OSArchitecture
 msgbox msg
Next

Note that for Windows XP and 2003, OSArchitecture is not available, in which case you will probably have to examine either the Caption or Version to determine whether your OS is 64-bit.

You could also use something like this depending on the level of complexity you require.

Grant Wagner
I'm just reading from a specific key, so the test for reading from the node in Wow6432Node is enough. Thanks!
vividos
A: 

You didn't mention what API you use to read from the registry. For example, if you use the WMI StdRegProv class, you can use the __ProviderArchitecture flag to request access to the 32-bit registry hive regardless of whether the script is run under 32-bit or 64-bit Windows Script Host. This technique is described in the Requesting WMI Data on a 64-bit Platform article in MSDN.

Here's an example of reading from the 32-bit registry:

strComputer = "."
Const HKLM = &h80000002

''# Specify the required registry bitness
Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
oCtx.Add "__ProviderArchitecture", 32
oCtx.Add "__RequiredArchitecture", True

''# Load the 32-bit registry provider
Set oLocator = CreateObject("WbemScripting.SWbemLocator")
Set oWMI = oLocator.ConnectServer(strComputer, "root\default",,,,,, oCtx)
Set oReg = oWMI.Get("StdRegProv") 

''# Specify input parameters for the GetStringValue method call
Set oInParams = oReg.Methods_("GetStringValue").InParameters
oInParams.hDefKey     = HKLM
oInParams.sSubKeyName = "Software\xyz"
oInParams.sValueName  = "foobar"

''# Read a string value from the registry
Set oOutParams = oReg.ExecMethod_("GetStringValue", oInParams,, oCtx)
WScript.Echo oOutParams.sValue

Note also, that in this case the 32-bit key names should be specified in a usual way as HKLM\Software\xyz instead of HKLM\Software\Wow6432Node\xyz.

Helen