summaryrefslogtreecommitdiff
path: root/eject.c
diff options
context:
space:
mode:
Diffstat (limited to 'eject.c')
-rw-r--r--eject.c157
1 files changed, 157 insertions, 0 deletions
diff --git a/eject.c b/eject.c
new file mode 100644
index 0000000..c89624a
--- /dev/null
+++ b/eject.c
@@ -0,0 +1,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;
+}
+
+
+