Uwe
Thu May 31 02:04:26 CDT 2007
RFlaugher wrote:
> Windows explorer allows for a soft eject feature for USB drives that I would
> like to recreate in my program. This is useful for USB drives where a
> compact flash is inserted and removed but the USB drive remains connected to
> the PC. Does anyone know what functions are involved to accomplish this? I
> have seen code which accomplishes the hard eject (removing the entire USB
> device) but not the soft eject which only flushes the cache.
Open the volume using a string like "\\\\.\\X:", with GENERIC_READ
access.
Then flush the volume's cache:
FlushFileBuffers(hVol);
Then lock and dismount:
res = DeviceIoControl(hVol, FSCTL_LOCK_VOLUME, 0, 0, 0, 0, &dwRet, 0);
if ( res ) {
res = DeviceIoControl(hVol, FSCTL_DISMOUNT_VOLUME,
0, 0, 0, 0, &dwRet, 0);
}
Then eject:
if ( res ) {
res = DeviceIoControl(hVol, IOCTL_STORAGE_EJECT_MEDIA,
0, 0, 0, 0, &dwRet, 0);
}
Then close hVol. Do not eject when lock and diskmount
failed. IOCTL_STORAGE_EJECT_MEDIA is quite reckless!
By default this requires admin previleges under XP but it
can be changed by a policy:
http://www.uwe-sieber.de/usbstick_e.html#ejectmedia
Uwe