Solved

Open / Close CD-ROM from Console

Posted on 2004-09-01
2
281 Views
Last Modified: 2012-06-27
What is the easiest way to open / close a CD-ROM from the console?  

Regards,
Tony
0
Comment
Question by:fattumsdad
2 Comments
 
LVL 5

Accepted Solution

by:
danielsonchris earned 300 total points
ID: 11958653
What OS?  
If we are talking *nux just use
`eject`
and
`eject -t`
to retract.

If windows then using C++ (since this is the C++ forum):

#include "mmsystem.h"      // For mci functions
#include <stdio.h>            // For printf()

//call like Win32Eject(TRUE, 'E');     To eject drive E
//           Win32Eject(FALSE, 'E');     To retract drive E
void Win32Eject(BOOL bOpenDrive, TCHAR cDrive)
{
      MCI_OPEN_PARMS op;
      MCI_STATUS_PARMS st;
      DWORD flags;

      TCHAR szDriveName[4];
      strcpy(szDriveName, "X:");

      ::ZeroMemory(&op, sizeof(MCI_OPEN_PARMS));
      op.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_CD_AUDIO;
      
      if(cDrive > 1)
      {
            szDriveName[0] = cDrive;
            op.lpstrElementName = szDriveName;
            flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | MCI_OPEN_ELEMENT | MCI_OPEN_SHAREABLE;
      }
      else flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | MCI_OPEN_SHAREABLE;

      if (!mciSendCommand(0,MCI_OPEN,flags,(unsigned long)&op))
      {
            st.dwItem = MCI_STATUS_READY;

            if(bOpenDrive)
                  mciSendCommand(op.wDeviceID,MCI_SET,MCI_SET_DOOR_OPEN,0);
            else
                  mciSendCommand(op.wDeviceID,MCI_SET,MCI_SET_DOOR_CLOSED,0);

            mciSendCommand(op.wDeviceID,MCI_CLOSE,MCI_WAIT,0);
      }
}

you can take the above code and then create a console program (eject.exe).  From there you can script the CD Door opening and closing.
Peace,
Chris
0
 
LVL 8

Expert Comment

by:pjcrooks2000
ID: 11958939
Aye looks a good idea to me :)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

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

23 Experts available now in Live!

Get 1:1 Help Now