WIN32磁盘操作代码
+ -

擦除磁盘

2021-09-07 41 0

擦除磁盘

BOOL CKDiskInitDlg::EraseDisk()
{
    CComboBox* pList =(CComboBox*)GetDlgItem(IDC_COMBO_DISK_LIST);
    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)
#pragma warning(disable:4244)
    buff[0] = 0x28;
    buff[2] =  0x05;
    buff[9] =  0x02;
    buff[12] = 0x1e;

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

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


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

    USHORT data[0x100];
    memset(data,0,sizeof(data));

    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)

    ULONG d;
    BOOL    ok =     DeviceIoControl(h,IOCTL_ATA_PASS_THROUGH_DIRECT,buff,40,buff,40,&d,NULL);
    if(!ok)
    {
        CloseHandle(h);
        return FALSE;
    }

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

    buff[0] = 0x28;
    buff[2] =  0x01;
    buff[12] = 0x14;

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


    ok =     DeviceIoControl(h,IOCTL_ATA_PASS_THROUGH_DIRECT,buff,40,buff,40,&d,NULL);
    if(!ok)
    {
        CloseHandle(h);
        return FALSE;
    }


    //3
    memset(buff,0,sizeof(buff));
    memset(data,0,sizeof(data));

    buff[0] = 0x28;
    buff[2] =  0x05;
    buff[9] =  0x02;
    buff[12] = 0x2c;
    buff[13] = 0x01;

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

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


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

    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);
    return 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驱动开发,网站开发
好好学习,天天向上。
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

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

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