Link to home
Start Free TrialLog in
Avatar of rayocs
rayocs

asked on

Get drive handle in usermode

I'm writing an program to perform auto Safe Removal for all inserted USB drives with a click of a button.

From a MSDN KB that I've found, the recommended step are:
1. Call CreateFile with GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, and OPEN_EXISTING.
2. Lock the volume by issuing the FSCTL_LOCK_VOLUME IOCTL via DeviceIoControl.
3. Dismount the volume by issuing the FSCTL_DISMOUNT_VOLUME IOCTL.
4. Make sure the media can be removed by issuing the IOCTL_STORAGE_MEDIA_REMOVAL IOCTL.
5. Eject the media with the IOCTL_STORAGE_EJECT_MEDIA IOCTL.
6. Close the volume handle obtained in the first step or issue the FSCTL_UNLOCK_VOLUME IOCTL.

The above are working well when I've admin priviledges. Else, all will fail.

I know that the reason is because CreateFile will only return the drive handle with admin priviledge. However, my program needs to work under the limited user mode.

Is there an alternative method to the above or is there a way for me to obtained the drive handle in user mode? Only handles to USB drives are required.

Can any SetupDixxx or CM_xxx functions be used?

Thanks.
Avatar of jkr
jkr
Flag of Germany image

>>Is there an alternative method to the above

No. If you want to obtain a drive handle, your application must run under an account that has admin provileges. But, what is it that you finally want to achieve? Maybe there's a whole different way.
You could try to work through the ShellExec() API to perform the eject actions for the shell objects that are the USB drives.
... which in turn requires admin privileges to work properly.
well, forget about the ShellExec. Look at the recepy here: http://ask-leo.com/is_there_a_way_to_safely_remove_hardware_from_a_batch_file.html This shows how to do the work from batch file. You can use the DevCon (http://support.microsoft.com/Default.aspx?kbid=311272 or http://msdn2.microsoft.com/en-us/library/ms792824.aspx) from any program. The source code for DevCon is also available in the Windows DDK (which is available from http://www.microsoft.com/whdc/devtools/ddk/default.mspx) under DDK root\Src\Setup\Devcon, along with documentation. Some actions of DevCon.exe require administrator rights, but not the ones that you need to safe remove the devices.

Note that there exists a shareware tool to help people who extensively use USB devices : http://www.safelyremove.com/
Avatar of rayocs
rayocs

ASKER

Hi jkr,
I'm trying to write a program that will force safe eject to all inserted USB drive with a single click of a button. The program needs to work under limited/ restricted user mode. Above are the fns which I would like to call but I encounter 2 problems.
1) Unable to obtain device handle.
2) Using DeviceIOControl() fn returns ERROR_INVALID_FUNCTION.

*****
Hi Alexcohn,

Thanks for the leads. Have tested devcon.exe and safelyremove pgm but both doesn't help much.

The devcon.exe require admin rights to run and safelyremove still require to eject the drive 1 by 1 though you can do it using shortcut keys. Plus both are not redistributable freely.

I'm currently looking through the devcon DDK sample codes. Hopes I can find some solution there. If you can provide more specific pointers, it will be much appreciated.
As I mentioned, devcon requires admin rights for some of its tasks; but to eject it doesn't. You can't redistribute devcon.exe, but you can freely use their code, stripe away all unnecessary branches, and repackage it so that it enumerates all drives that can be ejected.
ASKER CERTIFIED SOLUTION
Avatar of alexcohn
alexcohn
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rayocs

ASKER

Hi Alexcohn,

Finally.., I managed to test all the reference you gave. Well the easiest solution for me is the one from the codeproject. Worked well enough for me. Thanks.