User Tools

Site Tools


available_drives

List all available drives

This code snippet is originally from the former WinAPI site www.winapi.net

Sometimes you need to know about all logical drives. The WinAPI gives us for that the function GetLogicalDrives(). It returns a bit mask which has to be analysed. Here is an example how:

#include <windows.h>
#include <tchar.h>
#include <shlwapi.h>
#include <stdio.h>
 
#ifndef BITSPERBYTE
#define BITSPERBYTE 8
#endif
 
#define IS_BIT(val, bit) ((val) & (1 << (bit)))
 
int _tmain(void)
{
   DWORD dwLogicalDrives, x;
   TCHAR szRoot[32];
 
   dwLogicalDrives = GetLogicalDrives();
 
   for(x = 0; x < (sizeof(dwLogicalDrives) * BITSPERBYTE); x++)
   {
      if(IS_BIT(dwLogicalDrives, x))
      {
         PathBuildRoot(szRoot, x);
         _tprintf(TEXT("%s - DriveType: 0x%08X\n"), szRoot, GetDriveType(szRoot));
      }
   }
   return(0);
}
available_drives.txt · Last modified: 2011/12/12 06:55 by crl