Visual Basic WMI System Services
Obtaining Logical Disk Information using WMI
     
Posted:   Thursday March 07, 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_LogicalDisk: WMI Method to Invoke Chkdsk
Win32_DiskDrive: WMI Disk Drive Information
Win32_LogicalDisk: WMI Logical Disk Information
     
 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_LogicalDisk WMI class represents a data source that resolves to an actual local storage device on a Windows 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.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'/* 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 , , "Drv"
      .ColumnHeaders.Add , , "DrvType"
      .ColumnHeaders.Add , , "FileSys"
      .ColumnHeaders.Add , , "MediaType"
      .ColumnHeaders.Add , , "Free Space"
      .ColumnHeaders.Add , , "Volume"
      .ColumnHeaders.Add , , "SerialNo"
      .ColumnHeaders.Add , , "Description"
      .View = lvwReport
      .Sorted = False
   End With
     
   Command1.Caption = "Get Logical Disk Info"

End Sub


Private Sub Command1_Click()

   ListView1.ListItems.Clear
   Call wmiLogicalDiskInfo
   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 wmiLogicalDiskInfo()

   Dim LogicalDiskSet As SWbemObjectSet
   Dim lds As SWbemObject
   Dim itmx As ListItem
   Dim msg As String
   
  'must have error handler enabled, as all
  'disks do not return all information
   On Local Error Resume Next
   Set LogicalDiskSet = GetObject("winmgmts:{impersonationLevel=impersonate}"). _
                                  InstancesOf("Win32_LogicalDisk")
    
    
   For Each lds In LogicalDiskSet
   
      Set itmx = ListView1.ListItems.Add(, , lds.Caption)
      
      If Not IsNull(lds.DriveType) Then
      
         msg = CStr(lds.DriveType)
         Select Case lds.DriveType
            Case 1: msg = msg & " (unknown)"
            Case 2: msg = msg & " (removable)"
            Case 3: msg = msg & " (local)"
            Case 4: msg = msg & " (network)"
            Case 5: msg = msg & " (cd)"
            Case 6: msg = msg & " (ramdisk)"
         End Select
         itmx.SubItems(1) = msg
      
      End If
      
      If Not IsNull(lds.FileSystem) Then 
      	itmx.SubItems(2) = lds.FileSystem
      End If
      
      If Not IsNull(lds.MediaType) Then
        
         msg = CStr(lds.MediaType)
         Select Case lds.MediaType
            Case 0: msg = msg & " (format unknown)"
            Case 1: msg = msg & " (51/4 floppy 1.2mb)"
            Case 2: msg = msg & " (31/2 floppy 1.44mb)"
            Case 3: msg = msg & " (31/2 floppy 2.88mb)"
            Case 4: msg = msg & " (31/2 floppy 20.8mb)"
            Case 5: msg = msg & " (31/2 floppy 720kb)"
            Case 6: msg = msg & " (51/4 floppy 360kb)"
            Case 7: msg = msg & " (51/4 floppy 320kb)"
            Case 8: msg = msg & " (51/4 floppy 320kb)"
            Case 9: msg = msg & " (51/4 floppy 180kb)"
            Case 10: msg = msg & " (51/4 floppy 160kb)"
            Case 11: msg = msg & " (removable)"
            Case 12: msg = msg & " (fixed disk)"
            Case 13: msg = msg & " (31/2 floppy 120mb)"
            Case 14: msg = msg & " (31/2 floppy 640kb)"
            Case 15: msg = msg & " (51/4 floppy 640kb)"
            Case 16: msg = msg & " (51/4 floppy 720kb)"
            Case 17: msg = msg & " (31/2 floppy 1.2mb)"
            Case 18: msg = msg & " (31/2 floppy 1.23mb)"
            Case 19: msg = msg & " (51/4 floppy 1.23mb)"
            Case 20: msg = msg & " (31/2 floppy 128mb)"
            Case 22: msg = msg & " (8 floppy 256kb)"
         End Select
         itmx.SubItems(3) = msg
            
      End If
      
      If Not IsNull(lds.FreeSpace) Then 
      	itmx.SubItems(4) = FormatNumber(lds.FreeSpace, 0)
      End If
      
      If Not IsNull(lds.VolumeName) Then 
      	itmx.SubItems(5) = lds.VolumeName
      End If
      
      If Not IsNull(lds.VolumeSerialNumber) Then 
      	itmx.SubItems(6) = lds.VolumeSerialNumber
      End If
            	
      If Not IsNull(lds.Description) Then 
      	itmx.SubItems(7) = lds.Description
      End If      	
      
   Next

End Sub
 Comments
All information returned in the Win32_LogicalDisk class (note that some systems may not return information in all class properties):
   
uint16 Access
uint16 Availability
uint64 BlockSize
string Caption
boolean Compressed
uint32 ConfigManagerErrorCode
boolean ConfigManagerUserConfig
string CreationClassName
string Description
string DeviceID
uint32 DriveType
boolean ErrorCleared
string ErrorDescription
string ErrorMethodology
string FileSystem
uint64 FreeSpace
datetime InstallDate
uint32 LastErrorCode
uint32 MaximumComponentLength
uint32 MediaType
string Name
uint64 NumberOfBlocks
string PNPDeviceID
uint16 PowerManagementCapabilities[]
boolean PowerManagementSupported
string ProviderName
string Purpose
boolean QuotasDisabled
boolean QuotasIncomplete
boolean QuotasRebuilding
uint64 Size
string Status
uint16 StatusInfo
boolean SupportsDiskQuotas
boolean SupportsFileBasedCompression
string SystemCreationClassName
string SystemName
boolean VolumeDirty
string VolumeName
string VolumeSerialNumber

 
 

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