Visual Basic WMI System Services
Obtaining System Cache Memory Information using WMI
     
Posted:   Monday March 05, 2002
Updated:   Monday November 28, 2011
     
Applies to:   VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   Windows NT, 2000, XP. See Prerequisites below.
Author:   VBnet - Randy Birch
     

Related:  

Win32_BaseBoard: WMI Baseboard (Motherboard) Info
Win32_BIOS: WMI System BIOS Information

Win32_Processor: WMI Processor Information
Win32_CacheMemory: WMI System Cache Memory Info
Win32_TemperatureProbe: WMI Temperature Probe Info
     
 Prerequisites
Windows Script Host is built into Microsoft Windows 98, 2000, ME and XP. If you are running Windows 95 or NT4, you can download Windows Script Host from the Microsoft Windows Script Technologies Web site at http://msdn.microsoft.com/scripting/. Some information is not returned on non-NT-based systems.

A reference set in Projects / References to the Microsoft WMI Scripting Library.


The Win32_CacheMemory WMI class represents the represents the internal and external cache memory on a computer system.

This demo and illustration only show some of the available information from the class. For a complete listing see the table in the Comments section below. Note that some systems may not return information in all class properties.

 BAS Module Code
None.

 Form Code
To a form add a command button (Command1) and a listview (Listview1). Set a reference in Projects / References to the Microsoft WMI Scripting Library, and add the following to the form:

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.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'/* Below used for listview column auto-resizing
Private Const LVM_FIRST As Long = &H1000
Private Const LVM_SETCOLUMNWIDTH As Long = (LVM_FIRST + 30)
Private Const LVSCW_AUTOSIZE As Long = -1
Private Const LVSCW_AUTOSIZE_USEHEADER As Long = -2

Private Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
  (ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long


Private Sub Form_Load()

   With ListView1
      .ListItems.Clear
      .ColumnHeaders.Clear
      .ColumnHeaders.Add , , "Device ID"
      .ColumnHeaders.Add , , "Size"
      .ColumnHeaders.Add , , "Location"
      .ColumnHeaders.Add , , "Level"
      .ColumnHeaders.Add , , "Status"
      .ColumnHeaders.Add , , "Avail"
      .ColumnHeaders.Add , , "ECC Type"
      .View = lvwReport
      .Sorted = False
   End With
   
   Command1.Caption = "Cache Info"
   
End Sub


Private Sub Command1_Click()

   ListView1.ListItems.Clear
   Call wmiCacheMemory
   Call lvAutosizeControl(ListView1)
   
End Sub


Private Sub lvAutosizeControl(lv As ListView)

   Dim col2adjust As Long

  '/* Size each column based on the maximum of
  '/* EITHER the columnheader text width, or,
  '/* if the items below it are wider, the
  '/* widest list item in the column
   For col2adjust = 0 To lv.ColumnHeaders.Count - 1
   
      Call SendMessage(lv.hwnd, _
                       LVM_SETCOLUMNWIDTH, _
                       col2adjust, _
                       ByVal LVSCW_AUTOSIZE_USEHEADER)

   Next
   
End Sub


Private Sub wmiCacheMemory()

   Dim CacheMemorySet As SWbemObjectSet
   Dim cache As SWbemObject
   Dim msg As String
   Dim itmx As ListItem

   On Local Error Resume Next
   Set CacheMemorySet = GetObject("winmgmts:{impersonationLevel=impersonate}"). _
                                   InstancesOf("Win32_CacheMemory")
    
   For Each cache In CacheMemorySet
    
      Set itmx = ListView1.ListItems.Add(, , cache.DeviceID)
      
      itmx.SubItems(1) = cache.MaxCacheSize

      msg = CStr(cache.Location)
      Select Case cache.Location
         Case 0: msg = msg & " (internal)"
         Case 1: msg = msg & " (external)"
         Case 2: msg = msg & " (reserved)"
         Case 3: msg = msg & " (unknown)"
      End Select
      itmx.SubItems(2) = msg
      
      
      msg = CStr(cache.Level)
      Select Case cache.Level
         Case 1: msg = msg & " (other)"
         Case 2: msg = msg & " (unknown)"
         Case 3: msg = msg & " (primary)"
         Case 4: msg = msg & " (secondary)"
         Case 5: msg = msg & " (tertiary)"
      End Select
      itmx.SubItems(3) = msg
      
      itmx.SubItems(4) = cache.Status
      
      msg = CStr(cache.Availability)
      Select Case cache.Availability
         Case 1: msg = msg & " (other)"
         Case 2: msg = msg & " (unknown)"
         Case 3: msg = msg & " (on/full pwr)"
         Case 4: msg = msg & " (warning)"
         Case 5: msg = msg & " (in test)"
         Case 6: msg = msg & " (not applicable)"
         Case 7: msg = msg & " (pwr off)"
         Case 8: msg = msg & " (off line)"
         Case 9: msg = msg & " (off duty)"
         Case 10: msg = msg & " (degraded)"
         Case 11: msg = msg & " (not installed)"
         Case 12: msg = msg & " (Install Error)"
         Case 13: msg = msg & " (pwr save - unknown)"
         Case 14: msg = msg & " (pwr save - low pwr mode)"
         Case 15: msg = msg & " (pwr save - standby)"
         Case 16: msg = msg & " (pwr cycle)"
         Case 17: msg = msg & " (pwr save - warning)"
         Case 18: msg = msg & " (paused)"
         Case 19: msg = msg & " (not ready)"
         Case 20: msg = msg & " (not configured)"
         Case 21: msg = msg & " (quiesced)"

      End Select
      itmx.SubItems(5) = msg
         

      msg = CStr(cache.ErrorCorrectType)
      Select Case cache.ErrorCorrectType
         Case 0: msg = msg & " (reserved)"
         Case 1: msg = msg & " (other)"
         Case 2: msg = msg & " (unknown)"
         Case 3: msg = msg & " (none)"
         Case 4: msg = msg & " (parity)"
         Case 5: msg = msg & " (single-bit ECC)"
         Case 6: msg = msg & " (multi-bit ECC)"
      End Select
      itmx.SubItems(6) = msg
   
   Next

End Sub
 Comments
All information returned in the Win32_TemperatureProbe class (note that some systems may not return information in all class properties):
   
sint32 Accuracy
uint16 Availability
string Caption
uint32 ConfigManagerErrorCode
boolean ConfigManagerUserConfig
string CreationClassName
sint32 CurrentReading
string Description
string DeviceID
boolean ErrorCleared
string ErrorDescription
datetime InstallDate
boolean IsLinear
uint32 LastErrorCode
sint32 LowerThresholdCritical
sint32 LowerThresholdFatal
sint32 LowerThresholdNonCritical
sint32 MaxReadable
sint32 MinReadable
string Name
sint32 NominalReading
sint32 NormalMax
sint32 NormalMin
string PNPDeviceID
uint16 PowerManagementCapabilities[]
boolean PowerManagementSupported
uint32 Resolution
string Status
uint16 StatusInfo
string SystemCreationClassName
string SystemName
sint32 Tolerance
sint32 UpperThresholdCritical
sint32 UpperThresholdFatal
sint32 UpperThresholdNonCritical

 
 

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