Windows disk operation 4-obtain the physical disk number based on the Logical Partition Number

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/624379

In section 1, http://cutebunny.blog.51cto.com/301216/624027we talked about the two formats of the device name:

For physical drive X, the format is \. \ physicaldrivex. The number starts from 0, for example

Name Description
\. \ Physicaldrive0 Open the first physical drive
\\. \ Physicaldrive2 Open the third physical drive
For logical partitions (volumes), the format is \. \ x:
For example

Name Description
\. \: Open drive)
\. \ C: Open disk C (logical partition of disk)
How to find the physical drive numbers 0, 1, 2 ......
And logical partition numbers C, D, E ...... What is the relationship between them? This section describes how to obtain a physical drive letter by using a logical partition number. The next section describes how to identify the logical Partition Number contained by a physical drive number. Of course, there will certainly be other ways to implement the same function. You are welcome to add this. First, we need to make it clear that the physical drive letter and the logical partition number should be one-to-many. For example, disk0 may contain three partitions: C, D, and E. So the function getphysicaldrivefrompartitionletter discussed below returns a separate integer number. Deviceiocontrol provides the operation code ioctl_storage_get_device_number, which allows you to conveniently obtain the type and number of the device that is enabled. The Code is as follows /************************************* **************************************** ** function: get disk's physical number from its drive letter * e.g. C --> 0 (C: Is On disk0) * input: letter, drive letter * output: N/A * return: succeed, disk Number * fail, -1 ************************************** **************************************** /DWORD getphysicaldrivefrompartitionletter (char letter) {handle hdevi CE; // handle to the drive to be examined bool result; // results flag DWORD readed; // discard results storage_device_number number; // use this to get disk numbers char path [disk_path_len]; sprintf (path ,"\\\\. \ % C: ", letter); hdevice = createfile (path, // drive to open generic_read | generic_write, // access to the drive file_0000_read | file_0000_write, // share mode null, // default security Attributes open_existing, // disposition 0, // file attributes null); // do not copy file attribute if (hdevice = invalid_handle_value) // cannot open the drive {fprintf (stderr, "createfile () error: % LD \ n", getlasterror (); Return DWORD (-1);} result = deviceiocontrol (hdevice, // handle to device ioctl_storage_get_device_number, // dwiocontrolcode null, // lpinbuffer 0, // ninbuffersize & number, // Output buffer sizeof (number), // size of output buffer & readed, // number of bytes returned null // overlapped structure); If (! Result) // fail {fprintf (stderr, "ioctl_storage_get_device_number error: % LD \ n", getlasterror (); (void) closehandle (hdevice); Return (DWORD) -1;} // printf ("% d \ n", number. devicetype, number. devicenumber, number. partitionnumber); (void) closehandle (hdevice); return number. devicenumber;} code analysis: 1. generate a device name based on the Partition Number. 2. Call createfile to open the device and obtain the device handle. 3. Call the deviceiocontrol function whose operation code is ioctl_storage_get_device_number. The output is the struct variable storage_device_number.
Number. The struct storage_device_number is defined as typedef struct _ storage_device_number {device_type devicetype; ulong devicenumber; ulong partitionnumber;} storage_device_number, * pstorage_device_number. The devicenumber is the required physical disk number. 4. Return devicenumber.

This article is from the "bunny Technology Workshop" blog, please be sure to keep this source http://cutebunny.blog.51cto.com/301216/624379

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.