Skip to main content
Email Password   helpLost your password?

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:

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++)
{
    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.

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralMultiple Drives?? Pin
SP_021
3:50 16 Jul '07  
Generalis there any other message like "set CDAudio door closed" Pin
sa_keles
2:55 12 Dec '06  
GeneralRe: is there any other message like "set CDAudio door closed" Pin
MinaFawzi
2:49 13 Dec '06  
GeneralRe: is there any other message like "set CDAudio door closed" Pin
sa_keles
3:44 13 Dec '06  
QuestionOne question [modified] Pin
janek84
10:18 24 Aug '06  
GeneralHow to get the status of CR-ROM drive (open or closed)? Pin
dragomir
7:49 11 May '06  
GeneralRe: How to get the status of CR-ROM drive (open or closed)? Pin
Niels Penneman
1:43 11 Jul '06  
GeneralSome issues Pin
luci79rom
22:02 28 Mar '06  
GeneralProbably needs threads Pin
gxdata
18:02 28 Aug '05  


Last Updated 19 Aug 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009