Click here to Skip to main content
15,895,781 members
Articles / Programming Languages / C++

Open or Close CD-ROM drive

Rate me:
Please Sign up or sign in to vote.
3.97/5 (17 votes)
28 Sep 20051 min read 151.5K   3.4K   23  
Eject or close specified CD-ROM or DVD-ROM in the system.
#include <windows.h>
#include <mmsystem.h>

BOOL fHelp = FALSE;

char HelpText[] = 
"CDROM - CD/DVD open/close utility for MS Windows - � 2005 by eraser (eraser@senior.cz)\n\n"
"Syntax: CDROM open | close letter\n\n"
" \t<none>\t- display this message\n"
"\topen\t- open drive\n"
"\tclose\t- close drive\n"
"\tletter\t- drive letter\n\n"
"Examples:\n"
"\tcdrom.exe open E\t- open drive E\n"
"\tcdrom.exe close E\t- close drive E";


void CDRomOpen(BOOL bOpenDrive, char *drive);
int ProcessCommandLine(LPSTR lpCmdLine, BOOL *cmd, char *drive);

int	WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	BOOL open;
	char drive[8];

	ProcessCommandLine(szCmdLine, &open, drive);

	if (fHelp)
	{
		MessageBox(NULL, HelpText, "CD/DVD-Drive utility", MB_OK);

		return 0;
	}

	CDRomOpen(open, drive);

	return 0;
}

void CDRomOpen(BOOL bOpenDrive, char *drive)
{ 
	MCI_OPEN_PARMS open;
	DWORD flags;

	ZeroMemory(&open, sizeof(MCI_OPEN_PARMS));

	open.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_CD_AUDIO;
	open.lpstrElementName = drive;

	//flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | MCI_OPEN_SHAREABLE;
	flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID;

	if (!mciSendCommand(0, MCI_OPEN, flags, (DWORD) &open)) 
	{
		mciSendCommand(open.wDeviceID, MCI_SET, (bOpenDrive) ? MCI_SET_DOOR_OPEN : MCI_SET_DOOR_CLOSED, 0);
		mciSendCommand(open.wDeviceID, MCI_CLOSE, MCI_WAIT, 0);
	}
}

int ProcessCommandLine(LPSTR lpCmdLine, BOOL *cmd, char *drive)
{
	int argc = 1;
	int i;
	char **argv;
	static char seps[] = " ";
	char *token;

	if (*lpCmdLine == '\0')
	{
		fHelp = TRUE;

		return 0;
	}

	strlwr(lpCmdLine);

	token = lpCmdLine;

	// Find out the number of tokens in the string
	if (*token == ' ')
	{
		while (*token++ == ' ')		// Trim left
			;
	}

	if (*token != '\0')
	{
		i = lstrlen(lpCmdLine);

		while (lpCmdLine[--i] == ' ')	// Trim right
		{
			lpCmdLine[i] = '\0';
		}
	}

	while (*token != '\0')
	{
		if (*token == ' ')
		{
			argc++;
		}

		while (*token++ == ' ')
			;
	}

	argc++;

	if ((argv = (char **) malloc((argc + 1) * sizeof(char *))) == NULL)
	{
		return 1;
	}

	argv[0] = "cdrom";
	argv[1] = strtok(lpCmdLine, seps);
	
	for (i = 2; i < argc; i++)
	{
		argv[i] = strtok(NULL, seps);
	}

	argv[argc] = '\0';

	if (argc >= 3)
	{
		if (!lstrcmp(argv[1], "open"))
			*cmd = TRUE;
		else if (!lstrcmp(argv[1], "close"))
			*cmd = FALSE;
		else
		{
			fHelp = TRUE;

			return 0;
		}

		wsprintf(drive, "%c:", argv[2][0]);
	}
	else
	{
		fHelp = TRUE;

		return 0;
	}

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Slovakia Slovakia
Wink | ;-)

Comments and Discussions