1. 程式人生 > >windows的磁碟操作之三——獲取和刪除磁碟分割槽資訊

windows的磁碟操作之三——獲取和刪除磁碟分割槽資訊

上一節中介紹瞭如何初始化一塊空白的磁碟,並建立分割槽。那麼對於一塊已存在分割槽的磁碟,我們如何獲得其分割槽資訊,如何刪除其分割槽資訊呢?本節對這兩類操作進行討論。

獲得磁碟分割槽資訊的程式碼如下。 /****************************************************************************** * 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; } 1. 根據disk名稱呼叫CreateFile開啟裝置控制代碼。 2. 呼叫操作碼為IOCTL_DISK_GET_DRIVE_LAYOUT_EXDeviceIoControl函式獲取分割槽資訊。返回的資訊儲存在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_LAYOUTDeviceIoControl函式刪除分割槽表。 4. 重新整理分割槽表。 呼叫DestroyDisk後的磁碟在windows磁碟管理中的狀態為