初学使用scsi命令写磁盘,总是在写最后一个扇区时,deviceiocontrol 就没反应了。 HANDLE hfile  = CreateFile("f:\\test.txt",
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hfile == INVALID_HANDLE_VALUE )
return 0;
DWORD dwFileLowSize = GetFileSize(hfile ,0 );
DWORD dwtotalSector = dwFileLowSize / bytesPerSector;
///如果有剩余的扇区
if( dwtotalSector*bytesPerSector < dwFileLowSize )
{
dwtotalSector+=1;
}
//最后一个扇区保留
DWORD dwStartSector = sectors - dwtotalSector;
while(1 )
{
memset(bufData,0 ,8*bytesPerSector);
ReadFile(hfile,bufData,8*bytesPerSector , &dwByteOfRet ,NULL );
if( dwByteOfRet <8*bytesPerSector )
{
DWORD dwRestSecotr = dwByteOfRet/bytesPerSector;
if(dwRestSecotr*bytesPerSector <dwByteOfRet )
dwRestSecotr+=2;
WriteTrack(hUdisk , dwStartSector,bufData,dwRestSecotr);
break;
}
if(!WriteTrack(hUdisk , dwStartSector,bufData,8))
break;
dwStartSector+=8; }BOOL WriteTrack(HANDLE hDev,DWORD dwStartSector , LPBYTE lpbuf ,DWORD dwNumSector)
{
BOOL status;
DWORD dwByteOfRet;
SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER sptdwb;
ZeroMemory(&sptdwb,sizeof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER));
ULONGLONG start_lba;
ULONG num_sectors;
start_lba =dwStartSector;
num_sectors = dwNumSector; sptdwb.sptd.Length = sizeof(SCSI_PASS_THROUGH_DIRECT);
sptdwb.sptd.DataIn = 0; // write to   drive
sptdwb.sptd.PathId = 0;
sptdwb.sptd.TargetId = 1;
sptdwb.sptd.Lun = 0;
sptdwb.sptd.CdbLength = 10;
sptdwb.sptd.DataTransferLength = 0x1000;
sptdwb.sptd.TimeOutValue = 300; // in milliseconds
sptdwb.sptd.DataBuffer = lpbuf;
sptdwb.sptd.SenseInfoOffset =48;
sptdwb.sptd.SenseInfoLength =0;

sptdwb.sptd.Cdb[0] = 0x2A; //opcode 0x2A
sptdwb.sptd.Cdb[1] = 0x00; 
sptdwb.sptd.Cdb[2] =(UCHAR)(start_lba >> 24) & 0xFF; // MSB of lba
sptdwb.sptd.Cdb[3] = (UCHAR)(start_lba >> 16) & 0xFF;
sptdwb.sptd.Cdb[4] = (UCHAR)(start_lba >> 8) & 0xFF;
sptdwb.sptd.Cdb[5] = (UCHAR)(start_lba) & 0xFF; // LSB of lba
sptdwb.sptd.Cdb[6] = 0x00; 
sptdwb.sptd.Cdb[7] =(UCHAR)(num_sectors >> 8) & 0xFF; // MSB of num blocks
sptdwb.sptd.Cdb[8] = (UCHAR)(num_sectors) & 0xFF; // LSB of num blocks
sptdwb.sptd.Cdb[9] = 0x00;

status = DeviceIoControl(hDev, // device handle   //最后一个扇区写的时候就死掉了
0x4d014, // control code
&sptdwb, // input query
0x50, // size of input
&sptdwb, // output
0x50, // size of output
&dwByteOfRet, // num bytes returned
NULL); // overlapped (async) IO struct
return status;
}