Search This Blog

Tuesday, May 5, 2009

How to Enumerate all Logical Drives using Win32 VC++







Intro


You should have seen Logical drives in Windows. Most commonly they are called C: D: E: etc drives. There are different type of Logical drives- Hard disk drives, CD/DVD Drives, Removable Drive, RAM Disk etc..


So, is there a way to get all of these drives installed or present on a system programatically? the Answer is very yes! Because the Win32 API's helps you to enumerate every drives presently on a system, and also to determine which type of drive is it. Ie., Hard disk, CD/DVD Drive etc...


How to Do?


The Win32 API used to enumerate all drives present on a system is  GetLogicalDrives(). This function retrieves a bitmask representing the currently available disk drives. We need to process the BitMask returned by the API to convert it to actual Drive letters. Here i will show you with a code snippet, how to enumerate all drives using GetLogicalDrives() API and process the return value to convert BitMask to appropriate Driveletter (C:, D:. E: etc..).


Code Snippet





void GetDirs()
{
DWORD dwMask = 1; // Least significant bit is A: flag
DWORD dwDrives = GetLogicalDrives ();
char strDrive[4] = {'\0'};
char strDrivex[4] = {'\0'};
int  nresult, dir;

// 26 letters in [A..Z] range
for (int i=0; i < 26; i++ )

//Logically 'AND' the Bitmask with 0x1. we get zero, if its a drive
if (dwDrives & dwMask)  
{
  wsprintfA ((LPSTR)strDrive, "%c:"'A'+i);
  printf (" %s",strDrive ); //Print out the Drive Letter
  //Just Zero filling the buffer, to prevent overwriting or junks
  for(int j = 0; j < 4; j++)
  strDrive[j] = '\0';
}
// Shift one bit position to left
dwMask <<= 1; 
}
}


Now, we have got all Logical drives (Drive Letters) Printed on our console(You can use it to display on Comboobox or other controls too, as i'm printing to console to make ease of the tutorial). 


Then, is there any way to check the types of each drives? I mean whether C: is a Hard drive or CD/DVD Drive or Removable media disk etc??


Yes!.. here comes the  GetDriveType() API. This function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive. lol.. so, you've it.. then.., how to do it?


See this snippet of code, its pretty easy.. i dont think there is a need of high level commenting ;-)


Code

//The lpdrv is the driveletter.( eg : C: or D:,  E: etc )
void FindDriveType(LPSTR lpdrv) 
{
UINT drvtype;
char szMsg[150];
//The GetDriveType() return value specifies the type of drive.
drvtype = GetDriveType(lpdrv); 
//Switching the returnvalue to check drive types
switch (drvtype)
   {
     case DRIVE_UNKNOWN:
wsprintf( szMsg, "Drive %s is of unknown type", lpdrv );
     break;
     case DRIVE_NO_ROOT_DIR:
wsprintf( szMsg, "Drive %s is invalid", lpdrv );
     break;
     case DRIVE_REMOVABLE:
wsprintf( szMsg, "Drive %s is a removable drive", lpdrv );
     break;
     case DRIVE_FIXED:
wsprintf( szMsg, "Drive %s is a hard disk", lpdrv );
     break;
     case DRIVE_REMOTE:
wsprintf( szMsg, "Drive %s is a network drive", lpdrv );
     break;
     case DRIVE_CDROM:
wsprintf( szMsg, "Drive %s is a CD-ROM drive", lpdrv );
     break;
     case DRIVE_RAMDISK:
wsprintf( szMsg, "Drive %s is a RAM disk", lpdrv );
     break;
default:
wsprintf( szMsg, "Drive %s is an unknown type",lpdrv ); 
break;
  }
}


So, Howz it?? Pretty cool and easy?... you can use this in your application to list drives and to test/evaluate which type of drive is it.. :-)


PS : Dont forget to Post your valuable feedbacks :-)
















1 comment:

  1. Can you pls show how to display the list of drives in any of the control? (like list control).

    ReplyDelete