views:

5897

answers:

7

Anyone knows how to 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 an 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 hundret 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?

+3  A: 

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.

Grant Wagner
But how to know which drive letter has been added for USB drive? I also want to remove USB drives based on volumes like Do not allow USB volume more than 5GG etc.
Aizaz
+1  A: 

This article and sample should do the trick: http://support.microsoft.com/default.aspx?scid=kb;en-us;165721

petr k.
+7  A: 

You can use the CM_Request_Device_Eject() function as well as possibly 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

Kris Kumler
Perfect.. You even linked to an utility to do the job. Thanks a lot.
Nils Pipenbrinck
MSDN reference @ http://msdn.microsoft.com/en-us/library/ms790831.aspx
stephbu
A: 

Pls. change Save->Safe in the title

akauppi
thanks.. I always get these two wrong.
Nils Pipenbrinck
+5  A: 

See Microsoft Knowledge Base article Q165721: http://support.microsoft.com/kb/165721 -- includes full C source code

mdb
+2  A: 

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

jop
A: 

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

Mick