GetLogicalDriveStrings() - Finding DOS logical drive strings

 

 

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 drive strings

To show: Using GetLogicalDriveStrings() function

 

 

#include <windows.h>

#include <stdio.h>

 

// Buffer length

DWORD mydrives = 100;

// Buffer for drive string storage

char lpBuffer[100];

 

// You may want to try the wmain() version

int main(void)

{

DWORD test;

int i;

test = GetLogicalDriveStrings(mydrives, (LPWSTR)lpBuffer);

if(test != 0)

{

printf("GetLogicalDriveStrings() return value: %d, Error (if any): %d \n", test, GetLastError());

printf("The logical drives of this machine are:\n");

 

// Check up to 100 drives...

for(i = 0; i<100; i++)

printf("%c", lpBuffer[i]);

printf("\n");

}

else

printf("GetLogicalDriveStrings() is failed lor!!! Error code: %d\n", GetLastError());

return 0;

}

 

Output example:

 

GetLogicalDriveStrings() return value: 24, Error (if any): 0

The logical drives of this machine are:

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

Press any key to continue . . .

 

 

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