4 // Tests for COM Interop related features
8 using System.Runtime.CompilerServices;
9 using System.Runtime.InteropServices;
14 [DllImport("libtest")]
15 public static extern int mono_test_marshal_bstr_in([MarshalAs(UnmanagedType.BStr)]string str);
17 [DllImport("libtest")]
18 public static extern int mono_test_marshal_bstr_out([MarshalAs(UnmanagedType.BStr)] out string str);
20 [DllImport("libtest")]
21 public static extern int mono_test_marshal_bstr_in_null([MarshalAs(UnmanagedType.BStr)]string str);
23 [DllImport("libtest")]
24 public static extern int mono_test_marshal_bstr_out_null([MarshalAs(UnmanagedType.BStr)] out string str);
26 [DllImport("libtest")]
27 public static extern int mono_test_marshal_variant_in_sbyte([MarshalAs(UnmanagedType.Struct)]object obj);
29 [DllImport("libtest")]
30 public static extern int mono_test_marshal_variant_in_byte([MarshalAs(UnmanagedType.Struct)]object obj);
32 [DllImport("libtest")]
33 public static extern int mono_test_marshal_variant_in_short([MarshalAs(UnmanagedType.Struct)]object obj);
35 [DllImport("libtest")]
36 public static extern int mono_test_marshal_variant_in_ushort([MarshalAs(UnmanagedType.Struct)]object obj);
38 [DllImport("libtest")]
39 public static extern int mono_test_marshal_variant_in_int([MarshalAs(UnmanagedType.Struct)]object obj);
41 [DllImport("libtest")]
42 public static extern int mono_test_marshal_variant_in_uint([MarshalAs(UnmanagedType.Struct)]object obj);
44 [DllImport("libtest")]
45 public static extern int mono_test_marshal_variant_in_long([MarshalAs(UnmanagedType.Struct)]object obj);
47 [DllImport("libtest")]
48 public static extern int mono_test_marshal_variant_in_ulong([MarshalAs(UnmanagedType.Struct)]object obj);
50 [DllImport("libtest")]
51 public static extern int mono_test_marshal_variant_in_float([MarshalAs(UnmanagedType.Struct)]object obj);
53 [DllImport("libtest")]
54 public static extern int mono_test_marshal_variant_in_double([MarshalAs(UnmanagedType.Struct)]object obj);
56 [DllImport("libtest")]
57 public static extern int mono_test_marshal_variant_in_bstr([MarshalAs(UnmanagedType.Struct)]object obj);
59 [DllImport("libtest")]
60 public static extern int mono_test_marshal_variant_out_sbyte([MarshalAs(UnmanagedType.Struct)]out object obj);
62 [DllImport("libtest")]
63 public static extern int mono_test_marshal_variant_out_byte([MarshalAs(UnmanagedType.Struct)]out object obj);
65 [DllImport("libtest")]
66 public static extern int mono_test_marshal_variant_out_short([MarshalAs(UnmanagedType.Struct)]out object obj);
68 [DllImport("libtest")]
69 public static extern int mono_test_marshal_variant_out_ushort([MarshalAs(UnmanagedType.Struct)]out object obj);
71 [DllImport("libtest")]
72 public static extern int mono_test_marshal_variant_out_int([MarshalAs(UnmanagedType.Struct)]out object obj);
74 [DllImport("libtest")]
75 public static extern int mono_test_marshal_variant_out_uint([MarshalAs(UnmanagedType.Struct)]out object obj);
77 [DllImport("libtest")]
78 public static extern int mono_test_marshal_variant_out_long([MarshalAs(UnmanagedType.Struct)]out object obj);
80 [DllImport("libtest")]
81 public static extern int mono_test_marshal_variant_out_ulong([MarshalAs(UnmanagedType.Struct)]out object obj);
83 [DllImport("libtest")]
84 public static extern int mono_test_marshal_variant_out_float([MarshalAs(UnmanagedType.Struct)]out object obj);
86 [DllImport("libtest")]
87 public static extern int mono_test_marshal_variant_out_double([MarshalAs(UnmanagedType.Struct)]out object obj);
89 [DllImport("libtest")]
90 public static extern int mono_test_marshal_variant_out_bstr([MarshalAs(UnmanagedType.Struct)]out object obj);
92 [DllImport ("libtest")]
93 public static extern int mono_test_marshal_com_object_create (out IntPtr pUnk);
95 [DllImport ("libtest")]
96 public static extern int mono_test_marshal_com_object_destroy (IntPtr pUnk);
98 [DllImport ("libtest")]
99 public static extern int mono_test_marshal_com_object_ref_count (IntPtr pUnk);
101 public static int Main() {
103 bool isWindows = !(((int)Environment.OSVersion.Platform == 4) ||
104 ((int)Environment.OSVersion.Platform == 128));
109 if (mono_test_marshal_bstr_in ("mono_test_marshal_bstr_in") != 0)
111 if (mono_test_marshal_bstr_out (out str) != 0 || str != "mono_test_marshal_bstr_out")
113 if (mono_test_marshal_bstr_in_null (null) != 0)
115 if (mono_test_marshal_bstr_out_null (out str) != 0 || str != null)
118 #endregion // BSTR Tests
120 #region VARIANT Tests
123 if (mono_test_marshal_variant_in_sbyte ((sbyte)100) != 0)
125 if (mono_test_marshal_variant_in_byte ((byte)100) != 0)
127 if (mono_test_marshal_variant_in_short ((short)314) != 0)
129 if (mono_test_marshal_variant_in_ushort ((ushort)314) != 0)
131 if (mono_test_marshal_variant_in_int ((int)314) != 0)
133 if (mono_test_marshal_variant_in_uint ((uint)314) != 0)
135 if (mono_test_marshal_variant_in_long ((long)314) != 0)
137 if (mono_test_marshal_variant_in_ulong ((ulong)314) != 0)
139 if (mono_test_marshal_variant_in_float ((float)3.14) != 0)
141 if (mono_test_marshal_variant_in_double ((double)3.14) != 0)
143 if (mono_test_marshal_variant_in_bstr ("PI") != 0)
145 if (mono_test_marshal_variant_out_sbyte (out obj) != 0 || (sbyte)obj != 100)
147 if (mono_test_marshal_variant_out_byte (out obj) != 0 || (byte)obj != 100)
149 if (mono_test_marshal_variant_out_short (out obj) != 0 || (short)obj != 314)
151 if (mono_test_marshal_variant_out_ushort (out obj) != 0 || (ushort)obj != 314)
153 if (mono_test_marshal_variant_out_int (out obj) != 0 || (int)obj != 314)
155 if (mono_test_marshal_variant_out_uint (out obj) != 0 || (uint)obj != 314)
157 if (mono_test_marshal_variant_out_long (out obj) != 0 || (long)obj != 314)
159 if (mono_test_marshal_variant_out_ulong (out obj) != 0 || (ulong)obj != 314)
161 if (mono_test_marshal_variant_out_float (out obj) != 0 || ((float)obj - 3.14) / 3.14 > .001)
163 if (mono_test_marshal_variant_out_double (out obj) != 0 || ((double)obj - 3.14) / 3.14 > .001)
165 if (mono_test_marshal_variant_out_bstr (out obj) != 0 || (string)obj != "PI")
168 #endregion // VARIANT Tests
170 #region Marshal COM Interop Tests
173 if (mono_test_marshal_com_object_create (out pUnk) != 0)
176 if (mono_test_marshal_com_object_ref_count (pUnk) != 1)
179 if (Marshal.AddRef (pUnk) != 2)
182 if (mono_test_marshal_com_object_ref_count (pUnk) != 2)
185 if (Marshal.Release (pUnk) != 1)
188 if (mono_test_marshal_com_object_ref_count (pUnk) != 1)
191 object com_obj = Marshal.GetObjectForIUnknown (pUnk);
196 IMath imath = com_obj as IMath;
201 if (imath.Add (20, 10) != 30)
204 if (imath.Subtract (20, 10) != 10)
208 imath.Same (out same1);
209 imath.Same (out same2);
213 if (!same1.Equals (same2))
217 imath.Different (out diff1);
218 imath.Different (out diff2);
222 if (diff1.Equals (diff2))
225 // same1 & same2 share a RCW
226 if (Marshal.ReleaseComObject (same1) != 1)
229 if (Marshal.ReleaseComObject (same2) != 0)
233 if (Marshal.ReleaseComObject (diff1) != 0 ||
234 Marshal.ReleaseComObject (diff2) != 0)
237 IntPtr pUnk2 = Marshal.GetIUnknownForObject (imath);
238 if (pUnk2 == IntPtr.Zero)
244 IntPtr pDisp = Marshal.GetIDispatchForObject (imath);
245 if (pDisp == IntPtr.Zero)
252 //if (mono_test_marshal_com_object_destroy (pUnk) != 0)
254 #endregion // Marshal COM Interop Tests
261 [Guid ("00000000-0000-0000-0000-000000000001")]
262 [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
263 public interface IMath
265 [MethodImplAttribute (MethodImplOptions.InternalCall,MethodCodeType=MethodCodeType.Runtime)]
266 int Add (int a, int b);
267 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
268 int Subtract (int a, int b);
269 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
270 int Same ([MarshalAs(UnmanagedType.Interface)] out IMath imath);
271 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
272 int Different ([MarshalAs (UnmanagedType.Interface)] out IMath imath);
276 [Guid ("00000000-0000-0000-0000-000000000002")]
277 public class Foo : IMath
279 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
280 public extern int Add (int a, int b);
281 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
282 public extern int Subtract (int a, int b);
283 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
284 public extern int Same ([MarshalAs (UnmanagedType.Interface)] out IMath imath);
285 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
286 public extern int Different ([MarshalAs (UnmanagedType.Interface)] out IMath imath);