c++hardwaredetectionwmi-queryoptical-drive

DVD or CD Player?


I want to know if my player is a DVD or CD Player, etc ...

I tried :

SELECT Drive, MediaType, Caption FROM Win32_CDROMDrive

MediaType doesn't work on XP, and not very well on Seven.

I tried on a computer (with Windows 7) with 1 player (Cd Writer/DVD-ROM) in MediaType I found Cd Writer.

Second solution :

I search in "Caption" if I find "DVD"

I tried a software, (SIW - System Information for Windows) and in my Player properties :

Capabilities :

CD Reader : CD ROM, CD R, CD RW

CD Writer : CD R, CD RW

DVD Reader : DVD ROM, DVD R, DVD RW, DVD RAM

DVD Writer : No

SMART Support : No

So, I want to know : with a WMI query (or other solution, I use C++), can I have the same informations or not?

It would be awesome if I could! If I can't, I just keep my "String parsing".


Solution

  • to determine if a drive is DVD or CDROM , you can use the the DeviceIoControl function with the IOCTL_STORAGE_GET_MEDIA_TYPES_EX control code and then check the value of the DeviceType field of the GET_MEDIA_TYPES structure.

    Try this sample

    #include "stdafx.h"
    #include <windows.h>
    #include <winioctl.h>
    #include <stdio.h>
    #include <iostream>
    using namespace std;
    
    #define wszDrive L"\\\\.\\D:"
    int wmain(int argc, wchar_t *argv[])
    {
      BOOL bResult;     
      HANDLE hDevice = INVALID_HANDLE_VALUE;  // handle to the drive to be examined 
      hDevice = CreateFileW(wszDrive,          // drive to open
                            GENERIC_READ,             
                            FILE_SHARE_READ | // share mode
                            FILE_SHARE_WRITE, 
                            NULL,             // default security attributes
                            OPEN_EXISTING,    // disposition
                            0,                // file attributes
                            NULL);            // do not copy file attributes
    
      if (hDevice == INVALID_HANDLE_VALUE)    // cannot open the drive
      {
        return (FALSE);
      }
    
      UCHAR lpOutBuffer[2048];
      DWORD nOutBufferSize = sizeof(lpOutBuffer);
      ULONG lpBytesReturned;
    
      bResult = DeviceIoControl(hDevice,                       // device to be queried
                                IOCTL_STORAGE_GET_MEDIA_TYPES_EX, // operation to perform
                                NULL, 0,                       // no input buffer
                                &lpOutBuffer, nOutBufferSize, &lpBytesReturned,
                                NULL);       
    
      CloseHandle(hDevice);  
      PGET_MEDIA_TYPES pMediaTypes = (PGET_MEDIA_TYPES) lpOutBuffer;
      if (bResult) 
      {
          if (pMediaTypes->DeviceType==FILE_DEVICE_DVD)       
          {
              wprintf(L"DVD\n");
          }
          else
          if (pMediaTypes->DeviceType==FILE_DEVICE_CD_ROM)    
          {
              wprintf(L"CDROM\n");
          }
      } 
      else 
      {
        wprintf (L"Failed. Error %ld.\n", GetLastError ());
      }
      cin.get();
      return ((int)bResult);
    }