获得磁盘分区信息的代码如下。
/******************************************************************************
* 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_SHARE_READ | FILE_SHARE_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;
}
如果你已对上一节中创建分区的代码http://cutebunny.blog.51cto.com/301216/624052 有了比较深刻的了解,那么这段代码就非常简单了。程序执行流程为:
1.
根据disk
名称调用
CreateFile
打开设备句柄。
2.
调用操作码为
IOCTL_DISK_GET_DRIVE_LAYOUT_EX
的
DeviceIoControl
函数获取分区信息。返回的信息存储在
DRIVE_LAYOUT_INFORMATION_EX *driveLayout
中。本例中我们只考虑了一个分区的情况,如果有多个分区,适当调整
DeviceIoControl
函数中的
nOutBufferSize
参数即可。
3.
解析
*driveLayout
即可获得分区信息。
删除磁盘分区信息的代码如下,
/******************************************************************************
* Function: delete the partition layout of the disk
* input: disk, disk name
* output: N/A
* 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_SHARE_READ | FILE_SHARE_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;
}
参数
DWORD disk
为物理驱动器号。函数执行流程为:
1.
根据驱动器号生成设备名称。
2.
调用
CreateFile
打开设备并获得设备句柄。
3.
调用操作码为
IOCTL_DISK_DELETE_DRIVE_LAYOUT
的
DeviceIoControl
函数删除分区表。
4.
刷新分区表。
调用
DestroyDisk
后的磁盘在windows
磁盘管理中的状态为