The previous section describes how to initialize a blank disk and create partitions. For a disk with an existing partition, how do we obtain its partition information and delete its partition information? This section discusses these two types of operations.
The code for obtaining disk partition information is as follows.
/*************************************** ***************************************
* Function: get the disk's drive layout infomation
* Input: disk, disk name
* Output: drive layout info
* Return: Succeed, 0
* Fail,-1
**************************************** **************************************/
DWORD GetDiskDriveLayout (const CHAR * disk, DRIVE_LAYOUT_INFORMATION_EX * driveLayout)
{
HANDLE hDevice; // handle to the drive to be examined
BOOL result; // results flag
DWORD readed; // discard results
HDevice = CreateFile (
Disk, // drive to open
GENERIC_READ | GENERIC_WRITE, // access to the drive
File_cmd_read | file_cmd_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_DISK_GET_DRIVE_LAYOUT_EX, // dwIoControlCode
NULL, // lpInBuffer
0, // nInBufferSize
DriveLayout, // output buffer
Sizeof (* driveLayout), // size of output buffer
& Readed, // number of bytes returned
NULL // OVERLAPPED structure
);
If (! Result)
{
Fprintf (stderr, "IOCTL_DISK_GET_DRIVE_LAYOUT_EX Error: % ld \ n", GetLastError ());
(Void) CloseHandle (hDevice );
Return DWORD (-1 );
}
(Void) CloseHandle (hDevice );
Return 0;
}
If you have a deep understanding of the code used to create a partition in the previous section, this code is very simple. The program execution process is as follows:
1. Call CreateFile according to the disk name to open the device handle.
2. Call the DeviceIoControl function with the operation code IOCTL_DISK_GET_DRIVE_LAYOUT_EX to obtain the partition information. The returned information is stored in DRIVE_LAYOUT_INFORMATION_EX * driveLayout. In this example, we only consider one partition. If there are multiple partitions, adjust the nOutBufferSize parameter in the DeviceIoControl function.
3. parse * driveLayout to obtain the partition information.
The code for deleting disk partition information is as follows,
/*************************************** ***************************************
* Function: delete the partition layout of the disk
* Input: disk, disk name
* Output: N/
* Return: Succeed, 0
* Fail,-1
**************************************** **************************************/
DWORD DestroyDisk (DWORD disk)
{
HANDLE hDevice; // handle to the drive to be examined
BOOL result; // results flag
DWORD readed; // discard results
CHAR diskPath [DISK_PATH_LEN];
Sprintf (diskPath, "\\\\\ PhysicalDrive % d", disk );
HDevice = CreateFile (
DiskPath, // drive to open
GENERIC_READ | GENERIC_WRITE, // access to the drive
File_cmd_read | file_cmd_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_DISK_DELETE_DRIVE_LAYOUT, // dwIoControlCode
NULL, // lpInBuffer
0, // nInBufferSize
NULL, // lpOutBuffer
0, // nOutBufferSize
& Readed, // number of bytes returned
NULL // OVERLAPPED structure
);
If (! Result)
{
// Fprintf (stderr, "IOCTL_DISK_DELETE_DRIVE_LAYOUT Error: % ld \ n", GetLastError ());
(Void) CloseHandle (hDevice );
Return DWORD (-1 );
}
// Fresh the partition table
Result = DeviceIoControl (
HDevice,
IOCTL_DISK_UPDATE_PROPERTIES,
NULL,
0,
NULL,
0,
& Readed,
NULL
);
If (! Result)
{
Fprintf (stderr, "IOCTL_DISK_UPDATE_PROPERTIES Error: % ld \ n", GetLastError ());
(Void) CloseHandle (hDevice );
Return DWORD (-1 );
}
(Void) CloseHandle (hDevice );
Return 0;
}
The DWORD disk parameter is the physical drive letter. The function execution process is as follows:
1. Generate a device name based on the drive letter.
2. Call CreateFile to open the device and obtain the device handle.
3. Call the DeviceIoControl function of IOCTL_DISK_DELETE_DRIVE_LAYOUT to delete the partition table.
4. Refresh the partition table.
The status of the disk after DestroyDisk is called is
This article is from the "bunny's technology Journey" blog