How to detect the CPU and OS Architecture

image_pdfimage_print

The client I am working for now, has both XP and Windows 7. XP is 32 bits and Windows 7 is 64 bits. To avoid that I have to make the packages twice, I make both 32 and 64 bits versions. Depending on the CPU architecture the MSI is chosen.

So I made the following code:

1
2
3
4
5
6
7
8
9
10
11
Dim objWMIService   : Set objWMIService  = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select Architecture from Win32_Processor")
For Each objItem in colItems
    if objItem.Architecture = 0 then 
       strArchitecture = "x86"
    end if
    if objItem.Architecture = 9 then 
       strArchitecture = "x64"
    end if
next
msgbox "The detected CPU Architecture is " & strArchitecture & "."

On XP it returns x86 on a 32 bits version, but on Windows 7 x32 it returns… x64. I suppose this is due to the fact that all the current processors are 64 bits.

To work around I had to think differently. For example: on a computer with a 64 bits OS, you have – for example – a %Windir%\SysWOW64 folder. And that one is not on 32 bits OS.

1
2
3
4
5
6
7
8
9
Dim objShell  : Set objShell = CreateObject("Wscript.Shell")
Dim objFSO    : Set objFSO   = WScript.CreateObject("Scripting.FileSystemObject")
Dim strWindir : strWinDir    = objShell.ExpandEnvironmentStrings("%WinDir%")
if objFSO.FolderExists(strWindir & "\syswow64") Then
   strArchitecture = "x64"
    else
   strArchitecture = "x86"
end if
msgbox "The detected OS Architecture is " & strArchitecture & "."

The examples have been included in this script that shows the different ways to detect both the CPU and OS Architecture.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
' ================================================================================================
' Sevaral ways to detect the:
'  - CPU Architecture
'  - OS Architecture
' Created by Willem-Jan Vroom
' 
' 0.0.1
'    Initial version
'
' ================================================================================================
 
' ------------------------------------------------------------------------------------------------
' Declare the most variables. 
' ------------------------------------------------------------------------------------------------
 
  Option Explicit
 
  Dim objFSO        : Set objFSO        = WScript.CreateObject("Scripting.FileSystemObject")
  Dim objShell      : Set objShell      = CreateObject("Wscript.Shell")
  Dim objReg        : Set objReg        = GetObject("winmgmts:\\.\root\default:StdRegProv")
  Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  Dim strWindir     : strWinDir         = objShell.ExpandEnvironmentStrings("%WinDir%")
  Dim strOS         : strOS             = ""
  Dim strSummary    : strSummary        = "Details about this Operating System" & vbcrlf
 
  Dim colItems
  Dim objItem
 
  Dim strCPUArchitecture_WMI
  Dim strCPUArchitecture_Registry
  Dim strOSArchitecture_WMI
  Dim strOSArchitecture_Windir
 
  const HKEY_LOCAL_MACHINE    = &H80000002
 
' ------------------------------------------------------------------------------------------------
' Detect the current OS.  
' ------------------------------------------------------------------------------------------------
 
  Set colItems = objWMIService.ExecQuery("Select Caption from Win32_OperatingSystem")
  For Each objItem in colItems
      strOS            = objItem.Caption
  next
 
' ------------------------------------------------------------------------------------------------
' Method 1:
'   - Find the CPU Architecture via WMI
' ------------------------------------------------------------------------------------------------
 
  Set colItems = objWMIService.ExecQuery("Select Architecture from Win32_Processor")
  For Each objItem in colItems
      if objItem.Architecture = 0 then 
         strCPUArchitecture_WMI = "x86"
      end if
      if objItem.Architecture = 9 then 
         strCPUArchitecture_WMI = "x64"
      end if
  next
 
' ------------------------------------------------------------------------------------------------
' Method 2:
'   - Find the CPU Architecture via the registry
'     It can be found in:
'     HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0
'      Hive: Identifier
'      The value might be something like 'x86 Family 6 Model 42 Stepping 7' or 
'      'Intel64 Family 6 Model 42 Stepping 7'.
' ------------------------------------------------------------------------------------------------
 
  objReg.GetStringValue HKEY_LOCAL_MACHINE, "HARDWARE\DESCRIPTION\System\CentralProcessor\0","Identifier",strCPUArchitecture_Registry
 
  if instr(strCPUArchitecture_Registry,"86") > 0 Then 
     strCPUArchitecture_Registry = "x86"
       else
     strCPUArchitecture_Registry = "x64"
  end if
 
' ------------------------------------------------------------------------------------------------
' Method 3:
'   - Find the OS Architecture via WMI
'     Accoring to http://msdn.microsoft.com/en-us/library/windows/desktop/aa394239(v=vs.85).aspx
'     this does not work on XP and below.
'   To avoid an error message on XP an 'on error resume next' has been included.
' ------------------------------------------------------------------------------------------------
 
  On Error Resume Next
 
  strOSArchitecture_WMI = ""
  Set colItems = objWMIService.ExecQuery("Select OSArchitecture from Win32_OperatingSystem")
  For Each objItem in colItems
      strOSArchitecture_WMI = objItem.OSArchitecture
  Next
 
  On Error Goto 0
 
  if strOSArchitecture_WMI = "" Then
     strOSArchitecture_WMI = "Could not be detected."
  End if
 
' ------------------------------------------------------------------------------------------------
' Method 4:
'   - Find the OS Architecture to check if C:\Windows\SysWOW64 exists.
' ------------------------------------------------------------------------------------------------
 
  if objFSO.FolderExists(strWindir & "\syswow64") Then
     strOSArchitecture_Windir = "x64"
      else
     strOSArchitecture_Windir = "x86"
  end if
 
' ------------------------------------------------------------------------------------------------
' Display the results on the screen.
' ------------------------------------------------------------------------------------------------
 
  strSummary = strSummary & "Operating system:                   " & strOS & vbCrLf
  strSummary = strSummary & "CPU Architecture via WMI:           " & strCPUArchitecture_WMI & vbCrLf
  strSummary = strSummary & "CPU Architecture via the registry:  " & strCPUArchitecture_Registry & vbCrlf
  strSummary = strSummary & "OS Architecture via WMI:            " & strOSArchitecture_WMI & vbcrlf
  strSummary = strSummary & "OS Architecture via SysWOW64 check: " & strOSArchitecture_Windir & vbcrlf
 
  wscript.echo strSummary

Some output examples:

  • On XP x86:
    Screenrpint Windows XP x86

    Screenrpint Windows XP x86

  • On Windows 8 x86:
    Screenrpint Windows 8 x86

    Screenrpint Windows 8 x86

  • On Windows 2008 Server x64:
    Screenprint Windows 2008 Server x64

    Screenprint Windows 2008 Server x64

Permanent link to this article: https://www.vroom.cc/wordpress/how-to-detect-the-cpu-and-os-architecture/