Here is an example I wrote that should eject a removable device just like explorer or disk managment. This is an interesting question and I want to know myself if there is any difference between them. Give this a try and keep me updated! :)
Add code to a (module.bas)
Usage: SafeEject "e:\"
Option Explicit
' safe eject media vb6.
' erik litze
Private Type PREVENT_MEDIA_REMOVAL
PreventMediaRemoval As Long
End Type
Private Const INVALID_HANDLE_VALUE As Long = (-1)
Private Const FSCTL_LOCK_VOLUME As Long = &H90018
Private Const FSCTL_UNLOCK_VOLUME As Long = &H9001C
Private Const FSCTL_DISMOUNT_VOLUME As Long = &H90020
Private Const IOCTL_STORAGE_MEDIA_REMOVAL As Long = &H2D4804
Private Const IOCTL_STORAGE_EJECT_MEDIA As Long = &H2D4808
Private Const OPEN_EXISTING As Long = 3
Private Const GENERIC_READ As Long = &H80000000
Private Const GENERIC_WRITE As Long = &H40000000
Private Const FILE_SHARE_READ As Long = &H1
Private Const FILE_SHARE_WRITE As Long = &H2
Private Const DRIVE_REMOVABLE As Long = 2
Private Const DRIVE_CDROM As Long = 5
Private Declare Function DeviceIoControl Lib "kernel32.dll" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, ByVal lpInBuffer As Long, ByVal nInBufferSize As Long, ByVal lpOutBuffer As Long, ByVal nOutBufferSize As Long, lpBytesReturned As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function CreateFileW Lib "kernel32.dll" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function GetDriveTypeW Lib "kernel32.dll" (ByVal nDrive As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Sub SafeEject(ByVal szDrive As String)
Dim udtPMR As PREVENT_MEDIA_REMOVAL
Dim dwAccess As Long
Dim dwDriveType As Long
Dim hDrive As Long
Dim cb As Long
' Check if trailing backslash exists.
If InStr(szDrive, "\") = 0 Then
Debug.Print "Please specify a full drive letter. ex. X:\"
Exit Sub
End If
' Backslash is required for this call.
dwDriveType = GetDriveTypeW(StrPtr(szDrive))
' Check if this type of drive is removable.
Select Case dwDriveType
Case DRIVE_REMOVABLE
dwAccess = GENERIC_READ Or GENERIC_WRITE
Case DRIVE_CDROM
dwAccess = GENERIC_READ
Case Else
Debug.Print "This type of volume is not a removable device."
Exit Sub
End Select
' Remove backslash.
szDrive = Replace$(szDrive, "\", vbNullString)
' This must not have a backslash.(open volume for direct access might need admin rights on vista/win7)
hDrive = CreateFileW(StrPtr("\\.\" & szDrive), dwAccess, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)
' checksum
If hDrive = INVALID_HANDLE_VALUE Then
Debug.Print "Bad handle. (try:run as admin)"
Exit Sub
End If
' Attempt to lock the volume for safe ejection.
If DeviceIoControl(hDrive, FSCTL_LOCK_VOLUME, 0, 0, 0, 0, cb, 0) Then
' DISMOUNT the volume and flush cache and any associations on the systems.
Call DeviceIoControl(hDrive, FSCTL_DISMOUNT_VOLUME, 0, 0, 0, 0, cb, 0)
' Make sure there is no prevent removal locks.
udtPMR.PreventMediaRemoval = 0
Call DeviceIoControl(hDrive, IOCTL_STORAGE_MEDIA_REMOVAL, VarPtr(udtPMR), Len(udtPMR), 0, 0, cb, 0)
' Eject the media.
Call DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, 0, 0, 0, 0, cb, 0)
Else
' You could still force the ejection here but any
' data in process would get corrupted.
Debug.Print "It's not safe to eject this media."
End If
' Close volume handle.
CloseHandle hDrive
End Sub
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79:





by: egl1044Posted on 2009-06-30 at 00:05:47ID: 24742934
I am pretty syre they both perform the same routine.
L and set the structure parameter to False.
Here is how you would eject a removable device using API.
1) Open a (volume) handle to the device using CreateFile()
2) Try to lock the volume using FSCTL_LOCK_VOLUME
3) If the call above succeeds it's safe to dismount the volume using FSCTL_DISMOUNT_VOLUME
4) You want to make sure there is no (prevent removal locks) on the volume so you call IOCTL_STORAGE_MEDIA_REMOVA
5) Use IOCTL_STORAGE_EJECT_MEDIA to eject the device.
6) Close the volume handle.
This is how all of them work but I will take a wild guess and say that maybe explorer doesn't go as far to disable any ejection locks but the disk managment eject option does.