我有大量的UDT,我试图使用Marshal和FileGet将记录读入结构。
该函数没有第一个只包含Long和Integer的结构,返回结构,没问题。
我的问题从第二条记录开始。
我为每个字段定义了MarshalAs属性的结构:
<StructLayout(LayoutKind.Sequential)>
Public Structure typStaticData
<MarshalAs(UnmanagedType.AsAny, SizeConst:=8)> Public DateStamp As Nullable(Of Date) '8
<MarshalAs(UnmanagedType.U8)> Public Farmcode As Nullable(Of Long) '8
<MarshalAs(UnmanagedType.AsAny, SizeConst:=8)> Public StartDate As Nullable(Of Date) '8
<MarshalAs(UnmanagedType.U8)> Public NoTrans As Nullable(Of Long) '8
<MarshalAs(UnmanagedType.Bool)> Public ClosingVisit As Nullable(Of Boolean) '1
<MarshalAs(UnmanagedType.U1)> Public FileType As Nullable(Of Byte) '1
<MarshalAs(UnmanagedType.AnsiBStr)> Public Cat As String
<MarshalAs(UnmanagedType.AsAny, SizeConst:=8)> <VBFixedArray(11)> Public VisitDates() As Nullable(Of Date) ' (1 To 12) ' 12 * 8
<MarshalAs(UnmanagedType.Bool)> <VBFixedArray(11)> Public VisitDOC() As Nullable(Of Boolean) ' (1 To 12) ' 12 * 1
<MarshalAs(UnmanagedType.AnsiBStr)> Public filler As String
<MarshalAs(UnmanagedType.Currency, SizeConst:=16)> Public fillercur As Nullable(Of Decimal) '16
<MarshalAs(UnmanagedType.U8)> Public fillerLong As Nullable(Of Long) '8
<MarshalAs(UnmanagedType.AsAny, SizeConst:=8)> Public ERRDate As Nullable(Of Date) '8
<MarshalAs(UnmanagedType.AsAny, SizeConst:=8)> Public fillerDate1 As Nullable(Of Date) '8
<MarshalAs(UnmanagedType.AsAny, SizeConst:=8)> Public fillerDate2 As Nullable(Of Date) '8
End Structure
读取数据的子程序是:
Public Sub ReadBinaryFile(ByRef outValue As Object,
Optional ByRef inRecordCount As Integer = 1)
Const C_PROC_NAME = "ReadBinaryFile"
' remember to convert Dates from their "Long/ticks" equivalent
If iFreeFile = 0 Then Exit Sub
Try
Dim recordLength As Integer = Marshal.SizeOf(outValue) '<-------------
Dim myBytes As Byte() : ReDim myBytes(recordLength)
FileGet(iFreeFile, myBytes, inRecordCount)
BuildStr(myBytes, outValue.GetType, outValue)
inRecordCount += 1
Catch ex As Exception
If gDebug Then Console.WriteLine(CODE_MODULE & "-" & C_PROC_NAME & ": " & ex.Message)
oERR.LogFileError(inModule:=CODE_MODULE,
inRoutine:=C_PROC_NAME,
inEX:=ex, inErr:=Err)
If gDebug Then Stop
End Try
End Sub
我不断遇到的错误是:
ReadBinaryFile: Type 'Claims32.modStaticData+typStaticData[]' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
我尝试了几种不同的方法
我在哪里错了,我还需要做什么?