Results 1 to 3 of 3

Thread: VB Snippet : Processor Info

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    VB Snippet : Processor Info

    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
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    James Stanich,

    Just what I needed!!

    Thanks

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    VB Code:
    1. Private Sub Command1_Click()
    2. '========================
    3. Dim WMI
    4. Dim wmiWin32Objects
    5. Dim wmiWin32Object
    6. Dim ComputerName As String
    7.  
    8.     ComputerName = Environ("computername") 'make sure this is YOUR computers name!!!!
    9.     Set WMI = GetObject("WinMgmts://" & ComputerName)
    10.  
    11.     Set wmiWin32Objects = WMI.InstancesOf("Win32_Processor")
    12.     With wmiWin32Object
    13.         For Each wmiWin32Object In wmiWin32Objects
    14.             Text1(0).Text = .cpustatus
    15.             Text1(1).Text = .currentclockspeed
    16.             Text1(2).Text = .datawidth
    17.             Text1(3).Text = .Description
    18.             Text1(4).Text = .extclock
    19.             Text1(5).Text = .l2cachesize
    20.             Text1(6).Text = .l2cachespeed
    21.             Text1(7).Text = .Manufacturer
    22.             Text1(8).Text = .maxclockspeed
    23.             Text1(9).Text = .Name
    24.             Text1(10).Text = .processortype
    25.             Text1(11).Text = .Revision
    26.             Text1(12).Text = .role
    27.             Text1(13).Text = .socketdesignation
    28.             Text1(14).Text = .Status
    29.         Next
    30.     End With
    31.  
    32. End Sub


    Has someone helped you? Then you can Rate their helpful post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width