Click here to Skip to main content
13,089,818 members (47,140 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 2 Dec 2010

How to get Present Logical Drives (the bitwise way)

, 2 Dec 2010
Rate this:
Please Sign up or sign in to vote.
How to get Present Logical Drives by function GetLogicalDrives


It shows how to get currently available Disk Drives with function "GetLogicalDrives()"


Is there any body hating the function "GetLogicalDriveStrings" and wanting to use the BITWISE way?

Using the code

You should familiarize with function first :

DWORD WINAPI GetLogicalDrives(void

This function has no parameters.
Return Value
If the function succeeds, the return value is a bitmask representing the currently available disk drives. Bit position 0 (the least-significant bit) is drive A, bit position 1 is drive B, bit position 2 is drive C, and so on.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

as the MSDN says you should find out a way of finding present drives from the bitmask given,But HOW?

As you can see a bitmask chain is needed ,but how to make that ?

The way I found:

1-Because the the drives bits are set from left to right(and from Drive A to Drive C),You have to provide a bitmask chain like the following:

the bitmask for the Drive A : 00000000000000000000000000000001

the bitmask for the Drive B : 00000000000000000000000000000010

the bitmask for the Drive C : 00000000000000000000000000000100

the bitmask for the Drive D : 00000000000000000000000000001000

and so on(Until Drive Z! ).

You can declare a mask set to 1(00000000000000000000000000000001) and do a shift to left the mask by one in a for loop 26 times!

2-And (the & operator) the current bitmask of the chain with the one from function "GetLogicalDrives()"

3-If the result doesn't become False(doesn't become 0) ,the Drive in the current bimask chain(See Given Example) is present.

Example Code

DWORD dwDrives  = 0, dwMask = 1;
CHAR chDrive;
dwDrives = GetLogicalDrives();
printf("Currently available Disk Drives : \n");
for(UINT i = 0; i< 26;i++)
        if(dwDrives & dwMask)
	        chDrive = 'A' + i;
	dwMask <<= 1;		


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote 5 Pin
Abdul Quader Mamun17-Dec-10 6:21
memberAbdul Quader Mamun17-Dec-10 6:21 
GeneralNice Pin
Abdul Quader Mamun16-Dec-10 8:34
memberAbdul Quader Mamun16-Dec-10 8:34 
GeneralReason for my vote of 5 Simple&Usefull Pin
eleqi9-Dec-10 21:19
membereleqi9-Dec-10 21:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 2 Dec 2010
Article Copyright 2010 by PE32_64
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid