Informace o procesoru pomocí WMI

Postup:
Do projektu přidejte referenci na Microsoft WMI Scripting V1.1 Library. Na formulář přidejte 2 jmenovky (lblTitle, lblCPUlist), ListBox (lstCPU) a TextBox (txtCPU), kterému nastavte MultiLine na True. V deklarační části formuláře zapište:

Private asCpuPaths() As String
Private m_objCPUSet As SWbemObjectSet
Private m_objWMINameSpace As SWbemServices

Pak na událost FormLoad:
Private Sub Form_Load()

   Dim oCpu As SWbemObject
'WMI objekt, v tomto případě lokální CPU
   Dim sPath As String, sCaption As String

   Dim lElement As Long
   ReDim asCpuPaths(0) As String


   On Error GoTo ErrorHandler

   Set m_objWMINameSpace = GetObject("winmgmts:")
   lstCPU.Clear

   Set m_objCPUSet = m_objWMINameSpace.InstancesOf("Win32_Processor")
   sCaption = m_objCPUSet.Count & " procesor"
   If m_objCPUSet.Count <> 1 Then sCaption = sCaption & "y"
   sCaption = sCaption & " detekováno na tomto stroji."
   lblTitle.Caption = sCaption

   For Each oCpu In m_objCPUSet
      With oCpu
         sPath = .Path_ & ""
         If sPath <> "" Then
            lstCPU.AddItem .Name
            lElement = IIf(asCpuPaths(0) = "", 0, UBound(asCpuPaths) + 1)
            ReDim Preserve asCpuPaths(lElement) As String
            asCpuPaths(lElement) = sPath
         End If
      End With
   Next

   If lstCPU.ListCount <> 0 Then lstCPU.ListIndex = 0

CleanUp:
   Set oCpu = Nothing
   Exit Sub

ErrorHandler:
   MsgBox "Informace se nepodařilo zjistit. Nastala chyba: " &   _
      Err.Description, , "WMI Demo"
   GoTo CleanUp

End Sub

Na událost Click seznamu procesorů zapište:
Private Sub lstCPU_Click()

   Dim oCpu As SWbemObject
   Dim sInfoString As String
   On Error Resume Next

   Set oCpu = m_objCPUSet(asCpuPaths(lstCPU.ListIndex))
   With oCpu
      sInfoString = "Popis: " & .Description & vbCrLf
      sInfoString = sInfoString & "ID procesoru: " & .ProcessorID & vbCrLf
      sInfoString = sInfoString & "Stav " & .Status & vbCrLf
      sInfoString = sInfoString & "Výrobce: " & .Manufacturer & vbCrLf
      sInfoString = sInfoString & "Využití: " & .LoadPercentage & vbCrLf
      sInfoString = sInfoString & "Rychlost: " & .CurrentClockSpeed & "   MHz" _
          & vbCrLf
      sInfoString = sInfoString & "Maximální rychlost: " & .MaxClockSpeed _
          & vbCrLf
      sInfoString = sInfoString & "Velikost Level 2 Cache: " & .L2CacheSize _
          & vbCrLf
      sInfoString = sInfoString & "Rychlost Level 2 Cache: " & .L2CacheSpeed _
          & vbCrLf
      sInfoString = sInfoString & "Podpora řízení spotřeby: " _
          & .PowerManagementSupported
   End With

   txtCpu.Text = sInfoString

End Sub

Zpět

Autor: The Bozena