Bob Moore's Coding Tips

54. How do I find out the drive letters in use on a given system?

This one's easy. You need to use the cunningly-named GetLogicalDriveStrings API.

int iStrsLen;
char smzDrives [(3+1)*26+1];
char szDrive [3+1];
int i;

iStrsLen = GetLogicalDriveStrings (sizeof(smzDrives), smzDrives);

if (iStrsLen == 0)
{
   // Handle error
}
else
{
   if (iStrsLen > sizeof(smzDrives))
   {
      // Buffer not big enough... eh? - I provided space for all 26 !?!
   }
   else
   {
      // Parse the returned multi-string array.

      i = 0;
      while (smzDrives[i])
      {
         strcpy (szDrive, &(smzDrives[i]));

         // szDrive now contains the root folder of this drive. I
         // put this in a listbox here:

         m_ctrlListbox.AddString (szDrive);
         while (smzDrives[i]) i++;
         i++;
      }
   }
}