Win32 Windows Volume Program and Code Example 8



Getting Logical Drive Program Example


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


Getting Logical Drive Program Example - creating a new Win32 console mode application project




Add the source file and give a suitable name.


Getting Logical Drive Program Example - adding C++ source file


Add the following source code.


#include <windows.h>

#include <stdio.h>


// Optional function: Decimal to binary

void decnumtobin(DWORD deci)


      DWORD input = deci;

      DWORD i;

      DWORD count = 0;

      DWORD binary[128];




            // Modulus 2 to get 1 or a 0

            i = input%2;

            // Load elements into the binary array

            binary[count] = i;

            // Divide input by 2 for binary decrement

            input = input/2;

            // Count the binary digits


      }while (input > 0);


      // Reverse and output binary digits

      wprintf(LThe bitmask of the logical drives in binary: );



            wprintf(L%d, binary[count - 1]);


      } while (count > 0);




int wmain()


      // Must give initial value and then let the

      // while loop iterates

      // There is weird thing here, we need to provide a space

      // at the beginning...what wrong here?

      WCHAR szDrive[] = L A;

      // Get the logical drive mask

      DWORD uDriveMask = GetLogicalDrives();


      // Display the drive mask    

      wprintf(LThe bitmask of the logical drives in hex: 0X%.8X\n, uDriveMask);

      wprintf(LThe bitmask of the logical drives in decimal: %.8d\n, uDriveMask);


      wprintf(LInitial dummy drive string: %s\n, szDrive);


      // Verify the returned drive mask

      if(uDriveMask == 0)

            wprintf(LGetLogicalDrives() failed with error code: %d\n, GetLastError());



            wprintf(LThis machine has the following logical drives:\n);



                  // use the bitwise AND, 1–available (1 & 1), 0-not available

                  // the binary representation is in reverse lol

                  if(uDriveMask & 1)


                        // Just print out the available drives

                        wprintf(LBitwise AND result = %u, drive %s\n, (uDriveMask & 1), szDrive);


                  // increment for next...


                  // shift the bitmask binary right

                  uDriveMask >>= 1;




      return 0;




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


Getting Logical Drive Program Example - sample output showing the bitmasks and logical drives


The following Figure shows the drives when seen in Windows explorer for the previous sample output.


Getting Logical Drive Program Example - logical drives as seen through Windows explorer



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