Do you know what is the API, or sequence of API calls that windows uses to accomplish the "Eject" function which is available on the shell context menu for removable volumes?
So far I've tried two things:
using CM_Request_Device_Eject, I enumerate the removable disks (using the SetupDiXXX APIs), find the one that I'm interested in, walk the device manager hierarchy (using CM_XXX APIs) and finally call
CM_Request_Device_Eject
on thedevInst
of the device I'm interesed in. This works in the sense that it does remove the volumes from My Computer and makes the device "safe to remove" (ready to be removed) but it is not the same as the shell context menu "Eject" function. The way I know this is because the device that I'm trying to eject is supposed to do something when it is ejected and that something is not happening when I do the eject usingCM_Request_Device_Eject
.using DeviceIoControl with the IOCTL_STORAGE_EJECT_MEDIA control code. The sequence of events is:
- obtain a handle to the volume I'm interested in using CreateFile as suggested in the documentation
- try to lock the volume with FSCTL_LOCK_VOLUME
- try to dismount it using FSCTL_DISMOUNT_VOLUME
- disable the prevent storage media removal using IOCTL_STORAGE_MEDIA_REMOVAL
- and finally execute the IOCTL_STORAGE_EJECT_MEDIA function.
This doesn't work at all. Each one of the `DeviceIoControl` calls fails with `ERROR_IVALID_FUNCTION` (0x00000001). I don't know why the calls fail. I've verified that other calls to DeviceIoControl work fine for the same file handle (such as [IOCTL_STORAGE_GET_DEVICE_NUMBER][11])
Finally, my development machine is running Windows 7 x64, and in order to get the second method to work I've tried running my application with Administrator privileges and that did not change anything.
Any information is welcome. Even maybe how to invoke the shell "Eject" menu item as a last recourse.
DeviceIoControl
. – BurkhardDeviceIoControl
returns1
on success. When you report gettingERROR_INVALID_FUNCTION
, is that the return value ofDeviceIoControl
, or is it returning0
and you are callingGetLastError
? – BurkhardCM_Request_Device_Eject
and I would you recommend the way 1. – Parquetry