Click here to Skip to main content
11,711,009 members (82,980 online)
Click here to Skip to main content

Tagged as

Open and Close CD/DVD Drive Tray

, 13 Mar 2014 CPOL 7.7K 9
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

You may also be interested in...

Comments and Discussions

 
BugThere is Bug in the code Pin
neelanjan bhattacharyya13-Mar-14 6:02
memberneelanjan bhattacharyya13-Mar-14 6:02 
GeneralRe: There is Bug in the code Pin
enhzflep17-Mar-14 1:02
memberenhzflep17-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 | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 13 Mar 2014
Article Copyright 2011 by Indivara
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid