Click here to Skip to main content
13,596,474 members
Click here to Skip to main content
Add your own
alternative version


47 bookmarked
Posted 19 Aug 2005

How to access the CD-ROM

, 19 Aug 2005
Rate this:
Please Sign up or sign in to vote.
How to access the CD-ROM by calling Windows APIs.


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:

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.

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:

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

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

// for every drive check whether it is a CD ROM or not
for(int i=0;i< logDrives.Length;i++)
        s+="Your CD ROM is on drive : " + 
        result = api.GetVolumeInformation(logDrives[i].ToString(),
            s+="there is NO CD in ur CD ROM";
            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.

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.


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


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


About the Author

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

You may also be interested in...

Comments and Discussions

Generalis there any other message like "set CDAudio door closed" Pin
sa_keles12-Dec-06 1:55
membersa_keles12-Dec-06 1:55 
GeneralRe: is there any other message like "set CDAudio door closed" Pin
MinaFawzi13-Dec-06 1:49
memberMinaFawzi13-Dec-06 1:49 
GeneralRe: is there any other message like "set CDAudio door closed" Pin
sa_keles13-Dec-06 2:44
membersa_keles13-Dec-06 2:44 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180621.3 | Last Updated 19 Aug 2005
Article Copyright 2005 by MinaFawzi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid