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

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,
skrisAsked:
Who is Participating?
 
jhanceConnect With a Mentor Commented:
Can you use mciSendCommand instead of mciSendString?  mciSendCommand lets you specify thje device.
0
 
DarrinECommented:
listening .....
0
 
NickRepinCommented:
HOWTO: Eject Removable Media on Windows 95

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

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
NickRepinCommented:
For NT, DeviceIoControl(IOCTL_STORAGE_EJECT_MEDIA)
0
 
NickRepinCommented:
Q165721 - HOWTO: Ejecting Removable Media in Windows NT/Windows 2000

http://support.microsoft.com/support/kb/articles/Q165/7/21.ASP
0
 
SamHobbsCommented:
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
 
SamHobbsCommented:
OOOps! I got distracted and so NickRepin beat me to the answer.
0
 
skrisAuthor Commented:
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
 
skrisAuthor Commented:
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
 
skrisAuthor Commented:
Oops, I meant DeviceIOControl doesn't work on 95/98 (and only on NT).
0
 
jhanceCommented:
skris,

You see where it says "Accept Comment as Answer"?  Now that you rejected the pending answer you should see it....
0
 
skrisAuthor Commented:
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
 
NickRepinCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.