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)]
17 [StructLayout(LayoutKind.Sequential)]
22 public FormattedClass(int i)
28 [StructLayout(LayoutKind.Sequential)]
40 public static unsafe int Main () {
43 /// Structure to pointer
46 Dummy dummy = new Dummy ();
47 dummy.a = new byte[16];
48 dummy.b = new float[16];
49 dummy.c = new long[16];
51 for(int i=0; i<16; i++)
52 dummy.a[i] = (byte)(dummy.b[i] = dummy.c[i] = i+1);
54 IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Dummy)));
55 Marshal.StructureToPtr(dummy, p, false);
57 int offset = (int)Marshal.OffsetOf(typeof(Dummy), "a");
58 byte *data1 = (byte*)p.ToPointer() + offset;
59 for(int i=0; i<16; i++) {
64 offset = (int)Marshal.OffsetOf(typeof(Dummy), "b");
65 float *data2 = (float*)((byte*)p.ToPointer() + offset);
66 for(int i=0; i<16; i++)
70 offset = (int)Marshal.OffsetOf(typeof(Dummy), "c");
71 long *data3 = (long*)((byte*)p.ToPointer() + offset);
72 for(int i=0; i<16; i++)
77 /// Pointer to structure
79 Dummy dummy2 = new Dummy ();
80 Marshal.PtrToStructure(p, dummy2);
82 if(dummy2.a.Length != dummy.a.Length) return 4;
83 if(dummy2.b.Length != dummy.b.Length) return 5;
84 if(dummy2.c.Length != dummy.c.Length) return 6;
86 for(int i=0; i<16; i++)
88 if(dummy2.a[i] != i+1) return 7;
89 if(dummy2.b[i] != i+1) return 8;
90 if(dummy2.c[i] != i+1) return 9;
93 Marshal.FreeHGlobal(p);
99 FormattedClass fc = new FormattedClass(20);
100 IntPtr fc_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(FormattedClass)));
101 Marshal.StructureToPtr(fc, fc_ptr, false);
102 Marshal.PtrToStructure(fc_ptr, fc);
105 Marshal.FreeHGlobal(fc_ptr);
107 bool exception = false;
110 object str = new Struct(20);
111 IntPtr str_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Struct)));
112 Marshal.StructureToPtr(str, str_ptr, false);
113 Marshal.PtrToStructure(str_ptr, str);
114 Marshal.FreeHGlobal(str_ptr);