Attempting to open CD tray

c++, mci, winapi

Solution

If you have multiple CD-Drives you should use the following code:

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

int _tmain() 
{ 
   DWORD dwBytes; 
   HANDLE hCdRom = CreateFile(_T("\\\\.\\M:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); 
   if (hCdRom == INVALID_HANDLE_VALUE) 
   { 
     _tprintf(_T("Error: %x"), GetLastError()); 
     return 1; 
   } 

   // Open the door:  
   DeviceIoControl(hCdRom, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, &dwBytes, NULL); 

   Sleep(1000); 

   // Close the door:  
   DeviceIoControl(hCdRom, IOCTL_STORAGE_LOAD_MEDIA, NULL, 0, NULL, 0, &dwBytes, NULL); 

   CloseHandle(hCdRom); 
} 

Problem

I'm trying to open and close the CD tray of my computer using a piece of code. I have been using MCI commands and have included `winmm.lib` in the additional dependencies of my project configuration. I've included `windows.h` and `mmsystem.h` as well. The code I'm using is as follows: ``` mciSendCommand(0, MCI_SET, MCI_SET_DOOR_OPEN, NULL); mciSendCommand(1, MCI_SET, MCI_SET_DOOR_CLOSED, NULL); ``` The code builds and runs fine, there's just no CD tray action going on! Can anyone suggest how I need to adapt this?

Original source