Visual Basic WMI System Services
Obtaining Disk Drive Information using WMI
     
Posted:   Monday March 25, 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

DeviceIoControl: Obtain Physical Drive 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_DiskDrive WMI class represents a physical disk drive as seen by a computer running the Windows operating system.

Any interface to a Windows physical disk drive is a descendent (or member) of this class. The features of the disk drive seen through this object correspond to the logical and management characteristics of the drive. In some cases, this may not reflect the actual physical characteristics of the device. Any object based on another logical device would not be a member of this class.

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
      .View = lvwReport
      .Sorted = False
   End With
   
   Command1.Caption = "Disk Info"

End Sub


Private Sub Command1_Click()

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

   Dim DiskDriveSet As SWbemObjectSet
   Dim dd As SWbemObject
   Dim thiscol As Long
   Dim capcount As Long
   Dim msg As String
   Dim sflag As String  'used in err trap
   Dim itmx As ListItem

   On Local Error GoTo diskinfo_error
   
  'add a first column, and set lv initial parameters
   With ListView1
      .ListItems.Clear
      .View = lvwReport
      .Sorted = False
      .ColumnHeaders.Clear
      .ColumnHeaders.Add , , "WMI Property"
      
      'add class properties to column 1
      'identification info
      .ListItems.Add , , "ID: Description"
      .ListItems.Add , , "ID: Index"
      .ListItems.Add , , "ID: DeviceID"
      .ListItems.Add , , "ID: Caption"
      .ListItems.Add , , "ID: Manufacturer"
      .ListItems.Add , , "ID: Model"
      .ListItems.Add , , "ID: InterfaceType"
      .ListItems.Add , , "ID: MediaLoaded"
      .ListItems.Add , , "ID: MediaType"
      
      'physical info
      .ListItems.Add , , "Phyical: Status"
      .ListItems.Add , , "Phyical: Size"
      .ListItems.Add , , "Phyical: Partitions"
      .ListItems.Add , , "Phyical: BytesPerSector"
      .ListItems.Add , , "Phyical: SectorsPerTrack"
      .ListItems.Add , , "Phyical: TotalCylinders"
      .ListItems.Add , , "Phyical: TotalHeads"
      .ListItems.Add , , "Phyical: TotalTracks"
      .ListItems.Add , , "Phyical: TracksPerCylinder"
      
      'capabilities of the device
      .ListItems.Add , , "Disk Capabilities:"
   End With

   Set DiskDriveSet = GetObject("winmgmts:{impersonationLevel=impersonate}"). _
                                           InstancesOf("Win32_DiskDrive")
       
  'for each drive, fill in respective columns
   For Each dd In DiskDriveSet
   
      With ListView1
         .ColumnHeaders.Add , , dd.Description & " " & dd.Index
         capcount = 0
         thiscol = (.ColumnHeaders.Count - 1)
         
         'add identification info
         .ListItems(1).SubItems(thiscol) = dd.Description
         .ListItems(2).SubItems(thiscol) = dd.Index
         .ListItems(3).SubItems(thiscol) = dd.DeviceID
         .ListItems(4).SubItems(thiscol) = dd.Caption
         .ListItems(5).SubItems(thiscol) = dd.Manufacturer
         .ListItems(6).SubItems(thiscol) = dd.Model
         .ListItems(7).SubItems(thiscol) = dd.InterfaceType
         .ListItems(8).SubItems(thiscol) = dd.MediaLoaded
         .ListItems(9).SubItems(thiscol) = dd.MediaType

         'add physical info
         .ListItems(10).SubItems(thiscol) = dd.Status
         .ListItems(11).SubItems(thiscol) = FormatNumber(dd.Size, 0)
         .ListItems(12).SubItems(thiscol) = dd.Partitions
         .ListItems(13).SubItems(thiscol) = FormatNumber(dd.BytesPerSector, 0)
         .ListItems(14).SubItems(thiscol) = FormatNumber(dd.SectorsPerTrack, 0)
         .ListItems(15).SubItems(thiscol) = FormatNumber(dd.TotalCylinders, 0)
         
         'note: the value for the TotalHeads property
         'is obtained through extended functions of
         'BIOS interrupt 13h. The value may be inaccurate
         'if the drive uses a translation scheme to
         'support high capacity disk sizes. Consult
         'the manufacturer for accurate drive
         'specifications
         .ListItems(16).SubItems(thiscol) = FormatNumber(dd.TotalHeads, 0)
         .ListItems(17).SubItems(thiscol) = FormatNumber(dd.TotalTracks, 0)
         .ListItems(18).SubItems(thiscol) = FormatNumber(dd.TracksPerCylinder, 0)
      
        'capabilities of the device
         
        'because different drives may have different
        'capabilites, the routine is coded such that
        'when an insertion is attempted against a
        'non-existant subitem index, the error trap
        'will add the additional row. The sflag
        'just assists in identifying the error
        'occured because of the capactities loop.
         sflag = "caploop"
         For capcount = LBound(dd.capabilities) To UBound(dd.capabilities)

            Select Case dd.capabilities(capcount)
               Case 0: msg = "Unknown "
               Case 1: msg = "Other "
               Case 2: msg = "Sequential Access "
               Case 3: msg = "Random Access "
               Case 4: msg = "Supports Writing "
               Case 5: msg = "Encryption "
               Case 6: msg = "Compression "
               Case 7: msg = "Supports Removable Media "
               Case 8: msg = "Manual Cleaning "
               Case 9: msg = "Automatic Cleaning "
               Case 10: msg = "SMART Notification "
               Case 11: msg = "Supports Dual Sided Media "
               Case 12: msg = "Ejection Prior to Drive Dismount Not Required"
            
            End Select
            
            .ListItems(19 + capcount).SubItems(thiscol) = msg
        
         Next
         sflag = ""
      
      End With
      
   Next
   
diskinfo_exit:

   On Local Error GoTo 0
   Exit Sub
   
diskinfo_error:

  'if "index out of bounds" error
  'and error occurred as result of
  'adding drive capabilities, add 
  'a new blank listitem and resume
   If Err.Number = 35600 And sflag = "caploop" Then
      ListView1.ListItems.Add 19 + capcount, , ""
      Resume
   Else
      Resume Next
   End If
   
End Sub
 Comments
All information returned in the Win32_LogicalDisk class (note that some systems may not return information in all class properties):
   
uint16 Availability
uint32 BytesPerSector
uint16 Capabilities[]
string CapabilityDescriptions[]
string Caption
string CompressionMethod
uint32 ConfigManagerErrorCode
boolean ConfigManagerUserConfig
string CreationClassName
uint64 DefaultBlockSize
string Description
string DeviceID
boolean ErrorCleared
string ErrorDescription
string ErrorMethodology
uint32 Index
datetime InstallDate
string InterfaceType
uint32 LastErrorCode
string Manufacturer
uint64 MaxBlockSize
uint64 MaxMediaSize
boolean MediaLoaded
string MediaType
uint64 MinBlockSize
string Model
string Name
boolean NeedsCleaning
uint32 NumberOfMediaSupported
uint32 Partitions
string PNPDeviceID
uint16 PowerManagementCapabilities[]
boolean PowerManagementSupported
uint32 SCSIBus
uint16 SCSILogicalUnit
uint16 SCSIPort
uint16 SCSITargetId
uint32 SectorsPerTrack
uint32 Signature
uint64 Size
string Status
uint16 StatusInfo
string SystemCreationClassName
string SystemName
uint64 TotalCylinders
uint32 TotalHeads
uint64 TotalSectors
uint64 TotalTracks
uint32 TracksPerCylinder

 
 

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