我正在运行可能在x64 Windows下运行的VBScript。我需要从注册表的32位部分读取注册表项。为此,我使用路径HKLM\Software\Wow6432Node\xyz
而不是HKLM\Software\xyz
。如何检查脚本是否在x64下执行?如何检查wscript/cscript是否在x64主机操作系统上运行?
回答
我不确定你需要检查脚本是否在x64下执行。
尝试从HKLM\Software\Wow6432Node\xyz
读取,如果失败,请尝试从HKLM\Software\xyz
读取,如果失败,您的注册表项不存在,请采取适当的措施。
当然,如果你的设计较为复杂(例如,你写一个值,该注册表项,如果不存在的话),那么建议将无法正常工作。
这是用于检查操作系统的VBScript。你可能还需要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
注意,对于Windows XP和2003,OSArchitecture
不可用,在这种情况下,你可能会检查的解释要么Caption
或Version
,以确定您的操作系统是否是64位。
您也可以使用类似this取决于您所需要的复杂程度。
你没有提到你用什么API来从注册表中读取。例如,如果使用WMI StdRegProv
类,则可以使用__ProviderArchitecture
标志来请求访问32位注册表配置单元,无论该脚本是在32位还是64位Windows脚本主机下运行。 MSDN中的Requesting WMI Data on a 64-bit Platform文章介绍了这种技术。
下面是从32位注册表读取一个例子:
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
还要注意,在这种情况下,32位密钥的名称应当以通常的方式为HKLM\Software\xyz
,而不是HKLM\Software\Wow6432Node\xyz
规定。
即使在64位版本的Windows上,脚本也可以在32位模式下执行。
您可以使用下面的代码,以确定真正的位模式,你的脚本上运行:
option explicit
function Determine64BitMode
dim Shell, Is64BitOs
set Shell = CreateObject("WScript.Shell")
on error resume next
Shell.RegRead "HKLM\Software\Microsoft\Windows\CurrentVersion\ProgramFilesDir (x86)"
Is64BitOs = Err.Number = 0
on error goto 0
if Is64BitOs then
Determine64BitMode = InStr(Shell.RegRead("HKLM\Software\Microsoft\Windows\CurrentVersion\ProgramFilesDir"), "(x86)") = 0
else
Determine64BitMode = false
end if
end function
dim ExecutingIn64BitMode
ExecutingIn64BitMode = Determine64BitMode
if ExecutingIn64BitMode then
MsgBox "64 bit"
else
MsgBox "32 bit"
end if
下面是基于Microsoft知识库文章How To Check If Computer Is Running A 32 Bit or 64 Bit Operating System的解决方案:
Function Is64BitOS()
Is64BitOS = Not(Is32BitOS())
End Function
Function Is32BitOS()
Const sRegKey = "HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor\0"
Const sIdentifierValue = "Identifier"
Const sPlatformIDValue = "Platform ID"
Dim oSh : Set oSh = CreateObject("WScript.Shell")
Dim sIdentifier, nPlatformID
sIdentifier = oSh.RegRead(sRegKey & "\" & sIdentifierValue)
nPlatformID = oSh.RegRead(sRegKey & "\" & sPlatformIDValue)
Set oSh = Nothing
If InStr(sIdentifier, "x86") > 0 And nPlatformID = 32 Then
Is32BitOS = True
Else
Is32BitOS = False
End if
End Function
替代解决方案
另一种更简洁的解决方案,使得使用的WMI可以发现here。
这显示系统和工艺两种架构:
Option Explicit
Dim WshShell, WshEnv
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("System")
MsgBox "System: " & WshEnv("PROCESSOR_ARCHITECTURE")
Set WshEnv = WshShell.Environment("Process")
MsgBox "Process: " & WshEnv("PROCESSOR_ARCHITECTURE")
只需选中你需要为<> "x86"
之一。
- 1. 确定是否在MATLAB中运行x64或x86操作系统
- 2. 在x64操作系统/平台上运行x86 msi设置
- 3. x64操作系统如何运行为x86机器编译的代码
- 4. 如何将guest操作系统中运行之间的主机操作系统与QEMU和主机操作系统传输信号
- 5. 是否可以在托管操作系统上运行“本机”代码?
- 6. 如何检查当前的操作系统是否为Windows 7?
- 7. 如何检查程序/软件包是否安装在操作系统上?
- 8. 是否有宏检测操作系统...?
- 9. 如何制作运行docker本身的主机操作系统的docker镜像?
- 10. 如何在XPMode中获取VS2010以查看在主机Win7操作系统上运行的SQL Server?
- 11. 连接到在主机操作系统上运行的Postgrsql数据库
- 12. 如何检测用户正在运行的操作系统?
- 13. 如何检查IP地址是多宿主系统上的本地主机?
- 14. 如何在'make'中检查当前的操作系统是?
- 15. 如何从主机操作系统运行Docker容器的应用程序?
- 16. 如何在Docker上运行* BSD操作系统?
- 17. 如何在Linux上检查给定的操作(或系统调用)是否为原子操作?
- 18. SVN服务器主机操作系统
- 19. 如何检查当前的操作系统是否为Windows 10并且该设备是否是手机?
- 20. 是否可以在主机操作系统中创建makefile步骤在VM中运行的客户操作系统内执行shell命令?
- 21. 如何是操作系统
- 22. C#如何获得操作系统体系结构(x86或x64)?
- 23. 如何检查主机是否可达?
- 24. 如何检查Java是否安装在我的Windows系统上?
- 25. 是否有可能在Android操作系统上运行简单的python游戏
- 26. 在所有操作系统上是否包含java可运行的C++代码?
- 27. 如何检查系统是否处于待机模式?
- 28. Kubernetes Docker操作系统参数vs主机操作系统参数
- 29. 我该如何要求操作系统在另一个内核上运行每个线程? - Linux操作系统
- 30. 我可以将本机操作系统安装作为虚拟机操作系统运行吗?
我只是从一个特定的键读取,所以从Wow6432Node节点读取的测试就足够了。谢谢! – vividos 2009-02-18 07:21:45