Removing a device from Win2K system
I am writing a program to automatically retrieve files from a USB Mass Storage device, and I would like to automatically remove the device after I'm done (like using the "Unplug or Eject Hardware" dialog). I have been searching the Win32 API docs, and I can't find any way to do it. Does anyone know how this can be done?
OK, I found setupapi.dll which looks like it should do what I want, but calling CM_Request_Device_Eject gets vetoed with PNP_VetoIllegalDeviceRequest. Any suggestions?
There's probably more than one device involved here.
Say I've plugged a pen drive into my pc. When I try to
stop it manually using the control panel or systray to bring
up the "Safely remove hardware dialog", I get the message "Stopping
this device will also stop"... and it lists three items, "USB Mass
storage device", "Generic volume" (with a drive letter), and then the
actual USB ramdrive device itself. If you click the button
labelled 'Display device components' in the main "safely remove"
dialog, you see how they're layered one on top of the other.
So I reckon you probably need to stop the dependent devices
first. You may be able to simply unmount the particular generic
volume or you may need to stop it using the p'n'p api, but presumably
if you could do things in the same order as the "Safely remove" dialog
it ought to work.
CM_Request_Device_Eject is probly for ejecting the tray of a CD-ROM drive - So I guess it wouldn't work with a flash drive, and that would be why Windows won't let you.
I've tried walking the tree and stopping all the devices below it, but they all still fail in the same way. I've tried using Depenency Walker to find out which functions hotplug.dll calls (it's the dll responsible for the unplug hardware dialog box) and it doesn't seem to call many functions.
I hate overly complicated systems like this. On Linux I could program this as
system("umount /mnt/flash");. But since I'm writing it for someone else I don't get to choose the OS.
This page gives me a headache just looking at it, but it looks to be what you need. It also makes Linux look very inviting...
OK, I solved it. I called CM_Request_Device_Eject on the parent node and it worked fine.