Windows的磁盘操作之三——获取和删除磁盘分区信息

下文转载自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 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 磁盘管理中的状态为

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值