How To Get All CD Drives

In the last post, I described how to open/close cd tray. Some of you might have raised your eye brows as there is no description how to find the drive letter of cd drive. Here is a sample program which prints all cd drive letters in a system.


#include <iostream>
#include <string>
#include "wtypes.h"
#include "basetsd.h"
#include "winbase.h"
using namespace std;

int main()
{ 
   // Iterate through all the 26 drives possible  
   for (char Drive = 'a'; Drive <= 'z'; ++Drive)
   {
      // Format drive like "c:\"
      string StrDrive = "";
      StrDrive.push_back(Drive);
      StrDrive.append(":\\");

      // Print the drive letter if it is a cd drive
      if (DRIVE_CDROM == GetDriveType(StrDrive.c_str()))
      {
         cout << Drive << '\n';
      }
   }

   return 0;
}

Some Background Info

The GetDriveType() function can give the type of a drive. The possible values are given below.

  • DRIVE_UNKNOWN = 0 : The drive type can not be determined.
  • DRIVE_NO_ROOT_DIR = 1 :The root path is invalid; for example, there is no volume is mounted at the path.
  • DRIVE_REMOVABLE = 2 : The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader.
  • DRIVE_FIXED = 3 : The drive has fixed media; for example, a hard drive or flash drive.
  • DRIVE_REMOTE = 4 : The drive is a remote (network) drive.
  • DRIVE_CDROM = 5 : The drive is a CD-ROM drive.
  • DRIVE_RAMDISK = 6 : The drive is a RAM disk.

The GetDriveType() function expects an argument of type LPCTSTR, which can specify the root path of the drive with a trailing slash (as in c:\). if the argument is NULL, it will take the root of the current directory.

GetDriveType() will be expanded to GetDriveTypeW (for UNIODE) and GetDriveTypeA (for ANSI). The macro _T() can automatically expand the argument to multibyte character string if unicode is defined.

One Response to “How To Get All CD Drives”

  1. How To Open/Close CD Tray « My Experiments with C++ Says:

    [...] Click to see how to get CD drives in a system. View Poll Possibly related posts: (automatically generated)CD DRIVE FUNPossessed: An I-Have-An-Exam-Tomorrow-But-I’m-Too-Cocky-To-Review Post [...]

Leave a Reply