Help - Search - Members - Calendar
Full Version: Need some help with code to read toc in C#
Hydrogenaudio Forums > CD-R and Audio Hardware > CD Hardware/Software
ponchorage
I'm working on a little app in C# that will read a CD's table of contents (TOC). I'm using ASPI to do this. I know you would normally do this in C, but I'm using C#. I'm able to get the number of drives, their names, and other info, but I haven't been able to read the TOC. Below is the relevant code if anybody can help out and tell me what I'm missing it would be great! If you need more code or details, please let me know. I've been racking my brain for a few days and can't figure this out.

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);
ponchorage
I forgot to mention what my problem is. smile.gif

The TOC data is supposed to get put into the buffer byReadToc, but nothing ever gets put there.
ponchorage
bump...

Anybody? I can get it to work with C++ code (not .NET) but I'm still unable to get anything back using C#.
Raja
I don't know how to help you, but I'd just like to poin out how wonderful it is that you commented your code. I don't see this enough on coding projects aside from my own.
ponchorage
I should actually do a better job of this.

Edit: I hadn't noticed I actually had commented any code in there. Sorry for that previous comment
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.