擦除磁盘
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;
}