如何使用VBScript确定我是运行32位还是64位Windows操作系统?

时间:2010-08-27 11:16:27

标签: windows vbscript 32bit-64bit

如何在VBScript中检测Windows操作系统的位数(32位与64位)?

我试过这种方法,但它不起作用;我猜(x86)导致了一些检查文件夹的问题..

还有其他选择吗?

progFiles="c:\program files" & "(" & "x86" & ")"

set fileSys=CreateObject("Scripting.FileSystemObject")

If fileSys.FolderExists(progFiles) Then    
   WScript.Echo "Folder Exists"    
End If

8 个答案:

答案 0 :(得分:19)

前几天在工作中遇到同样的问题。偶然发现这个天才的vbscript,并认为不分享是太好了。

Bits = GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth

来源:http://csi-windows.com/toolkit/csi-getosbits

答案 1 :(得分:14)

您可以查询PROCESSOR_ARCHITECTURE。描述here,您必须添加一些额外的检查,因为PROCESSOR_ARCHITECTURE的值对于任何32位进程都是x86,即使它在64位操作系统上运行。在这种情况下,变量PROCESSOR_ARCHITEW6432将包含OS位数。 MSDN中的详细信息。

Dim WshShell
Dim WshProcEnv
Dim system_architecture
Dim process_architecture

Set WshShell =  CreateObject("WScript.Shell")
Set WshProcEnv = WshShell.Environment("Process")

process_architecture= WshProcEnv("PROCESSOR_ARCHITECTURE") 

If process_architecture = "x86" Then    
    system_architecture= WshProcEnv("PROCESSOR_ARCHITEW6432")

    If system_architecture = ""  Then    
        system_architecture = "x86"
    End if    
Else    
    system_architecture = process_architecture    
End If

WScript.Echo "Running as a " & process_architecture & " process on a " _ 
    & system_architecture & " system."

答案 2 :(得分:4)

这是一对基于@Bruno非常简洁的答案的VBScript函数:

Function Is32BitOS()
    If GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth _
       = 32 Then
        Is32BitOS = True
    Else
        Is32BitOS = False
    End If
End Function

Function Is64BitOS()
    If GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth _
       = 64 Then
        Is64BitOS = True
    Else
        Is64BitOS = False
    End If
End Function

更新:根据@ Ekkehard.Horner的建议,这两个函数可以更简洁地编写using single-line syntax,如下所示:

Function Is32BitOS() : Is32BitOS = (GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth = 32) : End Function

Function Is64BitOS() : Is64BitOS = (GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth = 64) : End Function

(请注意,围绕GetObject(...) = 32条件的括号不是必需的,但我相信它们会增加运算符优先级的清晰度。另请注意,修订后的实现中使用的单行语法可避免使用If/Then构造!)

更新2:根据来自@ Ekkehard.Horner的额外反馈,有些人可能会发现这些进一步修订的实施提供了简洁性和增强的可读性:

Function Is32BitOS()
    Const Path = "winmgmts:root\cimv2:Win32_Processor='cpu0'"
    Is32BitOS = (GetObject(Path).AddressWidth = 32)
End Function

Function Is64BitOS()
    Const Path = "winmgmts:root\cimv2:Win32_Processor='cpu0'"
    Is64BitOS = (GetObject(Path).AddressWidth = 64)
End Function

答案 3 :(得分:4)

WMIC查询可能很慢。使用环境字符串:

Function GetOsBits()
   Set shell = CreateObject("WScript.Shell")
   If shell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%") = "AMD64" Then
      GetOsBits = 64
   Else
      GetOsBits = 32
   End If
End Function

答案 4 :(得分:3)

Bruno的补充答案:您可能想要检查操作系统而不是处理器本身,因为您可以在较新的CPU上安装较旧的操作系统:

strOSArch = GetObject("winmgmts:root\cimv2:Win32_OperatingSystem=@").OSArchitecture

返回字符串" 32位"或者" 64位"。

答案 5 :(得分:2)

确定CPU是32位还是64位很容易,但问的问题是如何确定操作系统是32位还是64位。运行64位Windows时,将定义 ProgramW6432 环境变量。

此:

CreateObject("WScript.Shell").Environment("PROCESS")("ProgramW6432") = ""

对于32位操作系统将返回true,对于64位操作系统将返回false,并且适用于所有版本的Windows,包括非常旧的Windows。

答案 6 :(得分:0)

您还可以检查文件夹C:\Windows\sysnative是否存在。此文件夹(或更好的别名)仅在32位进程中存在,请参见File System Redirector

Set fso = CreateObject("Scripting.FileSystemObject")
Set wshShell = CreateObject( "WScript.Shell" )

If fso.FolderExists(wshShell.ExpandEnvironmentStrings("%windir%") & "\sysnative" ) Then
    WScript.Echo "You are running in 32-Bit Mode"
Else
    WScript.Echo "You are running in 64-Bit Mode"
End if

答案 7 :(得分:0)

' performance should be good enough
' Example usage for console:
' CSript //NoLogo *ScriptName*.vbs
' If ErrorLevel 1 Echo.Win32
' VBScript:
On Error Resume Next
Const TargetWidth = 32
Set WMI = GetObject("winMgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set Query = WMI.ExecQuery("SELECT AddressWidth FROM Win32_Processor")
For Each Item in Query
  If Item.AddressWidth = TargetWidth Then
    WScript.Quit 1
  End If
Next
WScript.Quit 0
相关问题