我必须在 VB.net 中创建一个结构类型的数组。但我在编组此错误时遇到错误。我必须将这个结构类型数组传递给 Dll 函数。
代码:结构声明:
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure dx_entry
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=10)> _
Public dx As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=3)> _
Public type As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> _
Public narray As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> _
Public ctier As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> _
Public poa As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=1)> _
Public poa_rsvd As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> _
Public filler As String
End Structure
初始化和编组代码:
Dim stpDx(2) As dx_entry
stpDx(1).dx = "5939" & Space(6)
stpDx(1).type = "BK" & Space(1)
stpDx(1).narray = Space(1)
stpDx(1).ctier = Space(1)
stpDx(1).poa = "Y"
stpDx(1).poa_rsvd = Space(1)
stpDx(1).filler = Space(81)
stpDx(2).dx = "1231" & Space(6)
stpDx(2).type = "BF" & Space(1)
stpDx(2).narray = Space(1)
stpDx(2).ctier = Space(1)
stpDx(2).poa = "Y"
stpDx(2).poa_rsvd = Space(1)
stpDx(2).filler = Space(81)
Dim pDxBuf As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(stpDx))
Marshal.StructureToPtr(stpDx, pDxBuf, False)
ezg_Block.pDx = pDxBuf
我收到以下错误:
An unhandled exception of type 'System.ArgumentException' occurred in Audit_Demo_2307.exe
附加信息:类型 dx_entry[] 不能被编组为非托管结构;无法计算出有意义的大小或偏移量。