2010-04-07 Rodrigo Kumpera <rkumpera@novell.com>
[mono-project.git] / mono / tests / marshal7.cs
blob9e91e65443d756639427f96e733ef630f5da1d58
1 using System;
2 using System.Runtime.InteropServices;
4 public class Test
6 [StructLayout(LayoutKind.Sequential, Size=32)]
7 public class TestStruct1
9 public int a;
12 [StructLayout(LayoutKind.Sequential, Size=32)]
13 public class TestStruct2
15 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=16)]
16 public string a;
17 public int b;
20 [StructLayout(LayoutKind.Sequential, Size=32)]
21 public class TestStruct3 : TestStruct2
23 public int c;
26 [StructLayout (LayoutKind.Sequential)]
27 public class TestStruct4
29 [MarshalAs (UnmanagedType.Interface)]
30 object itf;
33 [StructLayout (LayoutKind.Sequential)]
34 public class TestStruct5
36 [MarshalAs (UnmanagedType.IUnknown)]
37 object itf;
40 [StructLayout (LayoutKind.Sequential)]
41 public class TestStruct6
43 [MarshalAs (UnmanagedType.IDispatch)]
44 object itf;
47 [StructLayout (LayoutKind.Sequential)]
48 public class TestStruct7
50 [MarshalAs (UnmanagedType.Struct)]
51 object itf;
54 public unsafe static int Main ()
56 ///
57 /// Testing simple struct size
58 ///
59 if(Marshal.SizeOf(typeof(TestStruct1)) != 32)
61 return 1;
64 TestStruct1 myStruct = new TestStruct1();
65 myStruct.a = 0x12345678;
67 IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct1)));
68 Marshal.StructureToPtr(myStruct, p, false);
70 Type testType = typeof(TestStruct1);
71 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType, "a")) != myStruct.a)
72 return 2;
74 Marshal.FreeHGlobal(p);
76 ///
77 /// Testing struct size with ByValTStr string
78 ///
79 if(Marshal.SizeOf(typeof(TestStruct2)) != 32)
80 return 3;
82 TestStruct2 myStruct2 = new TestStruct2();
83 myStruct2.b = 0x12345678;
85 p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct2)));
86 Marshal.StructureToPtr(myStruct2, p, false);
88 Type testType2 = typeof(TestStruct2);
89 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType2, "b")) != myStruct2.b)
90 return 4;
92 Marshal.FreeHGlobal(p);
94 ///
95 /// Test structure size and struct with inheritance
96 ///
97 if(Marshal.SizeOf(typeof(TestStruct3)) != 64)
98 return 5;
100 TestStruct3 myStruct3 = new TestStruct3();
101 myStruct3.b = 0x12345678;
102 myStruct3.c = 0x76543210;
103 p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct3)));
104 Marshal.StructureToPtr(myStruct3, p, false);
106 Type testType3 = typeof(TestStruct3);
108 if(Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "b")) != myStruct3.b)
109 return 6;
111 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "c")) != myStruct3.c)
112 return 7;
114 Marshal.FreeHGlobal(p);
117 /// Also make sure OffsetOf returns the correct Exception.
119 try {
120 Marshal.OffsetOf(testType3, "blah");
121 return 8;
123 catch(ArgumentException e) {
124 /// Way to go :)
126 catch(Exception e) {
127 return 9;
130 // test size of structs with objects
131 if (Marshal.SizeOf (typeof (TestStruct4)) != IntPtr.Size)
132 return 10;
133 if (Marshal.SizeOf (typeof (TestStruct5)) != IntPtr.Size)
134 return 11;
135 if (Marshal.SizeOf (typeof (TestStruct6)) != IntPtr.Size)
136 return 12;
137 // a VARIANT is
138 if (Marshal.SizeOf (typeof (TestStruct7)) != 16)
139 return 13;
141 return 0;