Open and Close CD Drive using C#


To open and close the cd drive

To open and close the cd drive, mciSendString function is used here.
The function mciSendString is in winmm.dll. It is explicitly imported using DllImport attribute.
mciSendString function sends a command string to Media Control Interface (MCI) device.

It has 4 parameters:

1. lpstrCommand - Command string to MCI device
set cdaudio door open - it opens the cd drive since it triggers the MCI device to push the cd drive.
set cdaudio door closed - it closes the cd drive since it triggers the MCI device to pull the cd drive inside.

2. lpstrReturnString - used to receive the return information
Null - no need of return value

3. uReturnLength - size of the return information
0 - no return information

4. hwndCallback - handles the callback if we need any notification
0 - no callbacks


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("winmm.dll", EntryPoint = "mciSendStringA")]
public static extern void mciSendString(string lpstrCommand,
string lpstrReturnString, long uReturnLength, long hwndCallback);
string rt = null;
private void button1_Click(object sender, EventArgs e)
{
mciSendString("set CDAudio door open", rt, 0, 0);
}
private void button2_Click(object sender, EventArgs e)
{
mciSendString("set CDAudio door closed", rt, 0, 0);
}
}
}


Comments

Author: nirav14 Apr 2010 Member Level: Gold   Points : 0

GOOD ONE...

Author: Shanthi M16 Apr 2010 Member Level: Gold   Points : 0

Thank you nirav

Author: sundar18 Jan 2012 Member Level: Bronze   Points : 0

Nice



  • 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: