opening and closing CD drive on damand

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <tchar.h>
#include <iostream>
#include <windows.h>
#include <mmsystem.h>

using namespace std;

#pragma comment(lib, "winmm")

void ControlCdTray(TCHAR drive, DWORD command)
{
	MCIERROR mciError = 0;

	DWORD mciFlags = MCI_WAIT | MCI_OPEN_SHAREABLE |
		MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | MCI_OPEN_ELEMENT;

	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);


	MCI_SET_PARMS mciSetParms = { 0 };
	mciFlags = MCI_WAIT | command; 
	mciError = mciSendCommand(mciOpenParms.wDeviceID,
		MCI_SET, mciFlags, (DWORD_PTR)&mciSetParms);


	mciFlags = MCI_WAIT;
	MCI_GENERIC_PARMS mciGenericParms = { 0 };
	mciError = mciSendCommand(mciOpenParms.wDeviceID,
		MCI_CLOSE, mciFlags, (DWORD_PTR)&mciGenericParms);
}



void EjectCdTray(TCHAR drive)
{
	ControlCdTray(drive, MCI_SET_DOOR_OPEN);
}

void CloseCdTray(TCHAR drive)
{
	ControlCdTray(drive, MCI_SET_DOOR_CLOSED);
}
int main()
{
	cout << "Press E to eject the CD drive" << endl;
	while (true)
	{
		if (GetAsyncKeyState(0x45))
		{
			cout << "Ejecting CD drive" << endl;
			EjectCdTray(TEXT('E'));
			cout << "Press R to retract the CD drive" << endl;
		}
		if (GetAsyncKeyState(0x52))
		{
			cout << "Retracting CD drive" << endl;
			CloseCdTray(TEXT('E'));
			cout << "Press E to eject the CD drive" << endl;
		}
	}
}


Last edited on
1
2
3
4
5
if (GetAsyncKeyState('E') & 0x8000)

//...

if (GetAsyncKeyState('R') & 0x8000)


Fixed those lines for you
You can do either way you can use the code for the VK or you can use the letter.
Topic archived. No new replies allowed.