[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2334
  • Last Modified:

eject USB devices by code

after breaking my head trying to make EjectDiskFromSADrive() work i finally decided to ask here.
i need to "safely remove" or eject USB devices from a computer by code. the operating system is Win XP or newer.

the msdn page for EjectDiskFromSADrive()

http://windowssdk.msdn.microsoft.com/en-us/library/ms724499.aspx

but so far i couldn't manage to even compile any code.
code sample will be greatly appreciated.

uri
0
urif
Asked:
urif
  • 7
  • 3
  • 3
  • +1
2 Solutions
 
manish_regmiCommented:
hi
 i  haven't use this but by a quick look i noticed

Client       Requires Windows Vista.
Server       Requires Windows Server "Longhorn" or Windows Server 2003.

It means Windows Xp is not supported. Try using this in server 2003

regards
Manish Regmi
0
 
urifAuthor Commented:
thanks manish, but that's not the issue here.
if you know of any way to eject those usb drives i'd appreciate the code (for win xp and up)
0
 
jkrCommented:
Check out http://www.codeproject.com/useritems/RemoveDriveByLetter.asp ("How to prepare an USB drive for save removal"), the scoop is to call 'CM_Request_Device_EjectW()'
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
cwwkieCommented:
On http://www.heise.de/ct/03/16/links/208.shtml is a c++ example which also allows to eject a device based on a device name. It also uses CM_Request_Device_Eject.
0
 
urifAuthor Commented:
thank jkr, that's a good example, the only problem is that i can't compile it. i don't have the ddk and the files listed as "possible replacements" won;'t even compile... but let me see if i can fix it. and i'll give you the points.

and i will also check cwwkie solution, if that works out the box then the points go to him.

thanks both of you again
0
 
cwwkieCommented:
If any of the examples don't compile, we can help you. Even the order of the include directories is sometimes important.
Can you post the error message, and mark the line with the error?
0
 
urifAuthor Commented:
jkr ok, i included the file from http://www.reactos.org/generated/doxygen/d3/d0e/cfg_8h-source.html
and this is what i get in return:

Compiling...
RemoveDriveByLetter.cpp
d:\RemoveDriveByLetter\RemoveDriveByLetter.cpp(24) : error C2146: syntax error : missing ';' before identifier 'GetDrivesDevInstByDiskNumber'
d:\RemoveDriveByLetter\RemoveDriveByLetter.cpp(24) : error C2501: 'DEVINST' : missing storage-class or type specifiers
d:\RemoveDriveByLetter\RemoveDriveByLetter.cpp(24) : fatal error C1004: unexpected end of file found
Error executing cl.exe.


the line with the errors is

DEVINST GetDrivesDevInstByDiskNumber(long DiskNumber, UINT DriveType);

after removing DEVINST with a simple int:

d:\RemoveDriveByLetter\RemoveDriveByLetter.cpp(82) : error C2065: 'CM_Get_Parent' : undeclared identifier
d:\tmp\code\removedrive\RemoveDriveByLetter\RemoveDriveByLetter.cpp(83) : error C2065: 'CM_Get_DevNode_Status' : undeclared identifier
d:\RemoveDriveByLetter\RemoveDriveByLetter.cpp(90) : error C2065: 'CM_Request_Device_EjectW' : undeclared identifier
d:\RemoveDriveByLetter\RemoveDriveByLetter.cpp(93) : error C2065: 'CM_Query_And_Remove_SubTreeW' : undeclared identifier
d:\RemoveDriveByLetter\RemoveDriveByLetter.cpp(95) : error C2065: 'CR_SUCCESS' : undeclared identifier
d:\RemoveDriveByLetter\RemoveDriveByLetter.cpp(130) : error C2065: 'GUID_DEVINTERFACE_DISK' : undeclared identifier
d:\RemoveDriveByLetter\RemoveDriveByLetter.cpp(133) : error C2065: 'GUID_DEVINTERFACE_CDROM' : undeclared identifier
Error executing cl.exe.


and i can't find where those variables / constants are declared.



cwwkie : now, for the second example, i got that same example searching in google. i know my c++ is rusty, but i can't understand how he manage to eject the drive. i am having trouble following the code. wost yet, if i want to use part of the code and convert it to C i just wont work. at all.
0
 
manish_regmiCommented:
It is working well in my VS 2005.

DEVINST and all those stuffs are in cfgmgr32.h
Did you include it?

regards
Manish Regmi


0
 
urifAuthor Commented:
hi manish, as i wrote before, i don't have the DDK and i used the the header from http://www.reactos.org/generated/doxygen/d3/d0e/cfg_8h-source.html as written on that article (a replacement file for the cfgmgr32). aparently that file is not complete.
0
 
manish_regmiCommented:
oops sorry.:)

The header is not enough you also need libraries.
The code-project page says you cannot compile and link with VS 6 because the LIbs are outdated.You need to install newer platform SDK.

its here:
http://www.microsoft.com/msdownload/platformsdk/sdkupdate/XPSP2FULLInstall.htm

It says After installing, go in the startmenu click on "Register PSDK
directories with Visual Studio" to use the new header and libs.

Have you tried all this?

regards
Manish Regmi
0
 
urifAuthor Commented:
so basically i need to download 266mb to be able to compile a program that has only one function and that its size is something like 53k...

now, from what i understood after reading a bit, without the DDK i still won't be able to compile the code. i managed to download all the headers and files (cfgmgr32.h, ntddk.h, etc) and now it's failing on other stuff, such as in the directive _asm_

thanks so much, i really appreciate the effort, although i don't think i will download and install 266mb (or more after the install) so i can be able to compile one line of code.

it has to be another way to unmounting a removable USB device.
0
 
cwwkieCommented:
> i don't think i will download and install 266mb (or more after the install) so i can be able to compile one line of code.

the link I provided includes an executable. You can call that from your own program (using CreateProcess).
0
 
urifAuthor Commented:
cwwkie thanks, but i already have a nice little utility that comes with cygwin that does that. the thing is that i need to include code to a much larger application written in C.
that's why my being so insisting.

aparently there isn't a solution, it's either intall the ddk (pay $5000 or more and install 700mb), or download 266mb, install them and hope that it will compile or ... or...

i come from UNIX and Linux, where most of the libraries are already available on the system, for free. it just can't understant why i need to spend so much time trying to figure how to perform a trivial task on an OS....
0
 
urifAuthor Commented:
ok, i guess then that without the ddk or other expensive or super big solution i won't be able to perform the simple task of unmounting a usb device. good for microsoft.
anyway, since cwwkie  and jkr tried really hard i will split the points. thanks all of you anyway.
0

Featured Post

Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

  • 7
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now