Skip to main content
Email Password   helpLost your password?

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Questionwhat about a CD-RW or DVD -RW Drive Pin
Vis_liner
7:36 22 May '07  
GeneralPossible handle leak? Pin
Blake Miller
7:40 2 Apr '07  
GeneralRe: Possible handle leak? Pin
tserface
9:24 2 Apr '07  
GeneralRe: Possible handle leak? Pin
Nathan Lewis
6:31 11 Jun '07  
QuestionBlueray and HDDVD drives ? Pin
IntVestor
7:35 29 Mar '07  
AnswerRe: Blueray and HDDVD drives ? Pin
tserface
7:40 29 Mar '07  
GeneralTranslation to another language Pin
Anthony Slash
13:07 27 Mar '07  
GeneralRe: Translation to another language Pin
tserface
13:19 27 Mar '07  
GeneralRe: Translation to another language Pin
tserface
13:36 27 Mar '07  
GeneralRe: Translation to another language Pin
Anthony Slash
16:50 27 Mar '07  
GeneralRe: Translation to another language Pin
Balboos
5:36 5 Apr '07  


Last Updated 27 Mar 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009