问题
How do i detect the bitness (32-bit vs. 64-bit) of the Windows OS in VBScript?
I tried this approach but it doesn't work; I guess the (x86) is causing some problem which checking for the folder..
Is there any other alternative?
progFiles="c:\program files" & "(" & "x86" & ")"
set fileSys=CreateObject("Scripting.FileSystemObject")
If fileSys.FolderExists(progFiles) Then
WScript.Echo "Folder Exists"
End If
回答1:
You can query the PROCESSOR_ARCHITECTURE
. A described here, you have to add some extra checks, because the value of PROCESSOR_ARCHITECTURE
will be x86
for any 32-bit process, even if it is running on a 64-bit OS. In that case, the variable PROCESSOR_ARCHITEW6432
will contain the OS bitness. Further details in 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:
Came up against this same problem at work the other day. Stumbled on this genius piece of vbscript and thought it was too good not to share.
Bits = GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth
Source: http://csi-windows.com/toolkit/csi-getosbits
回答3:
Here is a pair of VBScript functions based on the very concise answer by @Bruno:
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
UPDATE: Per the advice from @Ekkehard.Horner, these two functions can be written more succinctly using single-line syntax as follows:
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
(Note that the parentheses that surround the GetObject(...) = 32
condition are not necessary, but I believe they add clarity regarding operator precedence. Also note that the single-line syntax used in the revised implementations avoids the use of the If/Then
construct!)
UPDATE 2: Per the additional feedback from @Ekkehard.Horner, some may find that these further revised implementations offer both conciseness and enhanced readability:
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
回答4:
WMIC queries may be slow. Use the environment strings:
Function GetOsBits()
Set shell = CreateObject("WScript.Shell")
If shell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%") = "AMD64" Then
GetOsBits = 64
Else
GetOsBits = 32
End If
End Function
回答5:
Addendum to Bruno's answer: You may want to check the OS rather than the processor itself, since you could install an older OS on a newer CPU:
strOSArch = GetObject("winmgmts:root\cimv2:Win32_OperatingSystem=@").OSArchitecture
Returns string "32-bit" or "64-bit".
回答6:
Determining if the CPU is 32-bit or 64-bit is easy but the question asked is how to determine if the OS is 32-bit or 64-bit. When a 64-bit Windows is running, the ProgramW6432 environment variable is defined.
This:
CreateObject("WScript.Shell").Environment("PROCESS")("ProgramW6432") = ""
will return true for a 32-bit OS and false for a 64-bit OS and will work for all version of Windows including very old ones.
回答7:
You can also check if folder C:\Windows\sysnative
exist. This folder (or better alias) exist only in 32-Bit process, see 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
来源:https://stackoverflow.com/questions/3583604/how-can-i-use-vbscript-to-determine-whether-i-am-running-a-32-bit-or-64-bit-wind