2007-03-28 Chris Toshok <toshok@ximian.com>
[mono-project.git] / mono / tests / cominterop.cs
blob985e58f29edb1ede5f5147a0de0efa3c314a159e
1 //
2 // cominterop.cs:
3 //
4 // Tests for COM Interop related features
5 //
7 using System;
8 using System.Runtime.CompilerServices;
9 using System.Runtime.InteropServices;
11 public class Tests
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_in_bool_true ([MarshalAs (UnmanagedType.Struct)]object obj);
62 [DllImport ("libtest")]
63 public static extern int mono_test_marshal_variant_in_bool_false ([MarshalAs (UnmanagedType.Struct)]object obj);
65 [DllImport("libtest")]
66 public static extern int mono_test_marshal_variant_out_sbyte([MarshalAs(UnmanagedType.Struct)]out object obj);
68 [DllImport("libtest")]
69 public static extern int mono_test_marshal_variant_out_byte([MarshalAs(UnmanagedType.Struct)]out object obj);
71 [DllImport("libtest")]
72 public static extern int mono_test_marshal_variant_out_short([MarshalAs(UnmanagedType.Struct)]out object obj);
74 [DllImport("libtest")]
75 public static extern int mono_test_marshal_variant_out_ushort([MarshalAs(UnmanagedType.Struct)]out object obj);
77 [DllImport("libtest")]
78 public static extern int mono_test_marshal_variant_out_int([MarshalAs(UnmanagedType.Struct)]out object obj);
80 [DllImport("libtest")]
81 public static extern int mono_test_marshal_variant_out_uint([MarshalAs(UnmanagedType.Struct)]out object obj);
83 [DllImport("libtest")]
84 public static extern int mono_test_marshal_variant_out_long([MarshalAs(UnmanagedType.Struct)]out object obj);
86 [DllImport("libtest")]
87 public static extern int mono_test_marshal_variant_out_ulong([MarshalAs(UnmanagedType.Struct)]out object obj);
89 [DllImport("libtest")]
90 public static extern int mono_test_marshal_variant_out_float([MarshalAs(UnmanagedType.Struct)]out object obj);
92 [DllImport("libtest")]
93 public static extern int mono_test_marshal_variant_out_double([MarshalAs(UnmanagedType.Struct)]out object obj);
95 [DllImport("libtest")]
96 public static extern int mono_test_marshal_variant_out_bstr ([MarshalAs (UnmanagedType.Struct)]out object obj);
98 [DllImport("libtest")]
99 public static extern int mono_test_marshal_variant_out_bool_true ([MarshalAs (UnmanagedType.Struct)]out object obj);
101 [DllImport ("libtest")]
102 public static extern int mono_test_marshal_variant_out_bool_false ([MarshalAs (UnmanagedType.Struct)]out object obj);
105 public delegate int VarFunc (VarEnum vt, [MarshalAs (UnmanagedType.Struct)] object obj);
107 public delegate int VarRefFunc (VarEnum vt, [MarshalAs (UnmanagedType.Struct)] ref object obj);
109 [DllImport ("libtest")]
110 public static extern int mono_test_marshal_variant_in_sbyte_unmanaged (VarFunc func);
112 [DllImport ("libtest")]
113 public static extern int mono_test_marshal_variant_in_byte_unmanaged (VarFunc func);
115 [DllImport ("libtest")]
116 public static extern int mono_test_marshal_variant_in_short_unmanaged (VarFunc func);
118 [DllImport ("libtest")]
119 public static extern int mono_test_marshal_variant_in_ushort_unmanaged (VarFunc func);
121 [DllImport ("libtest")]
122 public static extern int mono_test_marshal_variant_in_int_unmanaged (VarFunc func);
124 [DllImport ("libtest")]
125 public static extern int mono_test_marshal_variant_in_uint_unmanaged (VarFunc func);
127 [DllImport ("libtest")]
128 public static extern int mono_test_marshal_variant_in_long_unmanaged (VarFunc func);
130 [DllImport ("libtest")]
131 public static extern int mono_test_marshal_variant_in_ulong_unmanaged (VarFunc func);
133 [DllImport ("libtest")]
134 public static extern int mono_test_marshal_variant_in_float_unmanaged (VarFunc func);
136 [DllImport ("libtest")]
137 public static extern int mono_test_marshal_variant_in_double_unmanaged (VarFunc func);
139 [DllImport ("libtest")]
140 public static extern int mono_test_marshal_variant_in_bstr_unmanaged (VarFunc func);
142 [DllImport ("libtest")]
143 public static extern int mono_test_marshal_variant_in_bool_true_unmanaged (VarFunc func);
145 [DllImport ("libtest")]
146 public static extern int mono_test_marshal_variant_in_bool_false_unmanaged (VarFunc func);
148 [DllImport ("libtest")]
149 public static extern int mono_test_marshal_variant_out_sbyte_unmanaged (VarRefFunc func);
151 [DllImport ("libtest")]
152 public static extern int mono_test_marshal_variant_out_byte_unmanaged (VarRefFunc func);
154 [DllImport ("libtest")]
155 public static extern int mono_test_marshal_variant_out_short_unmanaged (VarRefFunc func);
157 [DllImport ("libtest")]
158 public static extern int mono_test_marshal_variant_out_ushort_unmanaged (VarRefFunc func);
160 [DllImport ("libtest")]
161 public static extern int mono_test_marshal_variant_out_int_unmanaged (VarRefFunc func);
163 [DllImport ("libtest")]
164 public static extern int mono_test_marshal_variant_out_uint_unmanaged (VarRefFunc func);
166 [DllImport ("libtest")]
167 public static extern int mono_test_marshal_variant_out_long_unmanaged (VarRefFunc func);
169 [DllImport ("libtest")]
170 public static extern int mono_test_marshal_variant_out_ulong_unmanaged (VarRefFunc func);
172 [DllImport ("libtest")]
173 public static extern int mono_test_marshal_variant_out_float_unmanaged (VarRefFunc func);
175 [DllImport ("libtest")]
176 public static extern int mono_test_marshal_variant_out_double_unmanaged (VarRefFunc func);
178 [DllImport ("libtest")]
179 public static extern int mono_test_marshal_variant_out_bstr_unmanaged (VarRefFunc func);
181 [DllImport ("libtest")]
182 public static extern int mono_test_marshal_variant_out_bool_true_unmanaged (VarRefFunc func);
184 [DllImport ("libtest")]
185 public static extern int mono_test_marshal_variant_out_bool_false_unmanaged (VarRefFunc func);
187 [DllImport ("libtest")]
188 public static extern int mono_test_marshal_com_object_create (out IntPtr pUnk);
190 [DllImport ("libtest")]
191 public static extern int mono_test_marshal_com_object_same (out IntPtr pUnk);
193 [DllImport ("libtest")]
194 public static extern int mono_test_marshal_com_object_destroy (IntPtr pUnk);
196 [DllImport ("libtest")]
197 public static extern int mono_test_marshal_com_object_ref_count (IntPtr pUnk);
199 [DllImport ("libtest")]
200 public static extern int mono_test_marshal_ccw_identity ([MarshalAs (UnmanagedType.Interface)]ITest itest);
202 [DllImport ("libtest")]
203 public static extern int mono_test_marshal_ccw_reflexive ([MarshalAs (UnmanagedType.Interface)]ITest itest);
205 [DllImport ("libtest")]
206 public static extern int mono_test_marshal_ccw_transitive ([MarshalAs (UnmanagedType.Interface)]ITest itest);
208 [DllImport ("libtest")]
209 public static extern int mono_test_marshal_ccw_itest ([MarshalAs (UnmanagedType.Interface)]ITest itest);
211 [DllImport ("libtest")]
212 public static extern int mono_test_marshal_ccw_itest ([MarshalAs (UnmanagedType.Interface)]ITestPresSig itest);
214 public static int Main() {
216 bool isWindows = !(((int)Environment.OSVersion.Platform == 4) ||
217 ((int)Environment.OSVersion.Platform == 128));
218 if (isWindows) {
219 #region BSTR Tests
221 string str;
222 if (mono_test_marshal_bstr_in ("mono_test_marshal_bstr_in") != 0)
223 return 1;
224 if (mono_test_marshal_bstr_out (out str) != 0 || str != "mono_test_marshal_bstr_out")
225 return 2;
226 if (mono_test_marshal_bstr_in_null (null) != 0)
227 return 1;
228 if (mono_test_marshal_bstr_out_null (out str) != 0 || str != null)
229 return 2;
231 #endregion // BSTR Tests
233 #region VARIANT Tests
235 object obj;
236 if (mono_test_marshal_variant_in_sbyte ((sbyte)100) != 0)
237 return 13;
238 if (mono_test_marshal_variant_in_byte ((byte)100) != 0)
239 return 14;
240 if (mono_test_marshal_variant_in_short ((short)314) != 0)
241 return 15;
242 if (mono_test_marshal_variant_in_ushort ((ushort)314) != 0)
243 return 16;
244 if (mono_test_marshal_variant_in_int ((int)314) != 0)
245 return 17;
246 if (mono_test_marshal_variant_in_uint ((uint)314) != 0)
247 return 18;
248 if (mono_test_marshal_variant_in_long ((long)314) != 0)
249 return 19;
250 if (mono_test_marshal_variant_in_ulong ((ulong)314) != 0)
251 return 20;
252 if (mono_test_marshal_variant_in_float ((float)3.14) != 0)
253 return 21;
254 if (mono_test_marshal_variant_in_double ((double)3.14) != 0)
255 return 22;
256 if (mono_test_marshal_variant_in_bstr ("PI") != 0)
257 return 23;
258 if (mono_test_marshal_variant_out_sbyte (out obj) != 0 || (sbyte)obj != 100)
259 return 24;
260 if (mono_test_marshal_variant_out_byte (out obj) != 0 || (byte)obj != 100)
261 return 25;
262 if (mono_test_marshal_variant_out_short (out obj) != 0 || (short)obj != 314)
263 return 26;
264 if (mono_test_marshal_variant_out_ushort (out obj) != 0 || (ushort)obj != 314)
265 return 27;
266 if (mono_test_marshal_variant_out_int (out obj) != 0 || (int)obj != 314)
267 return 28;
268 if (mono_test_marshal_variant_out_uint (out obj) != 0 || (uint)obj != 314)
269 return 29;
270 if (mono_test_marshal_variant_out_long (out obj) != 0 || (long)obj != 314)
271 return 30;
272 if (mono_test_marshal_variant_out_ulong (out obj) != 0 || (ulong)obj != 314)
273 return 31;
274 if (mono_test_marshal_variant_out_float (out obj) != 0 || ((float)obj - 3.14) / 3.14 > .001)
275 return 32;
276 if (mono_test_marshal_variant_out_double (out obj) != 0 || ((double)obj - 3.14) / 3.14 > .001)
277 return 33;
278 if (mono_test_marshal_variant_out_bstr (out obj) != 0 || (string)obj != "PI")
279 return 34;
281 VarFunc func = new VarFunc (mono_test_marshal_variant_in_callback);
282 if (mono_test_marshal_variant_in_sbyte_unmanaged (func) != 0)
283 return 35;
284 if (mono_test_marshal_variant_in_byte_unmanaged (func) != 0)
285 return 36;
286 if (mono_test_marshal_variant_in_short_unmanaged (func) != 0)
287 return 37;
288 if (mono_test_marshal_variant_in_ushort_unmanaged (func) != 0)
289 return 38;
290 if (mono_test_marshal_variant_in_int_unmanaged (func) != 0)
291 return 39;
292 if (mono_test_marshal_variant_in_uint_unmanaged (func) != 0)
293 return 40;
294 if (mono_test_marshal_variant_in_long_unmanaged (func) != 0)
295 return 41;
296 if (mono_test_marshal_variant_in_ulong_unmanaged (func) != 0)
297 return 42;
298 if (mono_test_marshal_variant_in_float_unmanaged (func) != 0)
299 return 43;
300 if (mono_test_marshal_variant_in_double_unmanaged (func) != 0)
301 return 44;
302 if (mono_test_marshal_variant_in_bstr_unmanaged (func) != 0)
303 return 45;
304 if (mono_test_marshal_variant_in_bool_true_unmanaged (func) != 0)
305 return 46;
307 VarRefFunc reffunc = new VarRefFunc (mono_test_marshal_variant_out_callback);
308 if (mono_test_marshal_variant_out_sbyte_unmanaged (reffunc) != 0)
309 return 50;
310 if (mono_test_marshal_variant_out_byte_unmanaged (reffunc) != 0)
311 return 51;
312 if (mono_test_marshal_variant_out_short_unmanaged (reffunc) != 0)
313 return 52;
314 if (mono_test_marshal_variant_out_ushort_unmanaged (reffunc) != 0)
315 return 53;
316 if (mono_test_marshal_variant_out_int_unmanaged (reffunc) != 0)
317 return 54;
318 if (mono_test_marshal_variant_out_uint_unmanaged (reffunc) != 0)
319 return 55;
320 if (mono_test_marshal_variant_out_long_unmanaged (reffunc) != 0)
321 return 56;
322 if (mono_test_marshal_variant_out_ulong_unmanaged (reffunc) != 0)
323 return 57;
324 if (mono_test_marshal_variant_out_float_unmanaged (reffunc) != 0)
325 return 58;
326 if (mono_test_marshal_variant_out_double_unmanaged (reffunc) != 0)
327 return 59;
328 if (mono_test_marshal_variant_out_bstr_unmanaged (reffunc) != 0)
329 return 60;
330 if (mono_test_marshal_variant_out_bool_true_unmanaged (reffunc) != 0)
331 return 61;
333 #endregion // VARIANT Tests
335 #region Runtime Callable Wrapper Tests
337 IntPtr pUnk;
338 if (mono_test_marshal_com_object_create (out pUnk) != 0)
339 return 145;
341 if (mono_test_marshal_com_object_ref_count (pUnk) != 1)
342 return 146;
344 if (Marshal.AddRef (pUnk) != 2)
345 return 147;
347 if (mono_test_marshal_com_object_ref_count (pUnk) != 2)
348 return 148;
350 if (Marshal.Release (pUnk) != 1)
351 return 149;
353 if (mono_test_marshal_com_object_ref_count (pUnk) != 1)
354 return 150;
356 object com_obj = Marshal.GetObjectForIUnknown (pUnk);
358 if (com_obj == null)
359 return 151;
361 ITest itest = com_obj as ITest;
363 if (itest == null)
364 return 152;
366 IntPtr pUnk2;
367 if (mono_test_marshal_com_object_same (out pUnk2) != 0)
368 return 153;
370 object com_obj2 = Marshal.GetObjectForIUnknown (pUnk2);
372 if (com_obj != com_obj2)
373 return 154;
375 if (!com_obj.Equals (com_obj2))
376 return 155;
378 IntPtr pUnk3;
379 if (mono_test_marshal_com_object_create (out pUnk3) != 0)
380 return 156;
382 object com_obj3 = Marshal.GetObjectForIUnknown (pUnk3);
383 if (com_obj == com_obj3)
384 return 157;
386 if (com_obj.Equals (com_obj3))
387 return 158;
389 // com_obj & com_obj2 share a RCW
390 if (Marshal.ReleaseComObject (com_obj2) != 1)
391 return 159;
393 // com_obj3 should only have one RCW
394 if (Marshal.ReleaseComObject (com_obj3) != 0)
395 return 160;
397 IntPtr iunknown = Marshal.GetIUnknownForObject (com_obj);
398 if (iunknown == IntPtr.Zero)
399 return 170;
401 if (pUnk != iunknown)
402 return 171;
404 if (TestITest (itest) != 0)
405 return 172;
407 if (TestITestPresSig (itest as ITestPresSig) != 0)
408 return 173;
410 #endregion // Runtime Callable Wrapper Tests
412 #region COM Callable Wrapper Tests
414 ManagedTest test = new ManagedTest ();
416 mono_test_marshal_ccw_itest (test);
418 if (test.Status != 0)
419 return 200;
421 ManagedTestPresSig test_pres_sig = new ManagedTestPresSig ();
423 mono_test_marshal_ccw_itest (test_pres_sig);
425 #endregion // COM Callable Wrapper Tests
428 return 0;
432 [ComImport ()]
433 [Guid ("00000000-0000-0000-0000-000000000001")]
434 [InterfaceType (ComInterfaceType.InterfaceIsIUnknown)]
435 public interface ITest
437 // properties need to go first since mcs puts them there
438 ITest Test
440 [return: MarshalAs (UnmanagedType.Interface)]
441 [MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId (5242884)]
442 get;
445 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
446 void SByteIn (sbyte val);
447 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
448 void ByteIn (byte val);
449 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
450 void ShortIn (short val);
451 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
452 void UShortIn (ushort val);
453 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
454 void IntIn (int val);
455 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
456 void UIntIn (uint val);
457 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
458 void LongIn (long val);
459 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
460 void ULongIn (ulong val);
461 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
462 void FloatIn (float val);
463 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
464 void DoubleIn (double val);
465 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
466 void ITestIn ([MarshalAs (UnmanagedType.Interface)]ITest val);
467 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
468 void ITestOut ([MarshalAs (UnmanagedType.Interface)]out ITest val);
471 [ComImport ()]
472 [Guid ("00000000-0000-0000-0000-000000000001")]
473 [InterfaceType (ComInterfaceType.InterfaceIsIUnknown)]
474 public interface ITestPresSig
476 // properties need to go first since mcs puts them there
477 ITestPresSig Test
479 [return: MarshalAs (UnmanagedType.Interface)]
480 [MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId (5242884)]
481 get;
484 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
485 [PreserveSig ()]
486 int SByteIn (sbyte val);
487 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
488 [PreserveSig ()]
489 int ByteIn (byte val);
490 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
491 [PreserveSig ()]
492 int ShortIn (short val);
493 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
494 [PreserveSig ()]
495 int UShortIn (ushort val);
496 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
497 [PreserveSig ()]
498 int IntIn (int val);
499 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
500 [PreserveSig ()]
501 int UIntIn (uint val);
502 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
503 [PreserveSig ()]
504 int LongIn (long val);
505 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
506 [PreserveSig ()]
507 int ULongIn (ulong val);
508 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
509 [PreserveSig ()]
510 int FloatIn (float val);
511 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
512 [PreserveSig ()]
513 int DoubleIn (double val);
514 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
515 [PreserveSig ()]
516 int ITestIn ([MarshalAs (UnmanagedType.Interface)]ITestPresSig val);
517 [MethodImplAttribute (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
518 [PreserveSig ()]
519 int ITestOut ([MarshalAs (UnmanagedType.Interface)]out ITestPresSig val);
522 public class ManagedTestPresSig : ITestPresSig
523 { // properties need to go first since mcs puts them there
524 public ITestPresSig Test
528 return new ManagedTestPresSig ();
532 public int SByteIn (sbyte val)
534 if (val != -100)
535 return 1;
536 return 0;
539 public int ByteIn (byte val)
541 if (val != 100)
542 return 2;
543 return 0;
546 public int ShortIn (short val)
548 if (val != -100)
549 return 3;
550 return 0;
553 public int UShortIn (ushort val)
555 if (val != 100)
556 return 4;
557 return 0;
560 public int IntIn (int val)
562 if (val != -100)
563 return 5;
564 return 0;
567 public int UIntIn (uint val)
569 if (val != 100)
570 return 6;
571 return 0;
574 public int LongIn (long val)
576 if (val != -100)
577 return 7;
578 return 0;
581 public int ULongIn (ulong val)
583 if (val != 100)
584 return 8;
585 return 0;
588 public int FloatIn (float val)
590 if (Math.Abs (val - 3.14f) > .000001)
591 return 9;
592 return 0;
595 public int DoubleIn (double val)
597 if (Math.Abs (val - 3.14f) > .000001)
598 return 10;
599 return 0;
602 public int ITestIn ([MarshalAs (UnmanagedType.Interface)]ITestPresSig val)
604 if (val == null)
605 return 11;
606 if (null == val as ManagedTestPresSig)
607 return 12;
608 return 0;
611 public int ITestOut ([MarshalAs (UnmanagedType.Interface)]out ITestPresSig val)
613 val = new ManagedTestPresSig ();
614 return 0;
618 public class ManagedTest : ITest
620 private int status = 0;
621 public int Status
623 get { return status; }
625 public void SByteIn (sbyte val)
627 if (val != -100)
628 status = 1;
631 public void ByteIn (byte val)
633 if (val != 100)
634 status = 2;
637 public void ShortIn (short val)
639 if (val != -100)
640 status = 3;
643 public void UShortIn (ushort val)
645 if (val != 100)
646 status = 4;
649 public void IntIn (int val)
651 if (val != -100)
652 status = 5;
655 public void UIntIn (uint val)
657 if (val != 100)
658 status = 6;
661 public void LongIn (long val)
663 if (val != -100)
664 status = 7;
667 public void ULongIn (ulong val)
669 if (val != 100)
670 status = 8;
673 public void FloatIn (float val)
675 if (Math.Abs (val - 3.14f) > .000001)
676 status = 9;
679 public void DoubleIn (double val)
681 if (Math.Abs (val - 3.14) > .000001)
682 status = 10;
685 public void ITestIn (ITest val)
687 if (val == null)
688 status = 11;
689 if (null == val as ManagedTest)
690 status = 12;
693 public void ITestOut (out ITest val)
695 val = new ManagedTest ();
698 public ITest Test
702 return new ManagedTest ();
707 public static int mono_test_marshal_variant_in_callback (VarEnum vt, object obj)
709 switch (vt)
711 case VarEnum.VT_I1:
712 if (obj.GetType () != typeof (sbyte))
713 return 1;
714 if ((sbyte)obj != -100)
715 return 2;
716 break;
717 case VarEnum.VT_UI1:
718 if (obj.GetType () != typeof (byte))
719 return 1;
720 if ((byte)obj != 100)
721 return 2;
722 break;
723 case VarEnum.VT_I2:
724 if (obj.GetType () != typeof (short))
725 return 1;
726 if ((short)obj != -100)
727 return 2;
728 break;
729 case VarEnum.VT_UI2:
730 if (obj.GetType () != typeof (ushort))
731 return 1;
732 if ((ushort)obj != 100)
733 return 2;
734 break;
735 case VarEnum.VT_I4:
736 if (obj.GetType () != typeof (int))
737 return 1;
738 if ((int)obj != -100)
739 return 2;
740 break;
741 case VarEnum.VT_UI4:
742 if (obj.GetType () != typeof (uint))
743 return 1;
744 if ((uint)obj != 100)
745 return 2;
746 break;
747 case VarEnum.VT_I8:
748 if (obj.GetType () != typeof (long))
749 return 1;
750 if ((long)obj != -100)
751 return 2;
752 break;
753 case VarEnum.VT_UI8:
754 if (obj.GetType () != typeof (ulong))
755 return 1;
756 if ((ulong)obj != 100)
757 return 2;
758 break;
759 case VarEnum.VT_R4:
760 if (obj.GetType () != typeof (float))
761 return 1;
762 if (Math.Abs ((float)obj - 3.14f) > 1e-10)
763 return 2;
764 break;
765 case VarEnum.VT_R8:
766 if (obj.GetType () != typeof (double))
767 return 1;
768 if (Math.Abs ((double)obj - 3.14) > 1e-10)
769 return 2;
770 break;
771 case VarEnum.VT_BSTR:
772 if (obj.GetType () != typeof (string))
773 return 1;
774 if ((string)obj != "PI")
775 return 2;
776 break;
777 case VarEnum.VT_BOOL:
778 if (obj.GetType () != typeof (bool))
779 return 1;
780 if ((bool)obj != true)
781 return 2;
782 break;
784 return 0;
787 public static int mono_test_marshal_variant_out_callback (VarEnum vt, ref object obj)
789 switch (vt) {
790 case VarEnum.VT_I1:
791 obj = (sbyte)-100;
792 break;
793 case VarEnum.VT_UI1:
794 obj = (byte)100;
795 break;
796 case VarEnum.VT_I2:
797 obj = (short)-100;
798 break;
799 case VarEnum.VT_UI2:
800 obj = (ushort)100;
801 break;
802 case VarEnum.VT_I4:
803 obj = (int)-100;
804 break;
805 case VarEnum.VT_UI4:
806 obj = (uint)100;
807 break;
808 case VarEnum.VT_I8:
809 obj = (long)-100;
810 break;
811 case VarEnum.VT_UI8:
812 obj = (ulong)100;
813 break;
814 case VarEnum.VT_R4:
815 obj = (float)3.14f;
816 break;
817 case VarEnum.VT_R8:
818 obj = (double)3.14;
819 break;
820 case VarEnum.VT_BSTR:
821 obj = "PI";
822 break;
823 case VarEnum.VT_BOOL:
824 obj = true;
825 break;
827 return 0;
830 public static int TestITest (ITest itest)
832 try {
833 ITest itest2;
834 itest.SByteIn (-100);
835 itest.ByteIn (100);
836 itest.ShortIn (-100);
837 itest.UShortIn (100);
838 itest.IntIn (-100);
839 itest.UIntIn (100);
840 itest.LongIn (-100);
841 itest.ULongIn (100);
842 itest.FloatIn (3.14f);
843 itest.DoubleIn (3.14);
844 itest.ITestIn (itest);
845 itest.ITestOut (out itest2);
847 catch (Exception ex) {
848 return 1;
850 return 0;
853 public static int TestITestPresSig (ITestPresSig itest)
855 ITestPresSig itest2;
856 if (itest.SByteIn (-100) != 0)
857 return 1000;
858 if (itest.ByteIn (100) != 0)
859 return 1001;
860 if (itest.ShortIn (-100) != 0)
861 return 1002;
862 if (itest.UShortIn (100) != 0)
863 return 1003;
864 if (itest.IntIn (-100) != 0)
865 return 1004;
866 if (itest.UIntIn (100) != 0)
867 return 1005;
868 if (itest.LongIn (-100) != 0)
869 return 1006;
870 if (itest.ULongIn (100) != 0)
871 return 1007;
872 if (itest.FloatIn (3.14f) != 0)
873 return 1008;
874 if (itest.DoubleIn (3.14) != 0)
875 return 1009;
876 if (itest.ITestIn (itest) != 0)
877 return 1010;
878 if (itest.ITestOut (out itest2) != 0)
879 return 1011;
880 return 0;