Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Article

How to access the CD-ROM

Rate me:
Please Sign up or sign in to vote.
3.53/5 (17 votes)
19 Aug 20051 min read 76.4K   744   46   10
How to access the CD-ROM by calling Windows APIs.

Introduction

This is a very simple article to know how to access and get information about the CD-ROM (open, close, occupied?, drive name), and this is achieved using some Windows APIs.

Using the code

The code is very small and includes comments that explain each line. And the namespaces used are:

C#
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;  // so we can invoke APIs
using System.Text;  // to use the stringbuilder class

The only class in this program is the api class which defines the methods that will be invoked from some Windows DLLs.

C#
public class api
{
    // this api uesed to send string messege
    // to media control interface decice (mci)
    // like The cd rom
    [DllImport("winmm.dll", EntryPoint="mciSendStringA")]
    public static extern int mciSendString (string lpstrCommand, 
           string lpstrReturnString, int uReturnLength, int hwndCallback);

    // this api used to get information about
    // a drive ex: its name, seial number
    // if this function return zero means that
    // one of the information could not be retrived
    // so if it is a CD ROM drive and we can't
    // obtain its name ---> CD ROM is empty
    [DllImport("kernel32.dll", EntryPoint="GetVolumeInformationA")]
    public static extern int GetVolumeInformation (string lpRootPathName, 
           StringBuilder lpVolumeNameBuffer, int nVolumeNameSize, 
           int lpVolumeSerialNumber, int lpMaximumComponentLength, 
           int lpFileSystemFlags, string lpFileSystemNameBuffer, 
           int nFileSystemNameSize);

    //this api get the drive type (0:unknown,1:invalid path,
    //  2:removable(floppy,removabledisk),3:fixed(hard disk),
    //  4:remote(network drive),5:CDROM,6: RAM disk)
    [DllImport("kernel32.dll", EntryPoint="GetDriveTypeA")]
    public static extern int GetDriveType (string nDrive);
}

Then in the button click or whatever, you can use these functions, but first we will get all the drives in the computer using this line of code:

C#
// Directory.GetLogicalDrives return an array
// of strings that contain logical drives (c:\,d:\....)
string [] logDrives = System.IO.Directory.GetLogicalDrives();

Then for each drive in this array we will call the GetDriveType method to know which one is the CD-ROM, then use the GetVolumeInformation method to know whether there is a CD inside or not, but first we must declare and initialize the parameters used by these functions:

C#
// intialize tha parameters to the GetVolumeInformation function
            string s ="";
            StringBuilder volumeName = new StringBuilder(256);
            int srNum = new int();
            int comLen = new int();
            string sysName = "";
            int sysFlags = new int();
            int result;

Now let's call our methods:

C#
// for every drive check whether it is a CD ROM or not
for(int i=0;i< logDrives.Length;i++)
{
    if(api.GetDriveType(logDrives[i])==5)
    {
        s+="Your CD ROM is on drive : " + 
                  logDrives[i].ToString()+"\n";
        result = api.GetVolumeInformation(logDrives[i].ToString(),
                volumeName,256,srNum,comLen,sysFlags,sysName,256);
        if(result==0)
            s+="there is NO CD in ur CD ROM";
        else
        {
            s+="There is a CD inside ur CD ROM and its name is " + volumeName;
        }
    }
}

Now the last lines of code will explain how to open and close the CDROM by calling the mciSendString method.

C#
private void button1_Click(object sender, System.EventArgs e)
{
    api.mciSendString("set CDAudio door open",null, 127, 0);
}

private void button2_Click(object sender, System.EventArgs e)
{
    api.mciSendString("set CDAudio door closed",null, 127, 0);
}

That's it it. It is simple and I hope it could help.

Reference

Apiviewer 2004 is cool program that contain all the APIs with their declarations.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Egypt Egypt
Mina Fawzi
Faculty of engineering Ainshams university In CAIRO
intersted in .net technology and DirectX

Comments and Discussions

 
Generalentry points Pin
phowarso20-May-10 0:51
phowarso20-May-10 0:51 
QuestionMultiple Drives?? Pin
Seanph16-Jul-07 2:50
Seanph16-Jul-07 2:50 
Generalis there any other message like "set CDAudio door closed" Pin
sa_keles12-Dec-06 1:55
sa_keles12-Dec-06 1:55 
GeneralRe: is there any other message like "set CDAudio door closed" Pin
MinaFawzi13-Dec-06 1:49
MinaFawzi13-Dec-06 1:49 
GeneralRe: is there any other message like "set CDAudio door closed" Pin
sa_keles13-Dec-06 2:44
sa_keles13-Dec-06 2:44 
QuestionOne question [modified] Pin
janek8424-Aug-06 9:18
janek8424-Aug-06 9:18 
QuestionHow to get the status of CR-ROM drive (open or closed)? Pin
dragomir11-May-06 6:49
dragomir11-May-06 6:49 
AnswerRe: How to get the status of CR-ROM drive (open or closed)? Pin
Niels Penneman11-Jul-06 0:43
Niels Penneman11-Jul-06 0:43 
GeneralSome issues Pin
luci79rom28-Mar-06 21:02
luci79rom28-Mar-06 21:02 
GeneralProbably needs threads Pin
gxdata28-Aug-05 17:02
gxdata28-Aug-05 17:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.