Another _getdrives() example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
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: none
To do: Listing the logical drives used in local machine
To show: Using _getdrives() function
// crt, getdrives()
// This program retrieves and lists out all the logical drives that are currently mounted on the machine.
#include <windows.h>
#include <direct.h>
#include <stdio.h>
#include <tchar.h>
// = TCHAR[4]
// the _TCHAR (Microsoft) generic-text data type for your string types, the formal and
// actual parameter types for printf() match because _TCHAR* maps to char*.
_TCHAR g_szDrvMsg[] = _T("A:\n");
// TCHAR g_szDrvMsg[] = _T("A:\n");
int main(int argc, char* argv[])
{
// ULONG is a typedef for unsigned long
ULONG uDriveMask = _getdrives();
// if false
if (uDriveMask == 0)
{
printf( "_getdrives() failed with failure code: %d\n", GetLastError());
}
else
{
printf("The following logical drives are being used:\n");
// while true
while (uDriveMask)
{
if (uDriveMask & 1)
printf("Drive = %s:", g_szDrvMsg);
++g_szDrvMsg[0];
uDriveMask >>= 1;
printf("\n");
}
}
}
Output example:
The following logical drives are being used:
Drive = A:
Drive = C:
Drive = E:
Drive = F:
Drive = G:
Drive = J:
Drive = K:
Press any key to continue . . .
Note: A: is floppy, J: is USB's thumb drive and K: is CDRW