Win32 Windows Volume Program and Code Example 9

 

 

Getting the Logical Drive String Program Example

 

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

 

Getting the Logical Drive String Program Example - creating a new Win32 console application project using Visual studio 2008

 

Add the source file and give a suitable name.

 

 

 

Getting the Logical Drive String 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[3] = L :;

      WCHAR szDrive[5] = L :\\;

      // Initially not found

      BOOL bFound = FALSE;

      // Point pointer p to the temporary buffer

      WCHAR *p = szTemp;

 

      // 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 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\n, szDrive);

                  // This is WEIRD!!! 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 the Logical Drive String Program Example - a sample output showing logical derives

 

 

  < Windows Volume 8 | Win32 Programming Index | Windows Volume Index | Windows Volume 10 >