如何使用 VBScript 确定我运行的是 32 位还是 64 位 Windows 操作系统?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3583604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use VBScript to determine whether I am running a 32-bit or 64-bit Windows OS?
提问by Santhosh
How do i detect the bitness (32-bit vs. 64-bit) of the Windows OS in VBScript?
我如何在 VBScript 中检测 Windows 操作系统的位数(32 位与 64 位)?
I tried this approach but it doesn't work; I guess the (x86) is causing some problem which checking for the folder..
我试过这种方法,但它不起作用;我猜(x86)导致了一些检查文件夹的问题..
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
回答by Bruno
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.
前几天在工作中遇到了同样的问题。偶然发现了这个天才的 vbscript 并认为它太好了,不能分享。
Bits = GetObject("winmgmts:root\cimv2:Win32_Processor='cpu0'").AddressWidth
回答by Dirk Vollmar
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.
您可以查询PROCESSOR_ARCHITECTURE
. 此处描述的,您必须添加一些额外的检查,因为 的值PROCESSOR_ARCHITECTURE
将x86
适用于任何 32 位进程,即使它在 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."
回答by DavidRR
Here is a pair of VBScript functions based on the very concise answer by @Bruno:
这是基于@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
UPDATE:Per the advice from @Ekkehard.Horner, these two functions can be written more succinctly using single-line syntaxas follows:
更新:根据@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
(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!)
(请注意,GetObject(...) = 32
条件周围的括号不是必需的,但我相信它们增加了运算符优先级的清晰度。另请注意,修订后的实现中使用的单行语法避免使用If/Then
构造!)
UPDATE 2:Per the additional feedback from @Ekkehard.Horner, some may find that these further revised implementations offer both conciseness and enhanced readability:
更新 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
回答by Mustafa Burak Kalkan
WMIC queries may be slow. Use the environment strings:
WMIC 查询可能很慢。使用环境字符串:
Function GetOsBits()
Set shell = CreateObject("WScript.Shell")
If shell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%") = "AMD64" Then
GetOsBits = 64
Else
GetOsBits = 32
End If
End Function
回答by amonroejj
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:
布鲁诺回答的附录:您可能想要检查操作系统而不是处理器本身,因为您可以在较新的 CPU 上安装较旧的操作系统:
strOSArch = GetObject("winmgmts:root\cimv2:Win32_OperatingSystem=@").OSArchitecture
Returns string "32-bit" or "64-bit".
返回字符串“32 位”或“64 位”。
回答by Regis Desrosiers
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 ProgramW6432environment variable is defined.
确定 CPU 是 32 位还是 64 位很容易,但问题是如何确定操作系统是 32 位还是 64 位。当运行 64 位 Windows 时,将定义ProgramW6432环境变量。
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.
对于 32 位操作系统将返回 true,对于 64 位操作系统返回 false,并且适用于所有版本的 Windows,包括非常旧的 Windows。
回答by Wernfried Domscheit
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
您还可以检查文件夹是否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
Note: this script shows whether your current processis running in 32-bit or 64-bit mode - it does not show the architecture of your Windows.
注意:此脚本显示您当前的进程是在 32 位还是 64 位模式下运行 - 它不显示您的 Windows 架构。