How can I use VBScript to determine whether I am running a 32-bit or 64-bit Windows OS?

前端 未结 8 540
死守一世寂寞
死守一世寂寞 2020-12-25 08:46

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

8条回答
  •  孤独总比滥情好
    2020-12-25 09:39

    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
    

提交回复
热议问题