好的,我到底做错了什么..
“System.TypeLoadException:无法编组'MY_Struct1'类型的字段's2':此字段的类型定义具有布局信息,但具有无效的托管/非托管类型组合或者是不可编组的。”
'VENDORAPI short FunctionEx(Struct1* pstruct1);
Declare Auto Function FunctionEx Lib "VENDORDLL.dll" (<MarshalAs(UnmanagedType.Struct)> ByRef pstruct1 As MY_Struct1) As Int16
Enum MY_ANEnum
a
b
c
End Enum
Enum MY_ANEnum2
x
y
z
End Enum
Const foo_size = 5
Const foo1_size = 15
Const foo2_size = 25
Const foo3_size = 35
Const superfoo_size = 10
Const superfoo_len = 20
<StructLayout(LayoutKind.Sequential)> _
Structure MY_Struct1
'typedef struct
'{
'short Id;
'ANEnum aEnum;
'Struct2 s2;
'} Struct1;
<MarshalAs(UnmanagedType.I2)> Public Id As Int16
<MarshalAs(UnmanagedType.I2)> Public aEnum As MY_ANEnum
<MarshalAs(UnmanagedType.Struct)> Public s2 As MY_Struct2
End Structure
<StructLayout(LayoutKind.Sequential)> _
Structure MY_Struct2
'struct Struct2
'{
'Struct3 s3;
'Struct4 s4;
'Struct5 s5;
'Struct6 s6;
'};
Public s3 As MY_Struct3
Public s4 As MY_Struct4
Public s5 As MY_Struct5
Public s6 As MY_Struct6
End Structure
<StructLayout(LayoutKind.Sequential)> _
Structure MY_Struct3
'struct Struct3
'{
' short Flag;
' long Id;
' ANEnum2 aEnum2;
' char foo[foo_size];
'};
<MarshalAs(UnmanagedType.I2)> Public Flag As Int16
<MarshalAs(UnmanagedType.I4)> Public Id As Int32
<MarshalAs(UnmanagedType.I2)> Public enum2 As MY_ANEnum2
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=foo_size)> Public foo As String
End Structure
<StructLayout(LayoutKind.Sequential)> _
Structure MY_Struct4
'struct Struct4
'{
'char foo1[foo1_size];
'char foo2[foo2_size];
'};
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=foo1_size)> Public foo1 As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=foo2_size)> Public foo2 As String
End Structure
<StructLayout(LayoutKind.Sequential)> _
Structure MY_Struct5
'typedef struct
'{
'char szfoo31[foo3_size];
'char szfoo32[foo3_size];
'char szfoo33[foo3_size];
'char szfoo34[foo3_size];
'} Struct5;
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=foo3_size)> Public szfoo31 As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=foo3_size)> Public szfoo32 As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=foo3_size)> Public szfoo33 As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=foo3_size)> Public szfoo34 As String
End Structure
<StructLayout(LayoutKind.Sequential)> _
Structure MY_Struct6
'struct Struct6
'{
'short Id;
'char aaszSuperfoo[superfoo_len][superfoo_size];
'short siId[superfoo_len];
'short siId2[superfoo_len];
'};
<MarshalAs(UnmanagedType.I2)> Public Id As Int16
<MarshalAs(UnmanagedType.ByValArray, ArraySubType:=UnmanagedType.ByValTStr, SizeConst:=superfoo_len)> Public aaszSuperFoo() As String
<MarshalAs(UnmanagedType.ByValArray, ArraySubType:=UnmanagedType.I2, SizeConst:=superfoo_len)> Public siId() As Int16
<MarshalAs(UnmanagedType.ByValArray, ArraySubType:=UnmanagedType.I2, SizeConst:=superfoo_len)> Public siId2() As Int16
End Structure
我在这里猜测它与MY_Struct6中的多维数组有关。
我尝试了一个简单的构建和Marshal.SizeOf()直接做Marshal.StructureToPtr()作为测试,它给了我一个无法计算的大小错误。
你所有的P / Invoke忍者,请在这里给我一个答案,而不是“只需用C ++编写代码”就好了。
提前致谢。
答案 0 :(得分:0)
尝试以下定义。
Partial Public Class NativeConstants
'''superfoo_size -> 10
Public Const superfoo_size As Integer = 10
'''superfoo_len -> 20
Public Const superfoo_len As Integer = 20
End Class
<System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet:=System.Runtime.InteropServices.CharSet.[Ansi])> _
Public Structure Struct6
'''short
Public Id As Short
'''char[200]
<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=200)> _
Public aaszSuperfoo As String
'''short[20]
<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=20, ArraySubType:=System.Runtime.InteropServices.UnmanagedType.I2)> _
Public siId() As Short
'''short[20]
<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=20, ArraySubType:=System.Runtime.InteropServices.UnmanagedType.I2)> _
Public siId2() As Short
End Structure