ejecting cdrom

This is a discussion on ejecting cdrom within the C++ Programming forums, part of the General Programming Boards category; is there any tutorial that explains how eject cdrom? can you help me thanks...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    106

    ejecting cdrom

    is there any tutorial that explains how eject cdrom? can you help me thanks

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    The mciSendString is a good way to open a cdrom.......you can find examples on this board if you look..... dont forget the winmm.lib when linking...

    Also the DeviceIoControl() API can open these drives....

    Here's something I was messing with a while back..its not as good as using mciSendString(), but it kept me entertained for a little while .........it works on Win2000 and probably therefore XP, but I dont think it will work on Win98.....anyway...it uses an API to give a bitmask of all drives available, and then tests them to see if they are CDROMs....then it opens a handle to the drive (I think this is where it would fail with Win98) and uses DeviceIoControl() to open the drive. Its not very good, but I thought I'd post it anyway ...have a look

    Code:
    #include <windows.h>
    #include <winioctl.h>
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    
    int main(void){
    
    	DWORD dwDriveMask,
    		  dwMaskPos = 0,
    		  dwCountByte;
    	string str,
    		   temp;
    	char ch;
    	HANDLE hDrive;
    
    	str = "";
    
    	dwDriveMask =  GetLogicalDrives();
    
    	if(!dwDriveMask){
    		cout << "Error";
    		return 1;
    	}
    
    	while(dwDriveMask > 0 
    		&& dwMaskPos < 26){
    
    		if(dwDriveMask & 1){
    			
    			temp += (char)dwMaskPos + 65;
    			temp += ":\\";
    			
    			if(GetDriveType(temp.c_str()) == DRIVE_CDROM){
    				str += (char)dwMaskPos + 65;
    			}
    			temp = "";
    		}
    		dwDriveMask >>= 1;
    		dwMaskPos++;
    	}
    
    	cout << "Available CD drives" << endl;
    
    	for(int i = 0; i < str.length();i++)
    		cout << str[i] << " ";
    
    	while (1){
    	cout << endl << "Choose drive letter";
    	cout << " to open that drive's tray, 0 to exit" << endl;
    
    	cin >> ch;
    
    	if(ch == '0')
    		break;
    	temp = "\\\\.\\";
    	temp += ch;
    	temp += ":";
    
    	
    
    	hDrive = CreateFile(temp.c_str(),GENERIC_READ,
    		NULL,NULL,OPEN_EXISTING,NULL,NULL);
    
    	if(hDrive==INVALID_HANDLE_VALUE){
    		cout << "Error";
    		return 1;
    	}
    
    	DeviceIoControl(hDrive,IOCTL_STORAGE_EJECT_MEDIA,NULL,
    		NULL,NULL,NULL,&dwCountByte,NULL);
    
    	CloseHandle(hDrive);
    	temp = "";
    
    	}
    
    	return 0;
    }

  3. #3
    Shadow12345
    Guest
    That's sweet!! haha!! Why is it the stupid stuff that makes me so damn happy?

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    I dunno, but bumping threads makes me unhappy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from CDROM
    By Eibro in forum Tech Board
    Replies: 4
    Last Post: 03-20-2003, 04:48 PM
  2. Basic linux question (cdrom)
    By GanglyLamb in forum Tech Board
    Replies: 8
    Last Post: 02-19-2003, 01:33 PM
  3. ?polling? the cdrom drive.
    By un_reg in forum Linux Programming
    Replies: 5
    Last Post: 11-08-2002, 11:03 AM
  4. How to format my Pc?
    By CaseY in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 01-22-2002, 12:18 PM
  5. CDROM Copier
    By _Wandering_ in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2002, 05:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21