Windows disk operation 5-obtain all logical partition numbers on a physical disk

Source: Internet
Author: User

 

 

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.