Solved

Eject media from any CD-ROM (without DeviceIOControl or ASPI)

Posted on 2000-04-29
13
504 Views
Last Modified: 2013-12-03
Hi,

I would like get the same functionality in my (Borland C++ builder) code as what happens when you right click on a CD-ROM drive in My Computer and choose "Eject".

I'm perfectly able of opening the tray of whatever drive I'd like through the ASPI layer (sending a Start/Stop Unit command) or through the use of DeviceIOControl (only NT).
Unfortunately this is too low-level to be useable for me since it bypasses any application which could have control over the disc in the drive.
For example, Adaptec's Direct CD (UDF writer) keeps some data cached, you can send commands directly to the drive through the ASPI layer and you will be able to open the tray (Allow medium removal + Start/Stop Unit), but this will cause DirectCD to be unaware of any medium removal request and it will fail to complete all necessary actions (such as flushing buffers) prior to removal (with dramatic consequences, such as data loss on the CD-R/RW).

I've found a partial solution  :

mciSendSTring("set cdaudio door open",NULL,0,NULL);

and this works great ! Any application such as DirectCD will be informed of this eject request and will take care of business before ejecting.
UNFORTUNATELY I am unable to select which drive will be opened (it seems as if it always picks the first drive).

So, isn't this possible with MCI ? (which would suprise me)

If not, can you suggest something that works on the same level as MCI and also offers the ability to select which drive you want to control.



So, in short :
I'll need some way of opening the tray that :
- Isn't too low level for other applications too miss (such as MCI).
- That works both in Win98, WinNT and Win2K.
- And I should be able to select the drive which I want to control.

Or if somebody just could tell me how to select a drive with MCI...


Thanks,
0
Comment
Question by:skris
  • 4
  • 4
  • 2
  • +2
13 Comments
 
LVL 2

Expert Comment

by:DarrinE
ID: 2762072
listening .....
0
 
LVL 32

Accepted Solution

by:
jhance earned 400 total points
ID: 2762259
Can you use mciSendCommand instead of mciSendString?  mciSendCommand lets you specify thje device.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2762693
HOWTO: Eject Removable Media on Windows 95

http://support.microsoft.com/support/kb/articles/Q168/1/80.ASP 

0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 15

Expert Comment

by:NickRepin
ID: 2762697
For NT, DeviceIoControl(IOCTL_STORAGE_EJECT_MEDIA)
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2762707
Q165721 - HOWTO: Ejecting Removable Media in Windows NT/Windows 2000

http://support.microsoft.com/support/kb/articles/Q165/7/21.ASP
0
 
LVL 3

Expert Comment

by:SamHobbs
ID: 2762733
You say "through the use of DeviceIOControl (only NT)" but I am not sure if your requirements are limited to NT.

For NT, you could use DeviceIoControl to first lock the volume, then do the eject. The volume lock (FSCTL_LOCK_VOLUME) would probably fail if the volume is in use.

See http://msdn.microsoft.com/library/psdk/winbase/devio_02w5.htm

For 95, you might be able to use the count of locks to determine if the media can be ejected safely.

See http://msdn.microsoft.com/library/psdk/win95/95func_9jl4.htm
0
 
LVL 3

Expert Comment

by:SamHobbs
ID: 2762737
OOOps! I got distracted and so NickRepin beat me to the answer.
0
 

Author Comment

by:skris
ID: 2763564
Ok,

- DeviceIOControl isn't the way to go since I doesn't work on NT (and probably too low-level)
- 95/98 : INT21h locks or unlocks the volume but does not eject it. Furthermore, I'm not that keen of embedding assembler in my code (furthermore this is again too low-level for my purposes)

Anyway, the comment from jhance got me thinking... (use mciSendCommand instead of mciSendString).
So, I wrote this little piece of code that will do the job :

MCI_OPEN_PARMS MCI_Device;
DWORD dwRes,dwFlags;
char DriveLetter[]="f:";
char mci_error[130];

memset(&MCI_Device,0,sizeof(MCI_Device));
dwFlags=MCI_OPEN_TYPE|MCI_OPEN_SHAREABLE|MCI_OPEN_TYPE_ID|MCI_OPEN_ELEMENT;
MCI_Device.lpstrDeviceType=(LPSTR)MCI_DEVTYPE_CD_AUDIO;
MCI_Device.lpstrElementName=(LPSTR)DriveLetter;
// Open the device
dwRes=mciSendCommand(0,MCI_OPEN,dwFlags,(DWORD)(LPSTR)&MCI_Device);
if (dwRes) {
    mciGetErrorString(dwRes,mci_error,sizeof(mci_error));
    printf ("\nMCI_OPEN (%d) [%s]",dwRes,mci_error);
} else {
    // Open/Close the tray
    dwRes=mciSendCommand(MCI_Device.wDeviceID,MCI_SET,MCI_SET_DOOR_OPEN,0);
    //dwRes=mciSendCommand(MCI_Device.wDeviceID,MCI_SET,MCI_SET_DOOR_CLOSED,0);
    if (dwRes) {
        mciGetErrorString(dwRes,mci_error,sizeof(mci_error));
        printf ("\nMCI_SET (%d) [%s]",dwRes,mci_error);
    }
    // Close the device
    dwRes=mciSendCommand(MCI_Device.wDeviceID,MCI_CLOSE,0,0);
    if (dwRes) {
        mciGetErrorString(dwRes,mci_error,sizeof(mci_error));
        printf ("\nMCI_CLOSE (%d) [%s]",dwRes,mci_error);
    }
}

With this code you can use MCI to open any drive. Just specify the desired drive letter in char DriveLetter[].

Thanks for all the responses, especially to jhance.

Kris

0
 

Author Comment

by:skris
ID: 2763567
So, now I've answered my own question :

- How do I close the question ?
- Is it possible to give jhance some points for pointing me in the right direction (even though he only added a comment) ?

0
 

Author Comment

by:skris
ID: 2763569
Oops, I meant DeviceIOControl doesn't work on 95/98 (and only on NT).
0
 
LVL 32

Expert Comment

by:jhance
ID: 2763577
skris,

You see where it says "Accept Comment as Answer"?  Now that you rejected the pending answer you should see it....
0
 

Author Comment

by:skris
ID: 2763592
Ok, found it.
Enjoy the points.
Thanks for the input. It still needed some figuring out, but it got me to where I needed to be (so only a C).

Thanks,
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 2764542
Amazing...

The best info which can ever be is from MS. MS says how the CD must be ejected. MS is talking about how it can be done safely, with care about the open files etc.

>> DeviceIOControl isn't the way to go since I doesn't work on NT

Why do you think so?!!

>> 95/98 : INT21h locks or unlocks the volume but does not eject it

Laugh loudly...

Well, inspite of grade C you gave to jhance, you still can use the info from those MS articles.






0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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