WIN32磁盘操作代码
+ -

判断磁盘是否可擦除

2021-09-07 35 0
int nId = pList->GetCurSel();
    char buff[1024];
    sprintf(buff,"\\\\.\\PhysicalDrive%d",nId);
    HANDLE h = CreateFile(buff,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);

    if(h == INVALID_HANDLE_VALUE)
    {
        printf("打开磁盘失败:%d\n",GetLastError());
        return FALSE;
    }
    memset(buff,0,sizeof(buff));
#pragma warning(disable:4305)
#pragma warning(disable:4309)
    buff[0] = 0x28;
    buff[2] = 0x01;
    buff[12] = 0x0a;

    buff[33] = 0x6f;
    buff[34] = 0xfe;
    buff[35] = 0xef;

    buff[36] = 0xfa;
    buff[37] = 0xaf;
    buff[38] = 0xe0;
#pragma warning(default:4305)
#pragma warning(default:4309)


#define IOCTL_SCSI_BASE                 FILE_DEVICE_CONTROLLER
#define IOCTL_ATA_PASS_THROUGH_DIRECT    CTL_CODE(IOCTL_SCSI_BASE, 0x040c, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)

    ULONG d;
    BOOL ok =     DeviceIoControl(h,IOCTL_ATA_PASS_THROUGH_DIRECT,buff,40,buff,40,&d,NULL);


    memset(buff,0,sizeof(buff));
#pragma warning(disable:4305)
#pragma warning(disable:4309)
#pragma warning(disable:4244)

    buff[0] = 0x28;
    buff[2] = 0x03;
    buff[9] = 0x02;
    buff[12] = 0x0a;

    buff[20]= 0xa8;
    buff[21] = 0xef;
    buff[22] = 0x12;

    buff[37] = 0xa0;
    buff[38] = 0xec;


    USHORT data[0x100];
    ULONG pt = (ULONG)data;
    buff[20] = pt & 0xff;
    buff[21] = (pt>>8) & 0xff;
    buff[22] = (pt>>16) & 0xff;
    buff[23] = (pt>>24) & 0xff;

#pragma warning(default:4305)
#pragma warning(default:4309)    
#pragma warning(default:4244)    

    ok =     DeviceIoControl(h,IOCTL_ATA_PASS_THROUGH_DIRECT,buff,40,buff,40,&d,NULL);
    CloseHandle(h);

    USHORT  p128 = data[128];
    return !(p128 & 0x08)  && ok;

0 篇笔记 写笔记

擦除磁盘
擦除磁盘BOOL CKDiskInitDlg::EraseDisk(){ CComboBox* pList =(CComboBox*)GetDlgItem(IDC_COMBO_DISK_LIST); int nId = pList->GetCurSel(); cha......
判断磁盘是否可擦除
int nId = pList->GetCurSel(); char buff[1024]; sprintf(buff,"\.\PhysicalDrive%d",nId); HANDLE h = CreateFile(buff,GENERIC......
作者信息
我爱内核
Windows驱动开发,网站开发
好好学习,天天向上。
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

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

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