判断磁盘是否可擦除
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;