Click here to Skip to main content
Licence 
First Posted 27 Mar 2007
Views 30,457
Downloads 234
Bookmarked 14 times

Telling The Difference Between CD and DVD Drives

By | 27 Mar 2007 | Article
Provides code to determine if a drive is a CD or DVD drive.

Screenshot - CDorDV1.jpg

Introduction

The code included with this article demonstrates a way to determine whether an installed drive is a CD or DVD drive. The return from GetDriveType() only tells you that the drive is a DRIVE_CDROM, and it returns this for both CD and DVD (so far as I could tell). The additional code included here will help you determine the difference between a CD and DVD drive.

Background

I needed to know this information for a project a while back, but I couldn't find anything that encapsulated it using MFC. So, I wrote this small program to test out some ideas and ended up with the code included with this article.

Using the code

There is nothing fancy in this code. Users can simply cut and paste the code needed from this sample into their own projects.

Here is the part that does the work in question:

//
CDORDVD CCDOrDVDDriveDlg::GetMediaType(TCHAR cDrive)
{
    CString cs;
    cs.Format(_T("\\\\.\\%c:"),cDrive);
    HANDLE hDrive = CreateFile(cs, GENERIC_READ, FILE_SHARE_READ, 
                               NULL, OPEN_EXISTING, 0, NULL);

    if(hDrive == INVALID_HANDLE_VALUE || GetLastError() != NO_ERROR)
        return CDDRIVE_UNKNOWN;

    UCHAR buffer[2048]; // Must be big enough hold DEVICE_MEDIA_INFO
    ULONG returned;
    BOOL bStatus = DeviceIoControl(hDrive, 
                   IOCTL_STORAGE_GET_MEDIA_TYPES_EX,NULL, 0, 
                   &buffer, 2048, &returned, NULL);

    // Close handle. This should work, but if it can't close it something may
    // have gone wrong in the IOCTL call.
    if (!bStatus || !CloseHandle(hDrive))
        return CDDRIVE_UNKNOWN;

    PGET_MEDIA_TYPES pMediaTypes = (PGET_MEDIA_TYPES) buffer;
    if(pMediaTypes->DeviceType == FILE_DEVICE_CD_ROM)
        return CDDRIVE_CD;
    else if(pMediaTypes->DeviceType == FILE_DEVICE_DVD)
        return CDDRIVE_DVD;

    return CDDRIVE_UNKNOWN;
}

Points of interest

I tried several ideas while creating this small snippet of code. I needed it to work on any version of Windows from WinNT. I needed to know the difference because the user of my program can have either kind of media and I wanted to display the most logical drive for them to use.

History

  • Version 1.0 - March 28, 2007.
  • Version 1.1 - March 29, 2007 - fixed project file.

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

Thomas Serface

Software Developer (Senior)

United States United States

Member

I have been using VC++ with MFC since it first came out circa 1993 and I saw it demonstrated at a local Software Development show. I've been working for Rimage Corporation for around 23 years developing software to make our CD and DVD publishing hardware work.
 
When I'm not working I enjoy hanging out with my family, playing guitar, traveling, and taking my dogs for walks. My family enjoys watching Survivor on Thursday nights and we're not even embarassed by it.

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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionwhat about a CD-RW or DVD -RW Drive PinmemberVis_liner6:36 22 May '07  
QuestionPossible handle leak? PinmemberBlake Miller6:40 2 Apr '07  
AnswerRe: Possible handle leak? Pinmembertserface8:24 2 Apr '07  
GeneralRe: Possible handle leak? PinmemberNathan Lewis5:31 11 Jun '07  
QuestionBlueray and HDDVD drives ? PinmemberIntVestor6:35 29 Mar '07  
AnswerRe: Blueray and HDDVD drives ? Pinmembertserface6:40 29 Mar '07  
GeneralTranslation to another language PinmemberAnthony Slash12:07 27 Mar '07  
GeneralRe: Translation to another language Pinmembertserface12:19 27 Mar '07  
GeneralRe: Translation to another language Pinmembertserface12:36 27 Mar '07  
GeneralRe: Translation to another language PinmemberAnthony Slash15:50 27 Mar '07  
GeneralRe: Translation to another language PinmemberBalboos4:36 5 Apr '07  
It might be prudent (even wise) for you to become read-familiar with unmanaged C++.
 
It's not that I am without simpathy, as I was always cursing under by breath when the only code examples were given in VB: I never learned Basic (Fortran -> Vax Assembler-> C-> C++. . .).
 
One reason I've 'bought in' to .NET is that, finally, the same calls are useable in all languages*. VB.NET snippets are now pretty much readable.
 
You're familar, it would seem, with C#. C++ isn't so different, although it does keep you in touch with what you're actually doing (at the price of some extra typing). Think of it as a visit to your ancestoral home.
 
Balboos
 
*(occasionally requiring 15 .Missing arguments, and no documentation, but what the hell, right?)
 
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120529.1 | Last Updated 27 Mar 2007
Article Copyright 2007 by Thomas Serface
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid