Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2000-04-29
13
Medium Priority
?
513 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +2
13 Comments
 
LVL 2

Expert Comment

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

Accepted Solution

by:
jhance earned 800 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

715 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