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