Archive

Posts Tagged ‘GetDriveType’

Type of disk drive?

Any idea on how to know the type of a drive, i.e. whether it’s a removable drive or CD-ROM or a network drive and others. Yes! use GetDriveType( lpctszDrive ) which returns DRIVE_XXXX (drive type).

So I will write some functions just to demonstrate these features.

// Returns true if drive is invalid (hope so!)
bool IsDriveInvalid( LPCTSTR lpctszDrive )
{
  ASSERT( lpctszDrive );
  return ( GetDriveType( lpctszDrive ) == DRIVE_NO_ROOT_DIR );
}

// Returns true if we have a removable drive like floppy or USB drive
bool IsRemovableDrive( LPCTSTR lpctszDrive )
{
  ASSERT( lpctszDrive );
  return ( GetDriveType( lpctszDrive ) == DRIVE_REMOVABLE );
}

// Returns true if drive is a fixed drive like hard disk
bool IsFixedDrive( LPCTSTR lpctszDrive )
{
  ASSERT( lpctszDrive );
  return ( GetDriveType( lpctszDrive ) == DRIVE_FIXED );
}

// Returns true if drive is a network drive
bool IsNetworkDrive( LPCTSTR lpctszDrive )
{
  ASSERT( lpctszDrive );
  return ( GetDriveType( lpctszDrive ) == DRIVE_REMOTE );
}

// Returns true if drive is a cd rom
bool IsCDROMDrive( LPCTSTR lpctszDrive )
{
  ASSERT( lpctszDrive );
  return ( GetDriveType( lpctszDrive ) == DRIVE_CDROM );
}

// Returns true if drive is a ram disk
bool IsRAMDisk( LPCTSTR lpctszDrive )
{
  ASSERT( lpctszDrive );
  return ( GetDriveType( lpctszDrive ) == DRIVE_RAMDISK );
}
Advertisements

CD ROM present in CD Drive?

Good question! The easiest and working method is to call GetVolumeInformation on a CD-Drive, this function call will fail if no valid volume information is available for a drive this means CD-ROM drive should be valid one too!

So our next job is to find out whether we have a CD-Drive in our machine, for this purpose we iterate through all available drives and use GetDriveType( lpctszDrive ) == DRIVE_CDROM.

As always with me here is a function for this purpose…

bool Is_CD_ROM_Present()
{
   // Drive buffer
   TCHAR szLogicalDrives[MAX_PATH] = { 0 };

   // Get all drives in our computer, each drive is separated by NULL char
   GetLogicalDriveStrings( MAX_PATH, szLogicalDrives );

   // Value to return, provided to handle multiple CD-ROM drives,
   // if any of them contains a CD-ROM then we return true
   bool RetVal = false;

   // Temp var which helps us in looping through the drive buffer, sicne each drive is
   // NULL separated.
   LPTSTR lptszDrive = szLogicalDrives;
   while( *lptszDrive )
   {
      // Check whether it's a CD-ROM
      if( GetDriveType( lptszDrive ) == DRIVE_CDROM )
      {
         // We don't want a message box to pop up requesting us to insert a CD-ROM,
         // so set error mode accordingly
         const UINT PrevErrMode = SetErrorMode( SEM_FAILCRITICALERRORS );
         const bool RetValTemp = ( GetVolumeInformation( lptszDrive, 0, 0, 0, 0, 0, 0, 0 ) != 0 );

         // To handle multiple CD-ROM drives
         RetVal = RetVal || RetValTemp;

         // Restore error mode
         SetErrorMode( PrevErrMode );
      }// fi

      // Get next drive by moving one char ahead of "NULL" separator between drives
      lptszDrive += _tcslen( lptszDrive ) + 1;
   }// wend

   return RetVal;
}// End Is_CD_ROM_Present
%d bloggers like this: