Click here to Skip to main content
Click here to Skip to main content

How to access the CD-ROM

By , 19 Aug 2005
 

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.

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

About the Author

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalentry pointsmemberphowarso20 May '10 - 0:51 
hello!
i wanna know some entry points of window dll files.
eg. mciSendString of winmm.dll
could u help me pls
how i know those entry points of window dll files?
and where place can i know?
Could u help me any link or some E-Book about those?
i'll thz u very much........ Laugh | :laugh: Laugh | :laugh: Laugh | :laugh:
QuestionMultiple Drives??memberSP_02116 Jul '07 - 2:50 
Great work-thanks! But what if you have more than one drive, other than the master drive? For example, if I had 2 Drives-a DVD Reader, and a DVD Writer-and I just wanted to open the DVD Writer-or Visa Versa?
Generalis there any other message like "set CDAudio door closed"membersa_keles12 Dec '06 - 1:55 
hi Smile | :)
we sent "set CDAudio door closed" message to cd rom and it closed
Form1.mciSendString("set CDAudio door closed", null, 0, 0);
 
is there any other message like "set CDAudio door closed" and "set CDAudio door open"
 
openup your heart and let the sun shine in Smile | :)

GeneralRe: is there any other message like "set CDAudio door closed"memberMinaFawzi13 Dec '06 - 1:49 
check this link for the complete list
 
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_set.asp
 
MiNa

GeneralRe: is there any other message like "set CDAudio door closed"membersa_keles13 Dec '06 - 2:44 
thanks MinaFawzi Smile | :)
 
openup your heart and let the sun shine in Smile | :)

QuestionOne question [modified]memberjanek8424 Aug '06 - 9:18 
Hi
 
Nice article Smile | :) but what about if I have more then one cd-rom drive and sometimes I want to eject the first one and sometimes the second one ?

QuestionHow to get the status of CR-ROM drive (open or closed)?memberdragomir11 May '06 - 6:49 
Hello,
 
Great article, but do you know how to get the status of CR-ROM drive (open or closed)?
 
TIA Smile | :)
AnswerRe: How to get the status of CR-ROM drive (open or closed)?memberNiels Penneman11 Jul '06 - 0:43 
Windows doesn't even know that, I think. Go to My Computer, normally you can open and close your CD drive from there. Now open it manually and try a software eject. Nothing will happen, so I guess Windows doesn't know.
 
Another remark:
 
[DllImport("winmm.dll", EntryPoint = "mciSendStringA")]
private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
 
api.mciSendString("set CDAudio door open",null, 127, 0);
 
The docs say:
 
MCIERROR mciSendString(
LPCTSTR lpszCommand,
LPTSTR lpszReturnString,
UINT cchReturn,
HANDLE hwndCallback
);
 
I think int is definitely not the same as UINT; besides cchReturn is defined as the length of the return buffer pointed to by lpszReturnString, which is null in this case. What's the point in specifying 127 for cchReturn? And IntPtr should be used for Handles afaik.
 
greetz
Niels Penneman

GeneralSome issuesmemberluci79rom28 Mar '06 - 21:02 
I used parts of your code and I had some problems geting the serial number of the disc, it always came zero. I solved the problem using pointer like in the definition for the GetVolumeInformation.

GeneralProbably needs threadsmembergxdata28 Aug '05 - 17:02 
First, let me say this this works fine for me, up to a point. My criticisms are fairly minor.
 
But on occasions, the application errors - it can't be closed down. It's cycling through my drives, determining what they are - and for a floppy and even just one CD/DVD, this can cause the app to be "tied up" for a few seconds. So, some of the tests need to be on separate threads and some interprocess messaging used to minimize these problems.
 
As written, it's not a great example because a real-life application might eject the CD and await the user inserting another. Then, the CD label would be reported again.

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 19 Aug 2005
Article Copyright 2005 by MinaFawzi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid