Win32 Windows Volume Program and Code Example 10

 

 

Getting Drive Type Program Example

 

Create a new Win32 console application project and give a suitable project name.

 

Getting Drive Type Program Example - creating a new Win32 console application project using Visual Studio 2008

 

Add the source file and give a suitable name.

 

 

 

Getting Drive Type Program Example - adding a new C++ source file

 

Add the following source code.

 

#include <windows.h>

#include <stdio.h>

 

#define BUFSIZE 512

 

int wmain(int argc, WCHAR *argv[])

{

      // Translate path with device name to drive letters.

      WCHAR szTemp[BUFSIZE];

      // Hmmm...why the first index need to be NULL?

      szTemp[0] = '\0';

      // Allocate extra space for NULL lol! An initial value

      // WCHAR szDrive[2] = L ;

      // WCHAR szDrive[3] = L :;

      WCHAR szDrive[5] = L :\\;

      // Initially not found

      BOOL bFound = FALSE;

      // Point pointer p to the temporary buffer

      WCHAR *p = szTemp;

      UINT uDriveRet;

 

      // Fills szTemp buffer with strings that specify valid drives in the system.

      if (GetLogicalDriveStrings(BUFSIZE-1, szTemp))

      {

            wprintf(LGetLogicalDriveStrings() should be fine!\n);

            wprintf(LAvailable drives and types in this machine are:\n);

            do

            {

                  // Copy the drive letter to the template string

                  // Both pointers point to the same data, *p will

                  // be used to skip the NULL

                  *szDrive = *p;

                  // Print the found drives

                  wprintf(L%s - , szDrive);

                  uDriveRet = GetDriveType(szDrive);

 

                  switch(uDriveRet)

                  {

                  case DRIVE_UNKNOWN: wprintf(LThis drive type cannot be determined.\n);

                        break;

                  case DRIVE_NO_ROOT_DIR: wprintf(LThe root path is invalid. e.g. there is\n no volume mounted at the specified path.\n);

                        break;

                  case DRIVE_REMOVABLE: wprintf(LThis is a removable media. e.g. a floppy drive,\n thumb drive, or flash card reader.\n);

                        break;

                  case DRIVE_FIXED: wprintf(LThis is a fixed media. e.g. a hard drive or flash drive.\n);

                        break;

                  case DRIVE_REMOTE: wprintf(LThis is a remote (network) drive.\n);

                        break;

                  case DRIVE_CDROM: wprintf(LThis is a DVD/CD-ROM drive.\n);

                        break;

                  case DRIVE_RAMDISK: wprintf(LThis is a RAM disk.\n);

                        break;

                  default: wprintf(LI don't know this error, %u\n, GetLastError());

                  }

 

                  // This is WIERD!!! Can you see the while was used twice!!!

                  while (*p++);// skip the next NULL character, starts reading new drive string

            } while (!bFound && *p); // end of string

      }

      else

            wprintf(LGetLogicalDriveStrings() failed, error %u\n, GetLastError());

      return 0;

}

 

Build and run the project. The following screenshot is an output sample.

 

Getting Drive Type Program Example - sample output showing logical drives with their respective types

 

 

The following Figure is the drives seen through the Windows explorer.

 

Getting Drive Type Program Example - drives and their types seen in Windows explorer

 

 

  < Windows Volume 9 | Win32 Programming Index | Windows Volume Index | Windows Volume 11 >