上一节中介绍了如何初始化一块空白的磁盘,并创建分区。那么对于一块已存在分区的磁盘,我们如何获得其分区信息,如何删除其分区信息呢?本节对这两类操作进行讨论。
获得磁盘分区信息的代码如下。
/******************************************************************************
* 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 ", 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 ", GetLastError());
(void)CloseHandle(hDevice);
return DWORD(-1);
}
(void)CloseHandle(hDevice);
return 0;
}
如果你已对上一节中创建分区的代码有了比较深刻的了解,那么这段代码就非常简单了。程序执行流程为:
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 ", 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 ", 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 ", 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磁盘管理中的状态为
亿恩科技地址(ADD):郑州市黄河路129号天一大厦608室 邮编(ZIP):450008 传真(FAX):0371-60123888
联系:亿恩小凡
QQ:89317007
电话:0371-63322206 本文出自:亿恩科技【www.enkj.com】
服务器租用/服务器托管中国五强!虚拟主机域名注册顶级提供商!15年品质保障!--亿恩科技[ENKJ.COM]
|