Click here to Skip to main content
15,890,741 members
Articles / Programming Languages / C++
Tip/Trick

How to get Present Logical Drives (the bitwise way)

Rate me:
Please Sign up or sign in to vote.
4.38/5 (5 votes)
2 Dec 2010CPOL1 min read 15.4K   3   3
How to get Present Logical Drives by function GetLogicalDrives

Introduction

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

Background


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
);

Parameters
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;
		printf("%c:\n",chDrive);
	}
	dwMask <<= 1;		
}

License

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


Written By
President
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote 5 Pin
Abdul Quader Mamun17-Dec-10 6:21
Abdul Quader Mamun17-Dec-10 6:21 
GeneralNice Pin
Abdul Quader Mamun16-Dec-10 8:34
Abdul Quader Mamun16-Dec-10 8:34 
GeneralReason for my vote of 5 Simple&Usefull Pin
eleqi9-Dec-10 21:19
eleqi9-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.