Solved

eject USB devices by code

Posted on 2006-07-19
14
2,267 Views
Last Modified: 2008-01-09
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
Comment
Question by:urif
  • 7
  • 3
  • 3
  • +1
14 Comments
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17137523
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
 

Author Comment

by:urif
ID: 17137749
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
 
LVL 86

Accepted Solution

by:
jkr earned 63 total points
ID: 17138634
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
 
LVL 14

Assisted Solution

by:cwwkie
cwwkie earned 62 total points
ID: 17141792
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
 

Author Comment

by:urif
ID: 17147757
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
 
LVL 14

Expert Comment

by:cwwkie
ID: 17148042
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
 

Author Comment

by:urif
ID: 17162405
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 8

Expert Comment

by:manish_regmi
ID: 17162422
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
 

Author Comment

by:urif
ID: 17162710
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
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17162744
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
 

Author Comment

by:urif
ID: 17162803
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
 
LVL 14

Expert Comment

by:cwwkie
ID: 17163547
> 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
 

Author Comment

by:urif
ID: 17163879
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
 

Author Comment

by:urif
ID: 17190558
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now