I would like to know what the snippet of code does..
Drive[0] = 'A';
Drive[1] = ':';
Drive[2] = '\\';
Drive[3] = 0;
DriveMask = GetLogicalDrives();
for( anIndex = 0; anIndex < 26;
anIndex++ )
{
if( DriveMask & 1 )
{
Drive[0] = 'A' + anIndex;
DriveMask >>= 1;
}
}
Please let me 开发者_如何学编程know your answer.
Thank you for your time to read my post.
It checks if the lowest bit is set i.e. if there is an A drive. See GetLogicalDrives
It's enumerating all the possible attached drives between A:\ and Z:\ and checking to see whether they're removable (eg CD, floppy).
It loops 26 times, and each time
DriveMask >>= 1;
causes the bitmask to be shifted right by 1 bit, so that each logical drive can be tested for via the
if( DriveMask & 1 )
in succession.
GetDriveType() requires a drive path, so the label is constructed by adding the loop count to the letter A (so A, B, C, D, ..., Z) and leaving the previously-initialized :\ part in-place.
In C++ the &
is a bitwise and.
So take the value Drives
and do a bitwise with 0x00000001. The result should be 1 if the number is odd (only way to have an odd number is with the least significant bit is 1). Since 0 anded with 1 = 0, it basically zeroes out all the values except for the least significant bit. If that bit is 1, then the result is 1 and evaluates to true.
Otherwise it's 0, and you don't hit the if.
It checks if the number is odd.
&
is a bit-wise AND comparison.
0101 (5)
& 0001 (1)
= 0001 (1 -- true)
1110 (14)
& 0001 (1)
= 0000 (0 -- false)
In this case, GetLogicalDrives returns a number whose bits indicate the presence of certain drives. The least significant bit (20, 1) indicates the A drive.
The expression Drives & 1
is testing to see that the result of a logical and
between Drives
and 0x00000001
is non-zero. Thus it is checking to see if Drives
is odd.
actually api returns reply in binary format :- here what MSDN says about it
" 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. "
means
if( Drives & 1 ) // i dont understand this if condition here that what it checks ? {
}
Condition checking for digit drive presense.
The GetLogicalDrives function returns the set of logical drives where each drive is encoded as a bit (a binary digit, can be either 0 or 1). The drive labels start at "A" in bit 0 (the least significant bit). The bit is 1 if the drive is present, else it's 0. The & in the above code is a logical-AND operation to test bit 0. Essentially this code checks if the system has an "A:\" drive.
This piece of code does not do absolutely anything in the common understanding of the word do. This code contains only non-modifying query-type operations with no side effects, i.e. it makes some queries and verifies some conditions, but it doesn't make any actions based on the results of these conditions.
In other words, if this code was fed into some hypothetical super-optimizing compiler, which also knows the Windows API, that compiler would simply throw out (optimize away) the entire code, since it doesn't do anything.
Apparently, the code you provided is fake - it is not the whole code. Without the whole thing, it is impossible to say what it was supposed to do. However, if we guess that some useful functionality was supposed to be present between the {}
in the following if
if( GetDriveType( Drive ) == DRIVE_REMOVABLE )
{
// Actually DO something here
}
then we can make an educated guess about what it was supposed to do. This code iterates though all possible single-letter drive designations in a Windows system. It checks whether a logical drive designated by that letter is present in the system. And if the drive is present, it checks whether this drive works with removable media. And, finally, if it is true, then it does something useful that you are not showing us. I don't know what it was. Nobody does.
精彩评论