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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6991431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:45:14  来源:igfitidea点击:

VBS Windows 64bit/32bit registry read problem

windowsvbscript64-bit32-bit

提问by Ben

What I am trying to achieve I feel should be fairly simple yet it is driving me totally insane.

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

Background: We run a system monitoring tools across our clients which has the ability to run .vbs scripts remotely. This works very well normally.

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

What I am trying to achieve currently is being able to read a line from the registry on both 32bit versions of windows and 64 bit versions.

我目前想要实现的是能够从 32 位版本的 Windows 和 64 位版本的注册表中读取一行。

The Client side.exe which monitors the machine runs as a 32bit process on BOTH platforms (This is the trick).

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

I want to read a key from HKEY_LOCAL_MACHINE\SOFTWARE\ for example. My script works perfectly fine on 32bit. example: objRegistry.RegRead("HKEY_LOCAL_MACHINE\Software\anything")

例如,我想从 HKEY_LOCAL_MACHINE\SOFTWARE\ 读取密钥。我的脚本在 32 位上运行良好。示例:objRegistry.RegRead("HKEY_LOCAL_MACHINE\Software\anything")

The problem I have is when I run this same line on a 64bit folder is is automatically looking in the wow64node folder. Example: objRegistry.RegRead("HKEY_LOCAL_MACHINE\Software\wow64node\").

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

I need to have it checking in EXACTLY the same place.

我需要让它在完全相同的地方检查。

The key it is reading is part of a program that runs both 32bit and 64bit versions which is why it is not installed in the wow64node folder.

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

At this point I am unable to run the .VBS script as a 64bit process which would solve my problem entirely as it would then not look in the wow64node folder.

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

If anyone has any ideas at all please let me know.

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

回答by Ben

I solved it using this piece of code.

我用这段代码解决了它。

Const HKEY_LOCAL_MACHINE = &H80000002
sPath = ReadRegStr (HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\ASP.NET.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("", "root\default", "", "", , , , 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

Thank you Helen for your help!

谢谢海伦的帮助!

回答by Helen

Instead of WshShell.RegRead, use the WMI StdRegProvclass — it allows you to specify whether you want to read from the 32-bit or 64-bit registry. Check out this MSDN article for more info and examples:

而不是WshShell.RegRead使用 WMIStdRegProv类——它允许您指定是要从 32 位还是 64 位注册表中读取。查看此 MSDN 文章以获取更多信息和示例:

Requesting WMI Data on a 64-bit Platform

在 64 位平台上请求 WMI 数据