Visual Basic System Services
GetSystemInfo: System Processor Information
     
Posted:   Sunday November 28, 1999
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4, Windows 2000, Windows XP Pro
OS restrictions:   None, but see prerequisites
Author:   VBnet - Randy Birch
     

Related:  

GetVersionEx: Windows Version Info (Wrapper Routines)
GetFileVersionInfo: Handy Routines for Identifying Shell32 Versions
GetVersionEx: Windows Version, Service Pack and Platform Info
     
 Prerequisites
None, however obtaining the CPU speed is only available on NT4, Windows 2000 or Windows XP or higher (unless the WinMgmts object is used).

GetSystemInfo provides access to processor and memory--specific system attributes. This page looks at the values returned pertaining to processors, and how to retrieve meaningful information from those values.  As an added bonus, because no API as yet provides access to processor speed information, on Windows NT4 or greater systems a registry routine can be used to extract the processor speed. 

The code is presented as a one-button routine; extending the functionality into wrappers for the various data elements is a simple process.

 BAS Module Code
None.

 Form Code
Add the following code to a form containing a single command button (Command1):

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Flags for GetSystemInfo
Private Const PROCESSOR_INTEL_386 As Long = 386
Private Const PROCESSOR_INTEL_486 As Long = 486
Private Const PROCESSOR_INTEL_PENTIUM As Long = 586
Private Const PROCESSOR_MIPS_R4000 As Long = 4000
Private Const PROCESSOR_ALPHA_21064 As Long = 21064
Private Const PROCESSOR_PPC_601 As Long = 601
Private Const PROCESSOR_PPC_603 As Long = 603
Private Const PROCESSOR_PPC_604 As Long = 604
Private Const PROCESSOR_PPC_620 As Long = 620
Private Const PROCESSOR_HITACHI_SH3 As Long = 10003    'Windows CE
Private Const PROCESSOR_HITACHI_SH3E As Long = 10004   'Windows CE
Private Const PROCESSOR_HITACHI_SH4 As Long = 10005    'Windows CE
Private Const PROCESSOR_MOTOROLA_821 As Long = 821     'Windows CE
Private Const PROCESSOR_SHx_SH3 As Long = 103          'Windows CE
Private Const PROCESSOR_SHx_SH4 As Long = 104          'Windows CE
Private Const PROCESSOR_STRONGARM As Long = 2577       'Windows CE - 0xA11
Private Const PROCESSOR_ARM720 As Long = 1824          'Windows CE - 0x720
Private Const PROCESSOR_ARM820 As Long = 2080          'Windows CE - 0x820
Private Const PROCESSOR_ARM920 As Long = 2336          'Windows CE - 0x920
Private Const PROCESSOR_ARM_7TDMI As Long = 70001      'Windows CE

Private Const PROCESSOR_ARCHITECTURE_INTEL As Long = 0
Private Const PROCESSOR_ARCHITECTURE_MIPS As Long = 1
Private Const PROCESSOR_ARCHITECTURE_ALPHA As Long = 2
Private Const PROCESSOR_ARCHITECTURE_PPC As Long = 3
Private Const PROCESSOR_ARCHITECTURE_SHX As Long = 4
Private Const PROCESSOR_ARCHITECTURE_ARM As Long = 5
Private Const PROCESSOR_ARCHITECTURE_IA64 As Long = 6
Private Const PROCESSOR_ARCHITECTURE_ALPHA64 As Long = 7
Private Const PROCESSOR_ARCHITECTURE_UNKNOWN   As Long = &HFFFF&

Private Const PROCESSOR_LEVEL_80386 As Long = 3
Private Const PROCESSOR_LEVEL_80486 As Long = 4
Private Const PROCESSOR_LEVEL_PENTIUM As Long = 5
Private Const PROCESSOR_LEVEL_PENTIUMII As Long = 6

Private Const sCPURegKey = "HARDWARE\DESCRIPTION\System\CentralProcessor\0"

Private Const HKEY_LOCAL_MACHINE As Long = &H80000002

Private Type SYSTEM_INFO
   dwOemID As Long
   dwPageSize As Long
   lpMinimumApplicationAddress As Long
   lpMaximumApplicationAddress As Long
   dwActiveProcessorMask As Long
   dwNumberOfProcessors As Long
   dwProcessorType As Long
   dwAllocationGranularity As Long
   wProcessorLevel As Integer
   wProcessorRevision As Integer
End Type

Private Declare Sub GetSystemInfo Lib "kernel32" _
   (lpSystemInfo As SYSTEM_INFO)

Private Declare Function RegCloseKey Lib "advapi32" _
   (ByVal hKey As Long) As Long

Private Declare Function RegOpenKey Lib "advapi32" _
   Alias "RegOpenKeyA" _
  (ByVal hKey As Long, _
   ByVal lpSubKey As String, _
   phkResult As Long) As Long

Private Declare Function RegQueryValueEx Lib "advapi32" _
   Alias "RegQueryValueExA" _
  (ByVal hKey As Long, _
   ByVal lpValueName As String, _
   ByVal lpReserved As Long, _
   lpType As Long, _
   lpData As Any, _
   lpcbData As Long) As Long
   

Private Sub Command1_Click()

   Dim SI As SYSTEM_INFO
   Dim tmp As String
   
   Call GetSystemInfo(SI)
   
   Print "Number Of Processors", SI.dwNumberOfProcessors
   
   Select Case SI.dwProcessorType
      Case PROCESSOR_INTEL_386: tmp = "386"
      Case PROCESSOR_INTEL_486: tmp = "486"
      Case PROCESSOR_INTEL_PENTIUM: tmp = "Pentium"
      Case PROCESSOR_MIPS_R4000: tmp = "MIPS 4000"
      Case PROCESSOR_ALPHA_21064: tmp = "Alpha"
   End Select

   Print "Processor Type", SI.dwProcessorType, tmp

   Select Case SI.wProcessorLevel
      Case PROCESSOR_LEVEL_80386: tmp = "Intel 80386"
      Case PROCESSOR_LEVEL_80486: tmp = "Intel 80486"
      Case PROCESSOR_LEVEL_PENTIUM: tmp = "Intel Pentium"
      Case PROCESSOR_LEVEL_PENTIUMII: tmp = "Intel Pentium Pro, II, III or 4"
   End Select
   
   Print "Processor Level", SI.wProcessorLevel, tmp
   
   Print "Processor Revision", SI.wProcessorRevision, _
         "Model "; HiByte(SI.wProcessorRevision) & _
         ", Stepping " & LoByte(SI.wProcessorRevision)

   Print "CPU Speed", , " " & GetCPUSpeed() & " MHz"

End Sub


Private Function GetCPUSpeed() As Long
   
   Dim hKey As Long
   Dim cpuSpeed As Long
    
  'Open CPU key
   Call RegOpenKey(HKEY_LOCAL_MACHINE, sCPURegKey, hKey)
                    
  'and retrieve the value
   Call RegQueryValueEx(hKey, "~MHz", 0, 0, cpuSpeed, 4)
   Call RegCloseKey(hKey)
    
   GetCPUSpeed = cpuSpeed

End Function


Public Function HiByte(ByVal wParam As Integer) As Byte 
   
  'note: VB4-32 users should declare this function As Integer
   HiByte = (wParam And &HFF00&) \ (&H100)

End Function


Public Function LoByte(ByVal wParam As Integer) As Byte

  'note: VB4-32 users should declare this function As Integer
   LoByte = wParam And &HFF&    

End Function
 Comments
Note that much of the data returned is specific to NT. The MSDN information for the SYSTEM_INFO data type explains the returned information in detail. In addition, although a practice not recommended by Microsoft, some of this information can be obtained through querying environment variables.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter