Click here to Skip to main content
6,596,602 members and growing! (23,126 online)
Email Password   helpLost your password?
General Reading » Hardware & System » Hardware     Intermediate

How to access the CD-ROM

By MinaFawzi

How to access the CD-ROM by calling Windows APIs.
C#, Windows, .NET 1.1VS.NET2003, Dev
Posted:19 Aug 2005
Views:37,418
Bookmarked:39 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
17 votes for this article.
Popularity: 4.35 Rating: 3.53 out of 5
3 votes, 17.6%
1
1 vote, 5.9%
2
3 votes, 17.6%
3
5 votes, 29.4%
4
5 votes, 29.4%
5

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


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

Other popular Hardware & System articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
GeneralMultiple Drives?? PinmemberSP_0213:50 16 Jul '07  
Generalis there any other message like "set CDAudio door closed" Pinmembersa_keles2:55 12 Dec '06  
GeneralRe: is there any other message like "set CDAudio door closed" PinmemberMinaFawzi2:49 13 Dec '06  
GeneralRe: is there any other message like "set CDAudio door closed" Pinmembersa_keles3:44 13 Dec '06  
QuestionOne question [modified] Pinmemberjanek8410:18 24 Aug '06  
GeneralHow to get the status of CR-ROM drive (open or closed)? Pinmemberdragomir7:49 11 May '06  
GeneralRe: How to get the status of CR-ROM drive (open or closed)? PinmemberNiels Penneman1:43 11 Jul '06  
GeneralSome issues Pinmemberluci79rom22:02 28 Mar '06  
GeneralProbably needs threads Pinmembergxdata18:02 28 Aug '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Aug 2005
Editor: Smitha Vijayan
Copyright 2005 by MinaFawzi
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project