• 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 ", 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 ", 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 ", 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 ", 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 ", 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磁盘管理中的状态为

  • 相关阅读:
    第四周作业
    RHEL6+GFS2+MYSQL高可用
    第三周作业
    第二周作业
    centos7 安装redis 开机启动
    无线网卡连接网络后共享给本地有线网卡使用(Win10)
    第一周作业
    2019.8.13加入博客园
    智力题
    Python入门基础学习(模块,包)
  • 原文地址:https://www.cnblogs.com/chaikefusibushiji/p/7475619.html
Copyright © 2020-2023  润新知