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(L"GetLogicalDriveStrings() should be fine!\n");

            wprintf(L"Available drives and types in this machine are:\n");



                  // 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);




                  case DRIVE_UNKNOWN: wprintf(L"This drive type cannot be determined.\n");


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


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


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


                  case DRIVE_REMOTE: wprintf(L"This is a remote (network) drive.\n");


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


                  case DRIVE_RAMDISK: wprintf(L"This is a RAM disk.\n");


                  default: wprintf(L"I 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



            wprintf(L"GetLogicalDriveStrings() 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 >