VBS Windows 64位/32位注册表读取问题



我想实现的目标应该相当简单,但它却让我完全疯了。

背景:我们在客户端上运行一个系统监控工具,该工具能够远程运行.vbs脚本。这在正常情况下运行得很好。

我目前正在努力实现的是能够在32位版本的windows和64位版本上从注册表中读取一行。

监控机器的Client side.exe在两个平台上都作为32位进程运行(这就是诀窍(。

例如,我想从HKEY_LOCAL_MACHINE\SOFTWARE\中读取一个键。我的脚本在32位上运行得非常好。示例:objRegistry。RegRead("HKEY_LOCAL_MACHINE\Software\anything"(

我遇到的问题是,当我在64位文件夹上运行同一行时,它会自动在wow64node文件夹中查找。示例:objRegistry。RegRead("HKEY_LOCAL_MACHINE\Software\wow64node\"(。

我需要让它在同一个地方办理入住手续。

它正在读取的密钥是同时运行32位和64位版本的程序的一部分,这就是为什么它没有安装在wow64node文件夹中。

目前我无法运行。VBS脚本作为一个64位进程,它将完全解决我的问题,因为它不会在wow64node文件夹中查找。

如果有人有任何想法,请告诉我。

我使用这段代码解决了这个问题。

Const HKEY_LOCAL_MACHINE = &H80000002
sPath = ReadRegStr (HKEY_LOCAL_MACHINE, "SOFTWAREMicrosoftASP.NET2.0.50727.0", "Path", 64)
WScript.Echo sPath
' 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(VS.85).aspx for a list of possible values).
'   Key - The key that contains the desired value.
'   Value - The value that you want to get.
'   RegType - The registry bitness: 32 or 64.
'
Function ReadRegStr (RootKey, Key, Value, RegType)
    Dim oCtx, oLocator, oReg, oInParams, oOutParams
    Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
    oCtx.Add "__ProviderArchitecture", RegType
    Set oLocator = CreateObject("Wbemscripting.SWbemLocator")
    Set oReg = oLocator.ConnectServer("", "rootdefault", "", "", , , , oCtx).Get("StdRegProv")
    Set oInParams = oReg.Methods_("GetStringValue").InParameters
    oInParams.hDefKey = RootKey
    oInParams.sSubKeyName = Key
    oInParams.sValueName = Value
    Set oOutParams = oReg.ExecMethod_("GetStringValue", oInParams, , oCtx)
    ReadRegStr = oOutParams.sValue
End Function

谢谢海伦的帮助!

使用WMI StdRegProv类而不是WshShell.RegRead;它允许您指定是从32位注册表读取还是从64位注册表读取。查看MSDN的这篇文章,了解更多信息和示例:

在64位平台上请求WMI数据

最新更新