How to eject a USB removable disk/volume, similar to the “Eject” function in Windows Explorer?

别来无恙 提交于 2019-11-30 11:42:52

Eventually, I found out where I was making a mistake with approach #2.

It turns out that for some reason I was not setting the desired access correctly when opening the handle to the volume using CreateFile.

The correct access mode is GENERIC_READ | GENERIC_WRITE and I was passing 0. After correcting my error I was able to successfully eject the device using DeviceIoControl - IOCTL_STORAGE_EJECT_MEDIA, as well as with method #1, using CM_Request_Device_Eject.

Finally, it turns out that method #2 is indeed the method used by the shell context menu's "Eject" function. Using this method the device reacts correctly.

John Castleman

I came here accidentally while doing a search on "CM_Request_Device_Eject", and saw that it was similar to a solution I'd recently done by pulling together similar pieces of a solution. Forgive the late answer.

I've summarized the steps I've done for this on my project in this SO answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!