Arrays 将带数组的VB6类型转换为VB.NET结构

Arrays 将带数组的VB6类型转换为VB.NET结构,arrays,vb.net,vb6,structure,type-conversion,Arrays,Vb.net,Vb6,Structure,Type Conversion,我尝试将这些VB6类型转换为VB.NET世界 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 Byt

我尝试将这些VB6类型转换为VB.NET世界

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
目前的尝试惨遭失败

<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)

这里的链接似乎有一些相当广泛的讨论(包括示例代码):

您的结构只是缺少
Tracks
成员上的属性来告诉编译器它是一个内联100成员数组

从链接:

<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
_
结构CDROM\u-TOC
公共长度为UShort
作为字节的公共FirstTrack
公共LastTrack作为字节
_
公共轨迹数据()作为轨迹数据
端部结构

(该链接在结构中还包括两个方便的函数,我在这里省略了这些函数。)

Ah
MashalAs SizeConst
就是诀窍。顺便说一句,链接中的讨论是一个伟大的发现,谢谢!对于记录,VB6的
Long
通常映射到
Integer
,而不是
UInteger
(除非DLL需要uint,即)。
<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