Software opening the CD tray

I want to create a small C # program on Windows that would open the language of the CD-ROM drive - remove the CD-ROM, if you have one. I would like to know where to start.

+7
c #
source share
1 answer

Opening and closing a software disc in C # is not that difficult thanks to a useful API function called mciSendStringA.

First you will need to define the function that opens the disc tray:

[DllImport("winmm.dll", EntryPoint = "mciSendString")] public static extern int mciSendStringA(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback); 

If the code above does not compile, try adding the following C # line to the very top of your source code:

 using System.Runtime.InteropServices; 

Disc opening

To open the drive, you need to send two lines of commands using mciSendStringA. The first one names the desired drive. The second command will actually open the disc tray:

 mciSendStringA("open " + driveLetter + ": type CDaudio alias drive" + driveLetter, returnString, 0, 0); mciSendStringA("set drive" + driveLetter + " door open", returnString, 0, 0); 

Disk closing

To close the drive, you need to send two lines of commands again. The first will be the same. The second command will now close the disc tray:

 mciSendStringA("open " + driveLetter + ": type CDaudio alias drive" + driveLetter, returnString, 0, 0); mciSendStringA("set drive" + driveLetter + " door closed", returnString, 0, 0); 
+14
source share

All Articles