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;
}
 
如果你已對上一節中創建分區的代碼http://cutebunny.blog.51cto.com/301216/624052 有了比較深刻的瞭解,那麼這段代碼就非常簡單了。程序執行流程爲:
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磁盤管理中的狀態爲

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章