CODE
[StructLayout(LayoutKind.Sequential)]
public struct SRB_ExecSCSICmd
{
public byte SRB_Cmd; // ASPI command code = SC_EXEC_SCSI_CMD
public byte SRB_Status; // ASPI command status byte
public byte SRB_HaId; // ASPI host adapter number
public byte SRB_Flags; // ASPI request flags
public int SRB_Hdr_Rsvd; // Reserved, MUST = 0
public byte SRB_Target; // Target's SCSI ID
public byte SRB_Lun; // Target's LUN number
public int SRB_Rsvd1; // Reserved for Alignment
public int SRB_BufLen; // Data Allocation Length
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public byte[] SRB_BufPointer; // Data Buffer Pointer
public byte SRB_SenseLen; // Sense Allocation Length
public byte SRB_CDBLen; // CDB Length
public byte SRB_HaStat; // Host Adapter Status
public byte SRB_TargStat; // Target Status
public IntPtr SRB_PostProc; // Post routine
[MarshalAs(UnmanagedType.ByValArray, SizeConst=20)]
public byte[] SRB_Rsvd2; // Reserved, MUST = 0
[MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
public byte[] CDBByte; // SCSI CDB
[MarshalAs(UnmanagedType.ByValArray, SizeConst = SENSE_LEN + 2)]
public byte[] SenseArea; // Request Sense buffer
}
[DllImport("WNASPI32.dll", EntryPoint = "SendASPI32Command")]
public static extern int SendASPI32Command(ref SRB_ExecSCSICmd psrb);
byte[] byReadToc = new byte[Win32Functions.CB_CDROMSECTOR];
Array.Copy(byReadToc, 0, new string((char)0, byReadToc.Length).ToCharArray(), 0,
byReadToc.Length);
int dwASPIStatus;
Win32Functions.SRB_ExecSCSICmd srbExec = new Win32Functions.SRB_ExecSCSICmd();
srbExec.SRB_Cmd = Win32Functions.SC_EXEC_SCSI_CMD;
srbExec.SRB_HaId = 1;
srbExec.SRB_Flags = Win32Functions.SRB_DIR_IN;
srbExec.SRB_Target = 0;
srbExec.SRB_Lun = 0;
srbExec.SRB_BufLen = Win32Functions.CB_CDROMSECTOR;
srbExec.SRB_BufPointer = byReadToc;
srbExec.SRB_SenseLen = Win32Functions.SENSE_LEN;
srbExec.SRB_CDBLen = 16;
srbExec.SRB_PostProc = IntPtr.Zero;
srbExec.CDBByte = new byte[16];
Array.Copy(srbExec.CDBByte, 0, new string((char)0, srbExec.CDBByte.Length).ToCharArray(), 0,
srbExec.CDBByte.Length);
srbExec.CDBByte[0] = Win32Functions.SCSI_READ_TOC;
srbExec.CDBByte[7] = Win32Functions.CB_CDROMSECTOR >> 8;
srbExec.CDBByte[8] = Win32Functions.CB_CDROMSECTOR & 0xFF;
dwASPIStatus = Win32Functions.SendASPI32Command(ref srbExec);
while (srbExec.SRB_Status == Win32Functions.SS_PENDING);
Console.WriteLine("Return status: " + dwASPIStatus);
int wNumTracks = ((byReadToc[0] << 8) + byReadToc[1] - 2) / 8;
Console.WriteLine("# of tracks: " + wNumTracks);