2 using System.Runtime.InteropServices;
5 [StructLayout(LayoutKind.Sequential, Size=1024)]
7 [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
10 [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
13 [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
18 public static unsafe int Main () {
21 /// Structure to pointer
24 Dummy dummy = new Dummy ();
25 dummy.a = new byte[16];
26 dummy.b = new float[16];
27 dummy.c = new long[16];
29 for(int i=0; i<16; i++)
30 dummy.a[i] = (byte)(dummy.b[i] = dummy.c[i] = i+1);
32 IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Dummy)));
33 Marshal.StructureToPtr(dummy, p, false);
35 int offset = (int)Marshal.OffsetOf(typeof(Dummy), "a");
36 byte *data1 = (byte*)p.ToPointer() + offset;
37 for(int i=0; i<16; i++) {
42 offset = (int)Marshal.OffsetOf(typeof(Dummy), "b");
43 float *data2 = (float*)((byte*)p.ToPointer() + offset);
44 for(int i=0; i<16; i++)
48 offset = (int)Marshal.OffsetOf(typeof(Dummy), "c");
49 long *data3 = (long*)((byte*)p.ToPointer() + offset);
50 for(int i=0; i<16; i++)
55 /// Pointer to structure
57 Dummy dummy2 = new Dummy ();
58 Marshal.PtrToStructure(p, dummy2);
60 if(dummy2.a.Length != dummy.a.Length) return 4;
61 if(dummy2.b.Length != dummy.b.Length) return 5;
62 if(dummy2.c.Length != dummy.c.Length) return 6;
64 for(int i=0; i<16; i++)
66 if(dummy2.a[i] != i+1) return 7;
67 if(dummy2.b[i] != i+1) return 8;
68 if(dummy2.c[i] != i+1) return 9;
71 Marshal.FreeHGlobal(p);