Physical drive letter and Logical Disk

Source: Internet
Author: User

Two disk device names are available:

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

\\. \ Physicaldrive0 // open the first physical drive
\\. \ Physicaldrive2 // open the third physical drive

For logical partitions (volumes), the format is \. \ x:, for example

\. \ A: // open disk A (soft 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 physicalnumber 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******************************************************************************/DWORDGetPhysicalDriveFromPartitionLetter(CHAR letter){HANDLEhDevice; // handle to the drive to be examinedBOOL result;// results flagDWORD readed;// discard resultsSTORAGE_DEVICE_NUMBERnumber; //use this to get disk numbersCHAR path[DISK_PATH_LEN];sprintf(path,"\\\\.\\%c:", letter); hDevice =CreateFile(path, // drive to openGENERIC_READ |GENERIC_WRITE, // access to the driveFILE_SHARE_READ| FILE_SHARE_WRITE, //share modeNULL, //default security attributesOPEN_EXISTING,// disposition0, // fileattributesNULL); // donot copy file attribute if (hDevice ==INVALID_HANDLE_VALUE) // cannot open the drive{fprintf(stderr,"CreateFile() Error: %ld\n", GetLastError());returnDWORD(-1);} result =DeviceIoControl(hDevice, //handle to deviceIOCTL_STORAGE_GET_DEVICE_NUMBER,// dwIoControlCodeNULL, //lpInBuffer0, //nInBufferSize&number,// output buffersizeof(number),// size of output buffer&readed,// number of bytes returnedNULL //OVERLAPPED structure);if (!result)// fail{fprintf(stderr,"IOCTL_STORAGE_GET_DEVICE_NUMBER Error: %ld\n", GetLastError());(void)CloseHandle(hDevice);return(DWORD)-1;} //printf("%d%d %d\n\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_numbernumber.

Struct storage_device_number is defined

Typedef struct_storage_device_number {

Device_type devicetype;

Ulong devicenumber;

Ulong partitionnumber;

} Storage_device_number, * pstorage_device_number;

The devicenumber is the physical disk number we need.

4. Return devicenumber.

 

Source Document

 

 

Choose my computer> Manage to view the logical drive letter and the corresponding physical drive letter of the attached hard drive, optical drive, or removable disk.

 

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.