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

问题描述 投票:0回答:10

如何在 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
windows vbscript 32bit-64bit
10个回答
23
投票

前几天在工作中也遇到了同样的问题。偶然发现了这个天才的 vbscript 片段,觉得它太好了,不能不分享。

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

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


16
投票

您可以查询

PROCESSOR_ARCHITECTURE
。如here所述,您必须添加一些额外的检查,因为对于任何 32 位进程,
PROCESSOR_ARCHITECTURE
的值都将是
x86
,即使它运行在 64 位操作系统上。在这种情况下,变量
PROCESSOR_ARCHITEW6432
将包含操作系统位数。更多详细信息请参见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."

6
投票

这是一对基于 @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的建议,这两个函数可以更简洁地编写使用单行语法,如下所示:

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

5
投票

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
投票

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

这个:

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

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


3
投票

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

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

返回字符串“32位”或“64位”。


2
投票

您还可以检查文件夹

C:\Windows\sysnative
是否存在。该文件夹(或更好的别名)仅存在于 32 位进程中,请参阅文件系统重定向器

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

注意:此脚本显示您当前的进程是在 32 位还是 64 位模式下运行 - 它不显示您的 Windows 体系结构。


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

0
投票

使用环境。在XP中测试过,但找不到32位CPU来测试...

   function getbitsos()
      with WScript.CreateObject("WScript.Shell").environment("PROCESS")
        if .item("PROCESSOR_ARCHITECTURE") ="X86" and .item("PROCESSOR_ARCHITEW6432") =vbnullstring Then
          getbitsos=array(32,32,32)
        elseif .item("PROGRAMFILES(x86)")=vbnullstring Then 
          getbitsos=array(64,32,32)
        elseif .item("PROGRAMFILES(x86)")=.item("PROGRAMFILES") Then
          getbitsos=array(64,64,32)
        Else
          getbitsos=array(64,64,64)
        end if   
      end with
    end function
    
    a=getbitsos()
    wscript.echo "Processor " &a(0) & vbcrlf & "OS "  & a(1) &vbcrlf& "Process " & a(2)& vbcrlf 

0
投票

这与 Microsoft 博客中提出的解决方案相同https://learn.microsoft.com/en-us/archive/blogs/david.wang/howto-detect-process-bitness

在 XP 32 和 win7 64 中测试(使用 32 位和 64 位 CMD)

Set objShell = CreateObject("WScript.Shell")
os_bit = 64
arch = objShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%")
archW6432 = objShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITEW6432%")
If LCase(arch) = "x86" Then
    If archW6432 = "" Or LCase(archW6432) = "%processor_architew6432%" Then
        os_bit = 32
    End If
End If

WScript.Echo "Operating System Bit: " & os_bit
© www.soinside.com 2019 - 2024. All rights reserved.