3

I try to convert those VB6 Types into the VB.NET world.

Type TRACK_DATA
   Dim reserved As Byte
   Dim Control As Byte
   Dim Tracknumber As Byte
   Dim reserved1 As Byte
   Dim address As Long
End Type

Type CDTOC
  Dim Length As Long
  Dim FirstTrack As Byte
  Dim LastTrack As Byte
  Dim Tracks(100) As TRACK_DATA
End Type

Current attempt fails miserably

<System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, Size:=8)>
Structure TRACK_DATA
    Public reserved As Byte
    Public Control As Byte
    Public Tracknumber As Byte
    Public reserved1 As Byte
    Public address As UInteger
End Structure

<System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, Size:=806)>
Structure CDROM_TOC '4 + 1 + 1 + 800 = 806
    Public Length As UInteger
    Public FirstTrack As Byte
    Public LastTrack As Byte
    Public Tracks() As TRACK_DATA 
End Structure
...
Dim MyCD As CDTOC
ReDim MyCD.Tracks(100)

Any hints how to do that ?

It is to pass parameters and get them back to external dll, so i use Marshalling but the Marshal.SizeOf(MyCD) return wrong value (12) if i don't use InterOp Size, and, welp, all attemps with StructureToPtr ends wrong .

Code below, if of any use to understand :

    Toc_len = Marshal.SizeOf(MyCD)
    Dim Toc_ptr As IntPtr = Marshal.AllocHGlobal(CInt(Toc_len))

    'open the drive
    ...

    'access to the TOC
    DeviceIoControl(hFile, IOCTL_CDROM_READ_TOC, IntPtr.Zero, 0, Toc_ptr, Toc_len, BytesRead, IntPtr.Zero)

    'copy back the datas from unmanaged memory
    'fails here !
    MyCD = Marshal.PtrToStructure(Toc_ptr, CDTOC.GetType())
Proger_Cbsk
  • 362
  • 3
  • 12

1 Answers1

9

There looks to be some fairly extensive discussion (including example code) at the link here: https://social.msdn.microsoft.com/Forums/en-US/3df9e61d-440f-4bea-9556-b2531b30e5e6/problem-with-deviceiocontrol-function?forum=vblanguage

Your structure is just missing attributes on the Tracks member to tell the compiler that it's an inline 100-member array.

From the link:

<StructLayout(LayoutKind.Sequential)> _
Structure CDROM_TOC
    Public Length As UShort
    Public FirstTrack As Byte
    Public LastTrack As Byte
    <MarshalAs(UnmanagedType.ByValArray, SizeConst:=100)> _
    Public TrackData() As TRACK_DATA
End Structure

(The link also includes a couple of convenience functions in the structure which I have omitted here.)

Craig
  • 2,248
  • 1
  • 19
  • 23