VB Code:
  1. '***********************************************************
  2. 'NOTES:
  3.  
  4. 'YOU MUST HAVE WMI SDK INSTALLED.  YOU CAN GET IT AT
  5. 'http://msdn.microsoft.com/downloads/sdks/wmi/default.asp
  6. 'Remember to add it in Project References!
  7.  '***********************************************************************
  8.  
  9. Private asCpuPaths() As String
  10.  
  11. Private m_objCPUSet As SWbemObjectSet
  12.  
  13. Private m_objWMINameSpace As SWbemServices
  14.  
  15. Option Explicit
  16.  
  17. Private Sub cmdDone_Click()
  18.  
  19. Unload Me
  20.  
  21. End Sub
  22.  
  23. Private Sub Form_Load()
  24.  
  25.  
  26. Dim oCpu As SWbemObject 'WMI Object, in this case, local CPUs
  27. Dim sPath As String, sCaption As String
  28.  
  29. Dim lElement As Long
  30. ReDim asCpuPaths(0) As String
  31.  
  32.  
  33. On Error GoTo ErrorHandler
  34.  
  35. 'Get Default NameSpace, which will be the one for the local machine
  36. Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2
  37. Set m_objWMINameSpace = GetObject("winmgmts:")
  38. lstCPU.Clear
  39.  
  40.  
  41. 'Get CPU set
  42.  
  43. Set m_objCPUSet = m_objWMINameSpace.InstancesOf("Win32_Processor")
  44. sCaption = m_objCPUSet.Count & " processor"
  45. If m_objCPUSet.Count <> 1 Then sCaption = sCaption & "s"
  46. sCaption = sCaption & " detected on this machine"
  47. lblTitle.Caption = sCaption
  48. 'Populate list box with CPU names
  49.                
  50. For Each oCpu In m_objCPUSet
  51.      With oCpu
  52.         sPath = .Path_ & ""
  53.             If sPath <> "" Then
  54.                 lstCPU.AddItem .Name
  55.                 'save path to array, so on machines with multiple CPUs,
  56.                 'each can be identified and their info loaded into text box
  57.                
  58.                 lElement = IIf(asCpuPaths(0) = "", 0, UBound(asCpuPaths) + 1)
  59.                 ReDim Preserve asCpuPaths(lElement) As String
  60.                 asCpuPaths(lElement) = sPath
  61.             End If
  62.      End With
  63. Next
  64. If lstCPU.ListCount <> 0 Then lstCPU.ListIndex = 0
  65.      
  66.  
  67.  
  68.  
  69.  
  70. CleanUp:
  71. Set oCpu = Nothing
  72.  
  73. Exit Sub
  74.  
  75. ErrorHandler:
  76. MsgBox "CPU Information could not be displayed due to the following error: " & Err.Description, , "WMI Demo Failed"
  77. GoTo CleanUp
  78. End Sub
  79.  
  80. Private Sub Form_Unload(Cancel As Integer)
  81. Set m_objCPUSet = Nothing
  82. Set m_objWMINameSpace = Nothing
  83. End Sub
  84.  
  85. Private Sub lstCPU_Click()
  86. Dim oCpu As SWbemObject
  87. 'Refer to SDK documentation for more detail about each of these properties
  88. Dim sInfoString As String
  89. On Error Resume Next
  90. Set oCpu = m_objCPUSet(asCpuPaths(lstCPU.ListIndex))
  91. With oCpu
  92.     sInfoString = "Description: " & .Description & vbCrLf
  93.     sInfoString = sInfoString & "Processor ID: " & .ProcessorID & vbCrLf
  94.     sInfoString = sInfoString & "Status: " & .Status & vbCrLf
  95.     sInfoString = sInfoString & "Manufacturer: " & .Manufacturer & vbCrLf
  96.     sInfoString = sInfoString & "Availability: " & AvailabilityToString(.Availability) & vbCrLf
  97.     sInfoString = sInfoString & "Load Percentage: " & .LoadPercentage & vbCrLf
  98.     sInfoString = sInfoString & "Current Clock Speed: " & .CurrentClockSpeed & " MHz" & vbCrLf
  99.     sInfoString = sInfoString & "Maximum Clock Speed: " & .MaxClockSpeed & vbCrLf
  100.     sInfoString = sInfoString & "Level 2 Cache Size: " & .L2CacheSize & vbCrLf
  101.     sInfoString = sInfoString & "Level 2 Cache Speed: " & .L2CacheSpeed & vbCrLf
  102.     sInfoString = sInfoString & "Power Management Supported: " & .PowerManagementSupported
  103. End With
  104. txtCpu.Text = sInfoString
  105.  
  106. End Sub
  107. 'Conversions from code to string were developed
  108. 'based on information in WMI SDK documentation
  109. Private Function AvailabilityToString(Code As Integer) As String
  110. Dim sAns As String
  111.  
  112. Select Case Code
  113.     Case 1, 2
  114.         sAns = "Unknown"
  115.     Case 3
  116.         sAns = "Running/Full Power"
  117.     Case 4
  118.         sAns = "Warning"
  119.     Case 5
  120.         sAns = "In Test"
  121.     Case 6
  122.         sAns = "Not Applicable"
  123.     Case 7
  124.         sAns = "Power Off"
  125.     Case 8
  126.         sAns = "Off Line"
  127.     Case 9
  128.         sAns = "Off Duty"
  129.     Case 10
  130.         sAns = "Degraded"
  131.     Case 11
  132.         sAns = "Not Installed"
  133.     Case 12
  134.         sAns = "Install Error"
  135.     Case 13
  136.         sAns = "Power Save - Unknown"
  137.     Case 14
  138.         sAns = "Power Save - Low Power Mode"
  139.     Case 15
  140.         sAns = "Power Save - Standby"
  141.     Case 16
  142.         sAns = "Power Cycle"
  143.     Case 17
  144.         sAns = "Power Save - Warning"
  145.     Case Else
  146.         sAns = "Unknown"
  147. End Select
  148.  
  149. AvailabilityToString = sAns
  150.  
  151. End Function