Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safely remove a USB drive using the Win32 API?

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?

like image 254
Nils Pipenbrinck Avatar asked Sep 17 '08 17:09

Nils Pipenbrinck


People also ask

What is the safe method to remove USB?

To avoid losing data, it's important to remove external hardware like hard drives and USB drives safely. Look for the Safely Remove Hardware icon on the taskbar. If you don't see it, select Show hidden icons . Press and hold (or right-click) the icon and select the hardware you want to remove.

How do I safely remove USB from Windows 10?

Right-click or press and hold on the drive that you want to remove and, in the menu that opens, choose Eject. If everything went well, you see a notification that it is Safe To Remove Hardware. Unplug the device that you no longer want to use on your Windows 10 PC, and you are done.

How do I safely remove USB from Windows 11?

The most common way to safely eject your USB drive is by way of the System Tray. In the System Tray, click the “Up Arrow” icon to expand the menu. Next, double-click the “USB” icon. In the sub-menu that appears, select the “Eject <USB>” option.


3 Answers

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

like image 128
Kris Kumler Avatar answered Oct 12 '22 00:10

Kris Kumler


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.

like image 6
Grant Wagner Avatar answered Oct 11 '22 23:10

Grant Wagner


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

like image 4
jop Avatar answered Oct 11 '22 23:10

jop