Safely remove a USB drive using the Win32 API?

六眼飞鱼酱① 提交于 2019-12-17 04:27:02

问题


How do I remove a USB drive using the Win32 API? I do a lot of work on embedded systems and on one of these I have to copy my programs on a USB stick and insert it into the target hardware.

Since I mostly work on the console I don't like to use the mouse and click on the small task-bar icon hundred times a day.

I'd love to write a little program to do exactly that so I can put it into my makefiles, but I haven't found any API call that does the same thing.

Any ideas?


回答1:


You can use the CM_Request_Device_Eject() function as well as some other possibilities. Consult the following projects and articles:

DevEject: Straightforward. http://www.withopf.com/tools/deveject/

A useful CodeProject article: http://www.codeproject.com/KB/system/RemoveDriveByLetter.aspx




回答2:


It looks like Sync lets you specify -e to eject removable drives. While not a win32 API, you could probably just call sync -e [drive_letter] from your makefile.




回答3:


Here is a technet article about removable storage media. Look for DismountNtmsMedia.




回答4:


Here's a solution in Delphi, that I've modified and put into a service for use in a very large enterprise. Go to: link text

Look for "scapi (Setup & Config Manager API)", and download it. There will be a demo program called USBView that will get you on your way. If you have Delphi, this also includes a TUSBDeviceTree component that you can use to gather information about a USB device when.

Regards



来源:https://stackoverflow.com/questions/85649/safely-remove-a-usb-drive-using-the-win32-api

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