Visual Basic Disk/Drive API Routines

DeviceIoControl: Load/Eject Removable Media
     
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: Check Media Availability
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 page builds on the demo listed above, to add a DeviceIoControl method to eject or load disks from removable devices.
 BAS Module Code
Add the following additional 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_EJECT_MEDIA As Long = &H2D4808
Public Const IOCTL_STORAGE_LOAD_MEDIA As Long = &H2D480C

Public Function DeviceSetMedia(sDrive As String, ctrlCode As Long) As Boolean

   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)
   
  'obtain a handle to the device
   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,
     'DeviceIoControl returns zero
      success = DeviceIoControl(hDevice, _
                                ctrlCode, _
                                0&, _
                                0&, _
                                ByVal 0&, _
                                0&, _
                                bytesReturned, _
                                ByVal 0&)

   End If
   
   Call CloseHandle(hDevice)
   DeviceSetMedia = success <> 0

End Function
 Form Code
To the form created in DeviceIoControl: Lock/Unlock Removable Media Devices, add two additional command buttons in a control array (Command2(0), Command2(1)) along with the following additional code:

Option Explicit

Private Sub Command2_Click(Index As Integer)

   Dim sDrive As String

   If List1.ListIndex > -1 Then
   
      sDrive = List1.List(List1.ListIndex)

      Select Case Index
         Case 0
            Call DeviceSetMedia(sDrive, IOCTL_STORAGE_EJECT_MEDIA)
         Case 1
            Call DeviceSetMedia(sDrive, IOCTL_STORAGE_LOAD_MEDIA)
      End Select
   
   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