下文转载自http://blog.51cto.com/cutebunny/624079
上一节中介绍了如何初始化一块空白的磁盘,并创建分区。那么对于一块已存在分区的磁盘,我们如何获得其分区信息,如何删除其分区信息呢?本节对这两类操作进行讨论。
获得磁盘分区信息的代码如下。
/******************************************************************************* 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 examinedBOOL result; // results flagDWORD readed; // discard resultshDevice = CreateFile(disk, // drive to openGENERIC_READ | GENERIC_WRITE, // access to the driveFILE_SHARE_READ | FILE_SHARE_WRITE, //share modeNULL, // default security attributesOPEN_EXISTING, // disposition0, // file attributesNULL // 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 deviceIOCTL_DISK_GET_DRIVE_LAYOUT_EX, // dwIoControlCodeNULL, // lpInBuffer0, // nInBufferSizedriveLayout, // output buffersizeof(*driveLayout), // size of output buffer&readed, // number of bytes returnedNULL // 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 examinedBOOL result; // results flagDWORD readed; // discard resultsCHAR diskPath[DISK_PATH_LEN];sprintf(diskPath, "\\\\.\\PhysicalDrive%d", disk);hDevice = CreateFile(diskPath, // drive to openGENERIC_READ | GENERIC_WRITE, // access to the driveFILE_SHARE_READ | FILE_SHARE_WRITE, //share modeNULL, // default security attributesOPEN_EXISTING, // disposition0, // file attributesNULL // 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 deviceIOCTL_DISK_DELETE_DRIVE_LAYOUT, // dwIoControlCodeNULL, // lpInBuffer0, // nInBufferSizeNULL, // lpOutBuffer0, // nOutBufferSize&readed, // number of bytes returnedNULL // OVERLAPPED structure);if (!result){//fprintf(stderr, "IOCTL_DISK_DELETE_DRIVE_LAYOUT Error: %ld\n", GetLastError());(void)CloseHandle(hDevice);return DWORD(-1);}//fresh the partition tableresult = 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
磁盘管理中的状态为