Solved

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

Posted on 2000-04-29
13
500 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
Comment Utility
listening .....
0
 
LVL 32

Accepted Solution

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

Expert Comment

by:NickRepin
Comment Utility
HOWTO: Eject Removable Media on Windows 95

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

0
 
LVL 15

Expert Comment

by:NickRepin
Comment Utility
For NT, DeviceIoControl(IOCTL_STORAGE_EJECT_MEDIA)
0
 
LVL 15

Expert Comment

by:NickRepin
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

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

Author Comment

by:skris
Comment Utility
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
Comment Utility
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
Comment Utility
Oops, I meant DeviceIOControl doesn't work on 95/98 (and only on NT).
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

10 Experts available now in Live!

Get 1:1 Help Now