How to Eject CD tray using Win32 API

//Namespace references
using System.Text;
using System.Runtime.InteropServices;

//Win32 API Call
[DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi)]
protected static extern int mciSendString(string lpstrCommand,StringBuilder lpstrReturnString,int uReturnLength,IntPtr hwndCallback);

//Method
#region Open/Close CD Tray
///


/// method to open or close the CD tray based on the
/// boolean value passed
///

///
///
public bool ProcessCDTray(bool open)
{
int ret = 0;
//do a switch of the value passed
switch (open)
{
case true: //true = open the cd
ret = mciSendString("set cdaudio door open", null, 0, IntPtr.Zero);
return true;
break;
case false: //false = close the tray
ret = mciSendString("set cdaudio door closed", null, 0, IntPtr.Zero);
return true;
break;
default:
ret = mciSendString("set cdaudio door open", null, 0, IntPtr.Zero);
return true;
break;
}
}
#endregion


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: