示例C语言封送结构体数组演示代码如下
" K! U6 M0 M; K- j" m 1 static void Main(string[] args)
2 n: D, ?( ~/ D% N 2 {
% o% C7 {! t3 p8 _2 g 3 int size = Marshal.SizeOf(typeof(Class)) * 50;
6 m* `* j5 o7 P& b4 r. |, \7 x ] 4 byte[] bytes = new byte[size]; , Q% F4 ~* x! b& \; k
5 IntPtr pBuff = Marshal.AllocHGlobal(size); : z$ I' j) \6 P6 l& d
6 Class[] pClass = new Class[50];
) u% x' x' O4 I4 t- s; C 7 GetClass(pBuff); 0 c) f* Y" u# H' i- ~
8 for (int i = 0; i < 50; i++) $ [, X ]# _0 x- H- n
9 { ' t! B: b) ^, g
10 IntPtr pPonitor = new IntPtr(pBuff.ToInt64() + Marshal.SizeOf(typeof(Class)) * i);
' h! ?1 m9 `: x6 z 11 pClass = (Class)Marshal.PtrToStructure(pPonitor, typeof(Class)); ; y) j* C( j5 F) H" R1 [' r
12 } 4 s2 x. Q: r- O' E
13 Marshal.FreeHGlobal(pBuff); 6 U4 w" M6 \9 ^! b( L' y
14 Console.ReadLine(); 15 }有兴趣的不妨自己测试一下C语言封送结构体数组,看看输出结果是否正确! |