Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Open and Close CD/DVD Drive Tray

, 13 Mar 2014
Rate this:
Please Sign up or sign in to vote.
Illustrates a method of ejecting and closing the CD or DVD drive tray programmatically
Here's another method of doing it. This is something I had written some time ago and forgotten about. Just a code dump for now. Same disclaimer as original tip.

#include <tchar.h>
#include <windows.h>
#include <Mmsystem.h>
#include <iostream>
#include <string>

using namespace std;
 
void usage();
 
int _tmain(int argc, _TCHAR* argv[])
{
    int result = 0;
    char returnString[MAX_PATH] = { 0 };
    MCIERROR mcierror = 0;
 
    if (argc == 2)
    {
        basic_string<TCHAR> command = argv[1];
        if (command == TEXT("open"))
        {
            mcierror = mciSendStringA(
                ("set cdaudio door open"), returnString,
                _countof(returnString), NULL);
        }
        else if (command == TEXT("close"))
        {
            mcierror = mciSendStringA(
                ("set cdaudio door closed"), returnString,
                _countof(returnString), NULL);
        }
        else
        {
            usage();
            result = 1;
        }
        if (mcierror != 0)
        {
            cerr << "Unable to execute command - ";
            char errorString[MAX_PATH] = { 0 };
            mciGetErrorStringA(mcierror, errorString, _countof(errorString));
            cerr << errorString;
            result = 2;
        }
    }
    else
    {
        usage();
        result = 1;
    }
    return result;
}
 
void usage()
{
    cout << "Usage: cdtray <command>\n\n";
    cout << "Commands:\n";
    cout << " open  - Opens CD drive tray\n";
    cout << " close - Closes CD drive tray\n";
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Indivara
Engineer
Japan Japan
No Biography provided

Comments and Discussions

 
BugThere is Bug in the code Pinmemberneelanjan bhattacharyya13-Mar-14 6:02 
GeneralRe: There is Bug in the code Pinmemberenhzflep17-Mar-14 1:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 13 Mar 2014
Article Copyright 2011 by Indivara
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid