原創作品,允許轉載,轉載時請務必以超連結形式標明文章
原始出處 、作者資訊和本聲明。否則將追究法律責任。http://cutebunny.blog.51cto.com/301216/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 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磁碟管理中的狀態為
本文出自 “bunny技術坊” 部落格,請務必保留此出處http://cutebunny.blog.51cto.com/301216/624079