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

Source: Internet
Author: User
Original Works are allowed to be reprinted. During reprinting, please mark the article in hyperlink form
Original source, author information, and this statement. Otherwise, legal liability will be held. Http://cutebunny.blog.51cto.com/301216/624567

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 Merge (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;} the input parameter DWORD phydrivenumber of the code analysis function 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:
And so on. 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 http://cutebunny.blog.51cto.com/301216/624379 for details) to query the physical partition number to which the logical partition belongs. If it is not phydrivenumber, clear it 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, please be sure to keep this source http://cutebunny.blog.51cto.com/301216/624567

Related Article

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.