GetLogicalDrives() - finding the DOS logical devices

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Target platform: none, just for learning

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged

To do: Finding the DOS logical drives

To show: Using the GetLogicalDrives() C function

 

 

#include <windows.h>

#include <direct.h>

#include <stdio.h>

#include <tchar.h>

 

// Gives an initial value

TCHAR szDrive[] = _T(" A:");

 

// You may want to use wmain() version

int main(void)

{

DWORD uDriveMask = GetLogicalDrives();

printf("The bitmask of the logical drives in hex: %0X\n", uDriveMask);

printf("The bitmask of the logical drives in decimal: %d\n", uDriveMask);

if(uDriveMask == 0)

printf("GetLogicalDrives() failed with failure code: %d\n", GetLastError());

else

{

printf("This machine has the following logical drives:\n");

while(uDriveMask)

{

// Use the bitwise AND, 1â€"available, 0-not available

if(uDriveMask & 1)

printf("%S ", (const char *)szDrive);

// increment, check next drive

++szDrive[1];

// shift the bitmask binary right

uDriveMask >>= 1;

}

printf("\n ");

}

return 0;

}

 

Output example:

 

The bitmask of the logical drives in hex: 475

The bitmask of the logical drives in decimal: 1141

This machine has the following logical drives:

A: C: E: F: G: K:

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index