summaryrefslogtreecommitdiff
path: root/eject.c
blob: c89624a5fdaac9b8d908bc171f5fb0c5a6311fd6 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// 
// AUTHOR
// N. Nielsen
//
// LICENSE
// This software is in the public domain.
//
// The software is provided "as is", without warranty of any kind,
// express or implied, including but not limited to the warranties
// of merchantability, fitness for a particular purpose, and
// noninfringement. In no event shall the author(s) be liable for any
// claim, damages, or other liability, whether in an action of
// contract, tort, or otherwise, arising from, out of, or in connection
// with the software or the use or other dealings in the software.
// 
// SUPPORT
// Send bug reports to: <nielsen@memberwebs.com>
// 


#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers

#include <windows.h>
#include <tchar.h>
#include <winioctl.h>

// Get first CD-ROM Drive letter
TCHAR GetCDROM()
{
	TCHAR buff[4];
	TCHAR i;

	_tcscpy(buff, "X:\\");

	for(i = _T('A'); i <= _T('Z'); i++)
	{
		buff[0] = i;
		if(GetDriveType(buff) == DRIVE_CDROM)
			return i;
	}

	return 0;
}

int Usage()
{
	MessageBox(NULL, _T("usage: eject [-load] [drive] ..."), _T("Eject Usage"), MB_OK | MB_ICONINFORMATION);
	return 2;
}

int DoEject(TCHAR drive, int mode)
{
	TCHAR buff[7];
	HANDLE hDevice;
	
	_tcscpy(buff, "\\\\.\\X:");

	if(drive == 0)
	{
		// Create a CD-ROM drive string
		buff[4] = GetCDROM(drive);
	}
	else
	{
		buff[4] = drive;
	}

	if(buff[4] == 0)
	{
		MessageBox(NULL, _T("No CD-ROM drives found on system."), _T("Eject"), MB_OK | MB_ICONSTOP);
		return 1;
	}

	// Open the CD device
	hDevice = CreateFile(buff, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 
						 NULL, OPEN_EXISTING, 0, NULL);
    
	if(hDevice != INVALID_HANDLE_VALUE)
	{
		DWORD code = IOCTL_STORAGE_EJECT_MEDIA;
		DWORD bytesRet = 0;

		// If specified with the '-load' parameter then load 
		// the drive rather than eject
		if(mode)
			code = IOCTL_STORAGE_LOAD_MEDIA;

		DeviceIoControl(hDevice, code, NULL, 0, NULL, 0, &bytesRet, NULL);
	}

	CloseHandle(hDevice);

	return 0;
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	int mode = 0;
	int spec = 0;
	int i;

	for(i = 0; i < __argc; i++)
	{
		/* Specifying a parameter */
		if(__argv[i][0] == '-')
		{
			char* arg = __argv[i] + 1;

			if(strstr(arg, "load"))
			{
				mode = 1;
				continue;
			}

			else if(strstr(arg, "eject"))
			{
				mode = 0;
				continue;
			}

			else if(strstr(arg, "h"))
			{
				return Usage();
			}
		}

		/* Specifying a drive */
		if(strlen(__argv[i]) == 1)
		{
			char drive = tolower(__argv[i][0]);
			if(drive >= 'a' && drive <= 'z')
			{
				if(!DoEject(drive, mode))
					return 1;

				spec = 1;
				continue;
			}
		}

		return Usage();
	}

	if(!spec)
	{
		if(!DoEject(0, mode))
			return 1;
	}
	
	return 0;
}