WIN32磁盘操作代码
+ -

使用IOCTL_DISK_GET_LENGTH_INFO获取硬盘大小

2021-09-07 79 0

使用IOCTL_DISK_GET_LENGTH_INFO获取硬盘大小

char buff[512];
    sprintf(buff,"\\\\.\\PhysicalDrive%d",nId);
    HANDLE hDevice = CreateFile(buff,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);

    if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive 
    { 
        MessageBox("打开磁盘失败!","提示",MB_OK|MB_ICONSTOP);
        return ;
    }

    #define IOCTL_DISK_GET_LENGTH_INFO          CTL_CODE(IOCTL_DISK_BASE, 0x0017, METHOD_BUFFERED, FILE_READ_ACCESS)
    typedef struct _GET_LENGTH_INFORMATION {
        LARGE_INTEGER  Length;
    } GET_LENGTH_INFORMATION, *PGET_LENGTH_INFORMATION;

    GET_LENGTH_INFORMATION disklen;


    ULONG d;
    BOOL ok =     DeviceIoControl(hDevice,IOCTL_DISK_GET_LENGTH_INFO,NULL,0,&disklen,sizeof(disklen),&d,NULL);
    CloseHandle(hDevice);    

    CString str;
    if(ok)
    {
    //    str.Format("0x%08X%08X B(%d GB)\n",disklen.Length.u.HighPart,disklen.Length.u.LowPart,disklen.Length.QuadPart/1024/1024/1024);
        str.Format("%d GB\n",disklen.Length.QuadPart/1024/1024/1024);
    }
    else
    {
        MessageBox("获取磁盘大小失败!","提示",MB_OK|MB_ICONSTOP);
    }

0 篇笔记 写笔记

使用IOCTL_DISK_GET_LENGTH_INFO获取硬盘大小
使用IOCTL_DISK_GET_LENGTH_INFO获取硬盘大小char buff[512]; sprintf(buff,"\.\PhysicalDrive%d",nId); HANDLE hDevice = CreateFile(buff,GENE......
作者信息
我爱内核
Windows驱动开发,网站开发
好好学习,天天向上。
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

您的支持,是我们前进的动力!