public static void main(String[] args)
{
StringBuffer devicePath = new StringBuffer("\\\\.\\I:");
WinNT.HANDLE hVolume = openDiskHandleString(devicePath.toString(), 0);
Kernel32 lib = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
SCSI_PASS_THROUGH_DIRECT sptd = new SCSI_PASS_THROUGH_DIRECT();
int IOCTL_SCSI_PASS_THROUGH_DIRECT = 0x0004d014;
SCSI_result_buffer scsi_result = new SCSI_result_buffer();
System.out.println("Structure size: " + sptd.size());
Pointer pointer = sptd.getPointer();
int sector_to_read = 0x500;
sptd.Length = (byte)sptd.size();
sptd.ScsiStatus = 0;
sptd.PathId = 0;
sptd.TargetId = 0;
sptd.Lun = 0;
sptd.CdbLength = 10; //lenght of the SCSI Cdb we are using
sptd.SenseInfoLength = 0;
sptd.DataIn = 1; //1 means read data from the device
sptd.DataTransferLength = 512;
sptd.TimeOutValue = 10;
sptd.SenseInfoOffset = (short)sptd.size();
sptd.Cdb[0] = 0x28; //0x28 is the command code for a read operation
sptd.Cdb[1] = 0;
sptd.Cdb[5] = (byte)(sector_to_read % 256); //LSB byte
sector_to_read = (int) sector_to_read / 256;
sptd.Cdb[4] = (byte)(sector_to_read % 256);
sector_to_read = (int) sector_to_read / 256;
sptd.Cdb[3] = (byte)(sector_to_read % 256);
sector_to_read = (int) sector_to_read / 256;
sptd.Cdb[2] = (byte)(sector_to_read % 256);
sptd.Cdb[7] = 0;
sptd.Cdb[8] = 01;
IntByReference dwBytesReturned = new IntByReference(0);
Pointer test = sptd.getPointer();
System.out.println("Pointer: " + test.dump(0, 12));
boolean control_code = lib.DeviceIoControl(hVolume, IOCTL_SCSI_PASS_THROUGH_DIRECT, test, sptd.size(), scsi_result.getPointer(), 512, dwBytesReturned, Pointer.NULL);
if (control_code)
{
System.out.println("Data OK");
}
else
System.out.println("DeviceIOControl error: " + lib.GetLastError());
System.out.println("Returned bytes: " + dwBytesReturned.getValue());
for (int i = 0; i < dwBytesReturned.getValue(); i++)
{
System.out.printf(" %02x", scsi_result.data[i]);
}
}
private static WinNT.HANDLE openDiskHandleString(String path, int access) {
WinNT.HANDLE HandleToDevice;
int shareMode = WinNT.FILE_SHARE_READ | WinNT.FILE_SHARE_WRITE;
HandleToDevice = Kernel32.INSTANCE.CreateFile(
"\\\\.\\I:",
WinNT.GENERIC_READ|WinNT.GENERIC_WRITE,
shareMode,
null,
WinNT.OPEN_EXISTING,
0,
null);
if (HandleToDevice != WinNT.INVALID_HANDLE_VALUE)
{
System.out.println("OK");
return HandleToDevice;
}
else
{
System.out.println("Error");
return null;
}
}