This section describes how to obtain all the partition numbers on the disk based on the physical drive number. The DeviceIoControl function does not provide an operation code to directly perform this operation. Therefore, you need to perform this function in a circle.
The general idea is to first use the GetLogicalDrives function to obtain all the partition numbers in the system, then filter out non-hard disk partitions (such as software drives and optical drives), and then filter out partitions that do not belong to the specified physical disk, the last thing we need is the Partition Number.
The Code is as follows:
/*************************************** ***************************************
* Function: get disk's drive letters from physical number
* E.g. 0 --> {C, D, E} (disk0 has 3 drives, C:, D: and E :)
* Input: phyDriveNumber, disk's physical number
* Output: letters, letters array
* Return: Succeed, the amount of letters
* Fail,-1
**************************************** **************************************/
DWORD GetPartitionLetterFromPhysicalDrive (DWORD phyDriveNumber, CHAR ** letters)
{
DWORD mask;
DWORD driveType;
DWORD bmLetters;
DWORD diskNumber;
CHAR path [DISK_PATH_LEN];
CHAR letter;
DWORD letterNum;
WORD I;
CHAR * p;
BmLetters = GetLogicalDrives ();
If (0 = bmLetters)
{
Return (DWORD)-1;
}
LetterNum = 0;
For (I = 0; I <sizeof (DWORD) * 8; I ++)
{
Mask = 0x1u <I;
If (mask & bmLetters) = 0) // get one letter
{
Continue;
}
Letter = (CHAR) (0x41 + I); // ASCII change
Sprintf (path, "% c: \", letter );
DriveType = GetDriveType (path );
If (driveType! = DRIVE_FIXED)
{
BmLetters & = ~ Mask; // clear this bit
Continue;
}
DiskNumber = GetPhysicalDriveFromPartitionLetter (letter );
If (diskNumber! = PhyDriveNumber)
{
BmLetters & = ~ Mask; // clear this bit
Continue;
}
LetterNum ++;
}
// Build the result
* Letters = (CHAR *) malloc (letterNum );
If (NULL = * letters)
{
Return (DWORD)-1;
}
P = * letters;
For (I = 0; I <sizeof (DWORD) * 8; I ++)
{
Mask = 0x1u <I;
If (mask & bmLetters) = 0)
{
Continue;
}
Letter = (CHAR) (0x41 + I); // ASCII change
* P = letter;
P ++;
}
Return letterNum;
}
Code Analysis
The function input parameter DWORD phyDriveNumber is the physical disk number, for example, 0, 1, 2 .......
The function output parameter CHAR ** letters is the obtained logical Partition Number array pointer. Because a physical disk may have multiple partitions, you can store the obtained multiple partition numbers in arrays.
The Return Value of the function is the number of partitions.
1. Call the GetLogicalDrives function to obtain all the partition numbers. Note that when the returned value of the GetLogicalDrives function is A bitwise image, for example, 0th bits represent A: and 2nd bits represent C.
2. Check the obtained logical partitions one by one.
Call the GetDriveType function to obtain the partition type. If the type is not hard disk (driveType! = DRIVE_FIXED), clears this bit by 0.
Call the GetPhysicalDriveFromPartitionLetter function (see section 4) to query the physical Partition Number of the logical partition. If the value is not phyDriveNumber, set the value to 0.
The bitmap after filtering by the above two conditions stores the logical partition numbers we need.
3. Allocate space for * letters, and convert the bitmap into a drive letter and store it in an array.
This article is from the "bunny Technology Workshop" blog