Click here to Skip to main content
Click here to Skip to main content

Tagged as

How to get Present Logical Drives (the bitwise way)

, 2 Dec 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

PE32_64
President
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

Comments and Discussions

 
GeneralMy vote 5 PinmemberAbdul Quader Mamun17-Dec-10 7:21 
GeneralNice PinmemberAbdul Quader Mamun16-Dec-10 9:34 
GeneralReason for my vote of 5 Simple&Usefull Pinmembereleqi9-Dec-10 22:19 

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

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

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