2009-02-17 101 views

回答

3

我不确定你需要检查脚本是否在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不可用,在这种情况下,你可能会检查的解释要么CaptionVersion,以确定您的操作系统是否是64位。

您也可以使用类似this取决于您所需要的复杂程度。

+0

我只是从一个特定的键读取,所以从Wow6432Node节点读取的测试就足够了。谢谢! – vividos 2009-02-18 07:21:45

1

你没有提到你用什么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规定。

4

即使在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 
1

下面是基于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

0

这显示系统和工艺两种架构:

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"之一。

相关问题