Visual Basic Disk/Drive API Routines

DeviceIoControl: Check Media Availability
     
Posted:   Thursday March 9, 2000
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   Windows NT4, Windows 2000, Windows XP
Author:   VBnet - Randy Birch
     

Related:  

DeviceIoControl: Load/Eject Removable Media
DeviceIoControl: Lock/Unlock Removable Media Devices
DeviceIoControl: Obtain Physical Drive Information
     
 Prerequisites
Windows NT, Windows 2000, Windows XP

Form and BAS module from DeviceIoControl: Lock/Unlock Removable Media Devices


This is the DeviceIoControl method for determining if a given drive has media present. The nice thing about this call is that it does not need to spin up the disk - the call returns instantly. The only lag might be if a new disk was inserted and the call was made while the OS was spinning up the drive. Requires the form and module specified above.
 BAS Module Code
Add the following code to the BAS module constructed in DeviceIoControl: Lock/Unlock Removable Media Devices:

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.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Const IOCTL_STORAGE_CHECK_VERIFY As Long = &H2D4800

Public Function DeviceCheckMedia(sDrive As String) As Long

   Dim hDevice As Long
   Dim bytesReturned As Long
   Dim success As Long
  
  'the drive letter has to be passed
  'without a trailing slash (ie 'G:')
   sDrive = UnQualifyPath(sDrive)
   
   hDevice = CreateFile("\\.\" & sDrive, _
                        GENERIC_READ, _
                        FILE_SHARE_READ Or FILE_SHARE_WRITE, _
                        ByVal 0&, _
                        OPEN_EXISTING, _
                        0&, 0&)
    
   If hDevice <> INVALID_HANDLE_VALUE Then

     'If the operation succeeds and
     'the device media are accessible,
     'DeviceIoControl returns a nonzero value
      success = DeviceIoControl(hDevice, _
                                IOCTL_STORAGE_CHECK_VERIFY, _
                                0&, _
                                0&, _
                                ByVal 0&, _
                                0&, _
                                bytesReturned, _
                                ByVal 0&)
   
   End If
   
   Call CloseHandle(hDevice)
   DeviceCheckMedia = success <> 0
      
End Function
 Form Code
To the form created in DeviceIoControl: Lock/Unlock Removable Media Devices, add an additional command button (Command3) and the following code:

Option Explicit

Private Sub Command3_Click()

   Dim sDrive As String
   Dim result As Boolean
   
   If List1.ListIndex > -1 Then
   
      sDrive = List1.List(List1.ListIndex)
   
      result = DeviceCheckMedia(sDrive)
   
      If result Then
         MsgBox sDrive & " is ready", vbInformation
      Else
         MsgBox sDrive & " is not ready", vbExclamation
      End If
   
   End If
   
End Sub
 Comments

 
 

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