Bob Moore's Coding Tips

Tip 71 : How do I enumerate the drives on my hosting machine?

Very simply. The APIs you need are GetLogicalDriveStrings and GetDriveType. The code below is a command line program which enumerates all the drives on the host machine and lists them together with their types.

#include "stdafx.h"
#include
"windows.h"

int
_tmain(int argc, _TCHAR* argv[])
{
   TCHAR atchDrives [150];
   TCHAR atchResult [50];
   TCHAR * ptchDrive = NULL;

   if (GetLogicalDriveStrings (sizeof(atchDrives), atchDrives))
   {
      if (strlen (atchDrives) > 0)
      {
         ptchDrive = atchDrives;

         do
        
{
           
switch (GetDriveType (ptchDrive))
            {
              
case DRIVE_UNKNOWN:
                    wsprintf (atchResult,
"%s : Unknown type", ptchDrive);
                   
break;
              
case DRIVE_NO_ROOT_DIR:
                    wsprintf (atchResult,
"%s : No root directory", ptchDrive);
                   
break;
              
case DRIVE_REMOVABLE:
                    wsprintf (atchResult,
"%s : Removable", ptchDrive);
                   
break;
              
case DRIVE_FIXED:
                    wsprintf (atchResult,
"%s : Hard Disk", ptchDrive);
                   
break;
              
case DRIVE_REMOTE:
                    wsprintf (atchResult,
"%s : Remote (network) drive", ptchDrive);
                   
break;
              
case DRIVE_CDROM:
                    wsprintf (atchResult,
"%s : CD/DVD", ptchDrive);
                   
break;
              
case DRIVE_RAMDISK:
                    wsprintf (atchResult,
"%s : RAMDrive", ptchDrive);
                   
break;
              
default:
                    wsprintf (atchResult,
"%s : ERROR", ptchDrive);
                   
break;
            }
            printf (atchResult);
            printf (
"\n");

           
do ++ptchDrive;
           
while (*ptchDrive);
            ++ptchDrive;

         }
while (strlen (ptchDrive));
      }
   }
  
return 0;
}