I have a custom developed USB card reader. I am using the following code to interact and iterrate over the device:
http://www.codeproject.com/KB/system/usbeject.aspx
I'v the same task. :)
You need use ejecting removable media. See c++ sample here: How To Ejecting Removable Media
I came here accidentally while doing a search on "CM_Request_Device_Eject_NoUi", and saw that it was similar to a solution I'd recently done by pulling together similar pieces of a solution. Forgive the late answers.
Here's what worked for me (this also addresses some issues I've seen on other SO questions regarding AutoEjectVolume
from the Microsoft sample not "doing everything" that the system does when you Safely Remove Hardware using the OS):
AutoEjectVolume
with code that is, in effect, the body of the RemoveDrive
method from How to Prepare a USB Drive for Safe Removal. Note that this later work relies heavily on two other CodeProject articles — including the one you referenced in your question — ported to C#.In 2, I say "in effect" because — in practice — you use the same hVolume
in both solutions, and it makes more sense to do all the checks in the CodeProject RemoveDrive
method before calling LockVolume
, DismountVolume
, or PrepareRemovalOfVolume
in the Microsoft solution, and then call CM_Request_Device_Eject_NoUi
as shown in the CodeProject solution.
A short pseudo-code summary:
CreateFile
(CodeProject)DismountVolume
,
if any of the steps above fail (CodeProject)LockVolume
, DismountVolume
, and PrepareRemovalOfVolume
using the hVolume
returned from CreateFile
(Microsoft)hVolume
at any time after thisCM_Request_Device_Eject_NoUi
on the drive's parent's device