check OS and processor is 32 bit or 64 bit?

2019-02-24 04:11发布

问题:

I want vb6 code to check OS is 32 bit or 64 bit and also processor is 32 bit or 64 bit.So please help me to get these codes. In vb.net i can use Environment.Is64BitOperatingSystem Property and it works only in .net 4.0 and above. But how can i get these information in vb6?

回答1:

For CPU you can use WMI and get AddressWidth. A sample of VBScript can be found here



回答2:

The most straightforward way to answer both questions seems to be using Win32_Processor WMI class.


Is operating system 32-bit or 64-bit?

For operating system one can check AddressWidth property:

AddressWidth

On a 32-bit operating system, the value is 32 and on a 64-bit operating system it is 64.

Relevant VB6 code is:

Public Function GetOsBitness() As String
    Dim ProcessorSet As Object
    Dim CPU As Object

    Set ProcessorSet = GetObject("Winmgmts:"). _
        ExecQuery("SELECT * FROM Win32_Processor")
    For Each CPU In ProcessorSet
        GetOsBitness = CStr(CPU.AddressWidth)
    Next
End Function

Is processor 32-bit or 64-bit?

For processor one can check DataWidth property:

DataWidth

On a 32-bit processor, the value is 32 and on a 64-bit processor it is 64.

Relevant VB6 code is:

Public Function GetCpuBitness() As String
    Dim ProcessorSet As Object
    Dim CPU As Object

    Set ProcessorSet = GetObject("Winmgmts:"). _
        ExecQuery("SELECT * FROM Win32_Processor")
    For Each CPU In ProcessorSet
        GetCpuBitness = CStr(CPU.DataWidth)
    Next
End Function


回答3:

You can try the DataWidth or AddressWidth properties of the Win32_Processor WMI class, On a 32-bit processor, the value is 32 and on a 64-bit processor it is 64.



回答4:

Operating system architecture

One way to obtain it is to use GetNativeSystemInfo WinAPI function. It is covered in linked question.

OS architecture can be obtained through WMI too, in case you'd like to achieve both goals in similar way. In Windows Vista and newer operating systems one can query Win32_OperatingSystem class and analyze OSArchitecture property (MSDN). Sadly, this property doesn't exist in Windows XP and earlier versions. On these systems one may query Win32_ComputerSystem class and analyze SystemType property instead (MSDN).

Public Function GetOsArchitecture()
    If IsAtLeastVista Then
        GetOsArchitecture = GetVistaOsArchitecture
    Else
        GetOsArchitecture = GetXpOsArchitecture
    End If
End Function

Private Function IsAtLeastVista() As Boolean
    IsAtLeastVista = GetOsVersion >= "6.0"
End Function

Private Function GetOsVersion() As String
    Dim OperatingSystemSet As Object
    Dim OS As Object

    Set OperatingSystemSet = GetObject("winmgmts:{impersonationLevel=impersonate}"). _
                                    InstancesOf("Win32_OperatingSystem")
    For Each OS In OperatingSystemSet
        GetOsVersion = Left$(Trim$(OS.Version), 3)
    Next
End Function

Private Function GetVistaOsArchitecture() As String
    Dim OperatingSystemSet As Object
    Dim OS As Object

    Set OperatingSystemSet = GetObject("Winmgmts:"). _
        ExecQuery("SELECT * FROM Win32_OperatingSystem")
    For Each OS In OperatingSystemSet
        GetVistaOsArchitecture = Left$(Trim$(OS.OSArchitecture), 2)
    Next
End Function

Private Function GetXpOsArchitecture() As String
    Dim ComputerSystemSet As Object
    Dim Computer As Object
    Dim SystemType As String

    Set ComputerSystemSet = GetObject("Winmgmts:"). _
        ExecQuery("SELECT * FROM Win32_ComputerSystem")
    For Each Computer In ComputerSystemSet
        SystemType = UCase$(Left$(Trim$(Computer.SystemType), 3))
    Next

    GetXpOsArchitecture = IIf(SystemType = "X86", "32", "64")
End Function


回答5:

On a Windows Xp 32b, the Win32_Processor.AddressWidth returns ALWAYS 32. See the post How does the MAP tool find out it's a 64-Bit machine?



标签: vb6 wmi