Click here to Skip to main content
15,312,257 members
Articles / Multimedia
Posted 14 Jan 2011

Tagged as


2 bookmarked

Open and Close CD/DVD Drive Tray

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
14 Jan 2011CPOL
Illustrates a method of ejecting and closing the CD or DVD drive tray programmatically


This tip shows you how to open and close the CD or DVD drive tray programmatically.


There are tools that allow you to do this (like NirSoft's NirCmd), but I wrote this out of curiosity, to find out how it is done. The close command is particularly useful, since Windows doesn't provide that feature.


This is merely one method of controlling the drive tray, and may not work in all instances. I am sure there are other methods too.

The code provided merely illustrates the method, and is by no means production-ready.


We use the Media Control Interface (MCI) function mciSendCommand to open and close the tray. Specifying MCI_SET_DOOR_OPEN ejects the tray, and MCI_SET_DOOR_CLOSED retracts it. Example:

mciSendCommand(deviceId, MCI_SET, MCI_SET_DOOR_CLOSED, NULL);

Refer to the MSDN documentation for details of mciSendCommand and its arguments.

Demo Program

The following demo opens and closes the CD drive door. The drive letter is hardcoded for simplicity.

#include <tchar.h>
#include <windows.h>
#include <mmsystem.h> // for MCI functions

// Link to winmm.lib (usually included in project settings)
#pragma comment(lib, "winmm")

void ControlCdTray(TCHAR drive, DWORD command)
	// Not used here, only for debug
	MCIERROR mciError = 0;

	// Flags for MCI command

	// Open drive device and get device ID
	TCHAR elementName[] = { drive };
	MCI_OPEN_PARMS mciOpenParms = { 0 };
	mciOpenParms.lpstrDeviceType = (LPCTSTR)MCI_DEVTYPE_CD_AUDIO;
	mciOpenParms.lpstrElementName = elementName;	
	mciError = mciSendCommand(0, 
		MCI_OPEN, mciFlags, (DWORD_PTR)&mciOpenParms);

	// Eject or close tray using device ID
	MCI_SET_PARMS mciSetParms = { 0 };
	mciFlags = MCI_WAIT | command; // command is sent by caller
	mciError = mciSendCommand(mciOpenParms.wDeviceID, 
		MCI_SET, mciFlags, (DWORD_PTR)&mciSetParms);
	// Close device ID
	mciFlags = MCI_WAIT;
	MCI_GENERIC_PARMS mciGenericParms = { 0 };
	mciError = mciSendCommand(mciOpenParms.wDeviceID, 
		MCI_CLOSE, mciFlags, (DWORD_PTR)&mciGenericParms);

// Eject drive tray
void EjectCdTray(TCHAR drive)
	ControlCdTray(drive, MCI_SET_DOOR_OPEN);

// Retract drive tray
void CloseCdTray(TCHAR drive)
	ControlCdTray(drive, MCI_SET_DOOR_CLOSED);

int _tmain(int argc, _TCHAR* argv[])
	EjectCdTray(TEXT('E')); // drive letter hardcoded

	return 0;


That's it. Let me know if you discover any bugs.


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


About the Author

Japan Japan
No Biography provided

Comments and Discussions

GeneralMy vote of 5 Pin
SirHesti13-Sep-20 21:27
MemberSirHesti13-Sep-20 21:27 
GeneralMCI String interpreter, Pin
tonymudd17-Jan-11 7:18
Membertonymudd17-Jan-11 7:18 
this brings back memories. I did something similar a while ago, but combined with a telnet-like server and a windows service, so anyone on the network could telnet to one guys PC (because it was a windows service it was running before he'd even logged in in the morning) and send any MCI command strings - like "set cdaudio door open". I think there are commands to turn off the left audio channel and change the volume too. Serves him right for playing his music too loud in an open-plan office.
GeneralRe: MCI String interpreter, Pin
Indivara18-Jan-11 2:46
professionalIndivara18-Jan-11 2:46 

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

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