Thread: Detect CD drive
View Single Post
Old 06-08-2005, 05:42 PM   #1 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 734
DJMaze is on a distinguished road
Detect CD drive

i have small testing script to eject/load a cd drive.
Code:
#include "stdafx.h"
#include "windows.h"
#include "Winioctl.h"
#include "stdlib.h"

int main(int argc, char* argv[])
{
	HANDLE drive_file;
	BOOL   success;
	DWORD  temp;
        
	char drive_letter[7] = "\\\\.\\D:";

	// Open Target Drive
	drive_file = CreateFile(drive_letter, GENERIC_READ, 
                                        FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 
                                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

	if (drive_file != INVALID_HANDLE_VALUE) {
		while (success) {
			// Eject or Load the CD
			success = DeviceIoControl(drive_file, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, &temp, NULL);
			if (success) {
				Sleep(2000);
				success = DeviceIoControl(drive_file, IOCTL_STORAGE_LOAD_MEDIA, NULL, 0, NULL, 0, &temp, NULL);
				if (success) Sleep(2000);
			}
		}
	}
	return 0;
}
Shure i could walk thru D to Z or use GetLogicalDrives()/GetLogicalDriveStrings() and then GetDriveType() == DRIVE_CDROM

But isn't there a easier way ?
DJMaze is offline   Reply With Quote