2009-05-22 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / tests / cominterop.cs
1 //
2 // cominterop.cs:
3 //
4 //  Tests for COM Interop related features
5 //
6
7 using System;
8 using System.Runtime.CompilerServices;
9 using System.Runtime.InteropServices;
10
11 public class Tests
12 {
13
14         [DllImport("libtest")]
15         public static extern int mono_test_marshal_bstr_in([MarshalAs(UnmanagedType.BStr)]string str);
16
17         [DllImport("libtest")]
18     public static extern int mono_test_marshal_bstr_out([MarshalAs(UnmanagedType.BStr)] out string str);
19
20     [DllImport("libtest")]
21     public static extern int mono_test_marshal_bstr_in_null([MarshalAs(UnmanagedType.BStr)]string str);
22
23     [DllImport("libtest")]
24     public static extern int mono_test_marshal_bstr_out_null([MarshalAs(UnmanagedType.BStr)] out string str);
25
26         [DllImport("libtest")]
27         public static extern int mono_test_marshal_variant_in_sbyte([MarshalAs(UnmanagedType.Struct)]object obj);
28
29         [DllImport("libtest")]
30         public static extern int mono_test_marshal_variant_in_byte([MarshalAs(UnmanagedType.Struct)]object obj);
31
32         [DllImport("libtest")]
33         public static extern int mono_test_marshal_variant_in_short([MarshalAs(UnmanagedType.Struct)]object obj);
34
35         [DllImport("libtest")]
36         public static extern int mono_test_marshal_variant_in_ushort([MarshalAs(UnmanagedType.Struct)]object obj);
37
38         [DllImport("libtest")]
39         public static extern int mono_test_marshal_variant_in_int([MarshalAs(UnmanagedType.Struct)]object obj);
40
41         [DllImport("libtest")]
42         public static extern int mono_test_marshal_variant_in_uint([MarshalAs(UnmanagedType.Struct)]object obj);
43
44         [DllImport("libtest")]
45         public static extern int mono_test_marshal_variant_in_long([MarshalAs(UnmanagedType.Struct)]object obj);
46
47         [DllImport("libtest")]
48         public static extern int mono_test_marshal_variant_in_ulong([MarshalAs(UnmanagedType.Struct)]object obj);
49
50         [DllImport("libtest")]
51         public static extern int mono_test_marshal_variant_in_float([MarshalAs(UnmanagedType.Struct)]object obj);
52
53         [DllImport("libtest")]
54         public static extern int mono_test_marshal_variant_in_double([MarshalAs(UnmanagedType.Struct)]object obj);
55
56         [DllImport("libtest")]
57         public static extern int mono_test_marshal_variant_in_bstr ([MarshalAs (UnmanagedType.Struct)]object obj);
58
59         [DllImport ("libtest")]
60         public static extern int mono_test_marshal_variant_in_bool_true ([MarshalAs (UnmanagedType.Struct)]object obj);
61
62         [DllImport ("libtest")]
63         public static extern int mono_test_marshal_variant_in_bool_false ([MarshalAs (UnmanagedType.Struct)]object obj);
64
65         [DllImport("libtest")]
66         public static extern int mono_test_marshal_variant_out_sbyte([MarshalAs(UnmanagedType.Struct)]out object obj);
67
68         [DllImport("libtest")]
69         public static extern int mono_test_marshal_variant_out_byte([MarshalAs(UnmanagedType.Struct)]out object obj);
70
71         [DllImport("libtest")]
72         public static extern int mono_test_marshal_variant_out_short([MarshalAs(UnmanagedType.Struct)]out object obj);
73
74         [DllImport("libtest")]
75         public static extern int mono_test_marshal_variant_out_ushort([MarshalAs(UnmanagedType.Struct)]out object obj);
76
77         [DllImport("libtest")]
78         public static extern int mono_test_marshal_variant_out_int([MarshalAs(UnmanagedType.Struct)]out object obj);
79
80         [DllImport("libtest")]
81         public static extern int mono_test_marshal_variant_out_uint([MarshalAs(UnmanagedType.Struct)]out object obj);
82
83         [DllImport("libtest")]
84         public static extern int mono_test_marshal_variant_out_long([MarshalAs(UnmanagedType.Struct)]out object obj);
85
86         [DllImport("libtest")]
87         public static extern int mono_test_marshal_variant_out_ulong([MarshalAs(UnmanagedType.Struct)]out object obj);
88
89         [DllImport("libtest")]
90         public static extern int mono_test_marshal_variant_out_float([MarshalAs(UnmanagedType.Struct)]out object obj);
91
92         [DllImport("libtest")]
93         public static extern int mono_test_marshal_variant_out_double([MarshalAs(UnmanagedType.Struct)]out object obj);
94
95         [DllImport("libtest")]
96         public static extern int mono_test_marshal_variant_out_bstr ([MarshalAs (UnmanagedType.Struct)]out object obj);
97
98         [DllImport("libtest")]
99         public static extern int mono_test_marshal_variant_out_bool_true ([MarshalAs (UnmanagedType.Struct)]out object obj);
100
101         [DllImport ("libtest")]
102         public static extern int mono_test_marshal_variant_out_bool_false ([MarshalAs (UnmanagedType.Struct)]out object obj);
103
104
105         public delegate int VarFunc (VarEnum vt, [MarshalAs (UnmanagedType.Struct)] object obj);
106
107         public delegate int VarRefFunc (VarEnum vt, [MarshalAs (UnmanagedType.Struct)] ref object obj);
108
109         [DllImport ("libtest")]
110         public static extern int mono_test_marshal_variant_in_sbyte_unmanaged (VarFunc func);
111
112         [DllImport ("libtest")]
113         public static extern int mono_test_marshal_variant_in_byte_unmanaged (VarFunc func);
114
115         [DllImport ("libtest")]
116         public static extern int mono_test_marshal_variant_in_short_unmanaged (VarFunc func);
117
118         [DllImport ("libtest")]
119         public static extern int mono_test_marshal_variant_in_ushort_unmanaged (VarFunc func);
120
121         [DllImport ("libtest")]
122         public static extern int mono_test_marshal_variant_in_int_unmanaged (VarFunc func);
123
124         [DllImport ("libtest")]
125         public static extern int mono_test_marshal_variant_in_uint_unmanaged (VarFunc func);
126
127         [DllImport ("libtest")]
128         public static extern int mono_test_marshal_variant_in_long_unmanaged (VarFunc func);
129
130         [DllImport ("libtest")]
131         public static extern int mono_test_marshal_variant_in_ulong_unmanaged (VarFunc func);
132
133         [DllImport ("libtest")]
134         public static extern int mono_test_marshal_variant_in_float_unmanaged (VarFunc func);
135
136         [DllImport ("libtest")]
137         public static extern int mono_test_marshal_variant_in_double_unmanaged (VarFunc func);
138
139         [DllImport ("libtest")]
140         public static extern int mono_test_marshal_variant_in_bstr_unmanaged (VarFunc func);
141
142         [DllImport ("libtest")]
143         public static extern int mono_test_marshal_variant_in_bool_true_unmanaged (VarFunc func);
144
145         [DllImport ("libtest")]
146         public static extern int mono_test_marshal_variant_in_bool_false_unmanaged (VarFunc func);
147
148         [DllImport ("libtest")]
149         public static extern int mono_test_marshal_variant_out_sbyte_unmanaged (VarRefFunc func);
150
151         [DllImport ("libtest")]
152         public static extern int mono_test_marshal_variant_out_byte_unmanaged (VarRefFunc func);
153
154         [DllImport ("libtest")]
155         public static extern int mono_test_marshal_variant_out_short_unmanaged (VarRefFunc func);
156
157         [DllImport ("libtest")]
158         public static extern int mono_test_marshal_variant_out_ushort_unmanaged (VarRefFunc func);
159
160         [DllImport ("libtest")]
161         public static extern int mono_test_marshal_variant_out_int_unmanaged (VarRefFunc func);
162
163         [DllImport ("libtest")]
164         public static extern int mono_test_marshal_variant_out_uint_unmanaged (VarRefFunc func);
165
166         [DllImport ("libtest")]
167         public static extern int mono_test_marshal_variant_out_long_unmanaged (VarRefFunc func);
168
169         [DllImport ("libtest")]
170         public static extern int mono_test_marshal_variant_out_ulong_unmanaged (VarRefFunc func);
171
172         [DllImport ("libtest")]
173         public static extern int mono_test_marshal_variant_out_float_unmanaged (VarRefFunc func);
174
175         [DllImport ("libtest")]
176         public static extern int mono_test_marshal_variant_out_double_unmanaged (VarRefFunc func);
177
178         [DllImport ("libtest")]
179         public static extern int mono_test_marshal_variant_out_bstr_unmanaged (VarRefFunc func);
180
181         [DllImport ("libtest")]
182         public static extern int mono_test_marshal_variant_out_bool_true_unmanaged (VarRefFunc func);
183
184         [DllImport ("libtest")]
185         public static extern int mono_test_marshal_variant_out_bool_false_unmanaged (VarRefFunc func);
186
187     [DllImport ("libtest")]
188         public static extern int mono_test_marshal_com_object_create (out IntPtr pUnk);
189
190         [DllImport ("libtest")]
191         public static extern int mono_test_marshal_com_object_same (out IntPtr pUnk);
192
193     [DllImport ("libtest")]
194     public static extern int mono_test_marshal_com_object_destroy (IntPtr pUnk);
195
196         [DllImport ("libtest")]
197         public static extern int mono_test_marshal_com_object_ref_count (IntPtr pUnk);
198
199         [DllImport ("libtest")]
200         public static extern int mono_test_marshal_ccw_identity ([MarshalAs (UnmanagedType.Interface)]ITest itest);
201
202         [DllImport ("libtest")]
203         public static extern int mono_test_marshal_ccw_reflexive ([MarshalAs (UnmanagedType.Interface)]ITest itest);
204
205         [DllImport ("libtest")]
206         public static extern int mono_test_marshal_ccw_transitive ([MarshalAs (UnmanagedType.Interface)]ITest itest);
207
208         [DllImport ("libtest")]
209         public static extern int mono_test_marshal_ccw_itest ([MarshalAs (UnmanagedType.Interface)]ITest itest);
210
211         [DllImport ("libtest")]
212         public static extern int mono_test_marshal_ccw_itest ([MarshalAs (UnmanagedType.Interface)]ITestPresSig itest);
213
214         public static int Main() {
215
216         bool isWindows = !(((int)Environment.OSVersion.Platform == 4) || 
217             ((int)Environment.OSVersion.Platform == 128));
218                 if (isWindows) {
219                         #region BSTR Tests
220
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;
230
231                         #endregion // BSTR Tests
232
233                         #region VARIANT Tests
234
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;
280
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;
306
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;
332
333                         #endregion // VARIANT Tests
334
335                         #region Runtime Callable Wrapper Tests
336
337                         IntPtr pUnk;
338                         if (mono_test_marshal_com_object_create (out pUnk) != 0)
339                                 return 145;
340
341                         if (mono_test_marshal_com_object_ref_count (pUnk) != 1)
342                                 return 146;
343
344                         if (Marshal.AddRef (pUnk) != 2)
345                                 return 147;
346
347                         if (mono_test_marshal_com_object_ref_count (pUnk) != 2)
348                                 return 148;
349
350                         if (Marshal.Release (pUnk) != 1)
351                                 return 149;
352
353                         if (mono_test_marshal_com_object_ref_count (pUnk) != 1)
354                                 return 150;
355
356                         object com_obj = Marshal.GetObjectForIUnknown (pUnk);
357
358                         if (com_obj == null)
359                                 return 151;
360
361                         ITest itest = com_obj as ITest;
362
363                         if (itest == null)
364                                 return 152;
365
366                         IntPtr pUnk2;
367                         if (mono_test_marshal_com_object_same (out pUnk2) != 0)
368                                 return 153;
369
370                         object com_obj2 = Marshal.GetObjectForIUnknown (pUnk2);
371                         
372                         if (com_obj != com_obj2)
373                                 return 154;
374
375                         if (!com_obj.Equals (com_obj2))
376                                 return 155;
377
378                         IntPtr pUnk3;
379                         if (mono_test_marshal_com_object_create (out pUnk3) != 0)
380                                 return 156;
381
382                         object com_obj3 = Marshal.GetObjectForIUnknown (pUnk3);
383                         if (com_obj == com_obj3)
384                                 return 157;
385
386                         if (com_obj.Equals (com_obj3))
387                                 return 158;
388
389                         // com_obj & com_obj2 share a RCW
390                         if (Marshal.ReleaseComObject (com_obj2) != 1)
391                                 return 159;
392
393                         // com_obj3 should only have one RCW
394                         if (Marshal.ReleaseComObject (com_obj3) != 0)
395                                 return 160;
396
397                         IntPtr iunknown = Marshal.GetIUnknownForObject (com_obj);
398                         if (iunknown == IntPtr.Zero)
399                                 return 170;
400
401                         if (pUnk != iunknown)
402                                 return 171;
403
404                         if (TestITest (itest) != 0)
405                                 return 172;
406
407                         if (TestITestPresSig (itest as ITestPresSig) != 0)
408                                 return 173;
409
410                         #endregion // Runtime Callable Wrapper Tests
411
412                         #region COM Callable Wrapper Tests
413
414                         ManagedTest test = new ManagedTest ();
415
416                         mono_test_marshal_ccw_itest (test);
417
418                         if (test.Status != 0)
419                                 return 200;
420
421                         ManagedTestPresSig test_pres_sig = new ManagedTestPresSig ();
422
423                         mono_test_marshal_ccw_itest (test_pres_sig);
424
425                         #endregion // COM Callable Wrapper Tests
426                 }
427
428         return 0;
429         }
430
431
432         [ComImport ()]
433         [Guid ("00000000-0000-0000-0000-000000000001")]
434         [InterfaceType (ComInterfaceType.InterfaceIsIUnknown)]
435         public interface ITest
436         {
437                 // properties need to go first since mcs puts them there
438                 ITest Test
439                 {
440                         [return: MarshalAs (UnmanagedType.Interface)]
441                         [MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId (5242884)]
442                         get;
443                 }
444
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);
469         }
470
471         [ComImport ()]
472         [Guid ("00000000-0000-0000-0000-000000000001")]
473         [InterfaceType (ComInterfaceType.InterfaceIsIUnknown)]
474         public interface ITestPresSig
475         {
476                 // properties need to go first since mcs puts them there
477                 ITestPresSig Test
478                 {
479                         [return: MarshalAs (UnmanagedType.Interface)]
480                         [MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId (5242884)]
481                         get;
482                 }
483
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);
520         }
521
522         public class ManagedTestPresSig : ITestPresSig
523         {               // properties need to go first since mcs puts them there
524                 public ITestPresSig Test
525                 {
526                         get
527                         {
528                                 return new ManagedTestPresSig ();
529                         }
530                 }
531
532                 public int SByteIn (sbyte val)
533                 {
534                         if (val != -100)
535                                 return 1;
536                         return 0;
537                 }
538
539                 public int ByteIn (byte val)
540                 {
541                         if (val != 100)
542                                 return 2;
543                         return 0;
544                 }
545
546                 public int ShortIn (short val)
547                 {
548                         if (val != -100)
549                                 return 3;
550                         return 0;
551                 }
552
553                 public int UShortIn (ushort val)
554                 {
555                         if (val != 100)
556                                 return 4;
557                         return 0;
558                 }
559
560                 public int IntIn (int val)
561                 {
562                         if (val != -100)
563                                 return 5;
564                         return 0;
565                 }
566
567                 public int UIntIn (uint val)
568                 {
569                         if (val != 100)
570                                 return 6;
571                         return 0;
572                 }
573
574                 public int LongIn (long val)
575                 {
576                         if (val != -100)
577                                 return 7;
578                         return 0;
579                 }
580
581                 public int ULongIn (ulong val)
582                 {
583                         if (val != 100)
584                                 return 8;
585                         return 0;
586                 }
587
588                 public int FloatIn (float val)
589                 {
590                         if (Math.Abs (val - 3.14f) > .000001)
591                                 return 9;
592                         return 0;
593                 }
594
595                 public int DoubleIn (double val)
596                 {
597                         if (Math.Abs (val - 3.14f) > .000001)
598                                 return 10;
599                         return 0;
600                 }
601
602                 public int ITestIn ([MarshalAs (UnmanagedType.Interface)]ITestPresSig val)
603                 {
604                         if (val == null)
605                                 return 11;
606                         if (null == val as ManagedTestPresSig)
607                                 return 12;
608                         return 0;
609                 }
610
611                 public int ITestOut ([MarshalAs (UnmanagedType.Interface)]out ITestPresSig val)
612                 {
613                         val = new ManagedTestPresSig ();
614                         return 0;
615                 }
616         }
617
618         public class ManagedTest : ITest
619         {
620                 private int status = 0;
621                 public int Status
622                 {
623                         get { return status; }
624                 }
625                 public void SByteIn (sbyte val)
626                 {
627                         if (val != -100)
628                                 status = 1;
629                 }
630
631                 public void ByteIn (byte val)
632                 {
633                         if (val != 100)
634                                 status = 2;
635                 }
636
637                 public void ShortIn (short val)
638                 {
639                         if (val != -100)
640                                 status = 3;
641                 }
642
643                 public void UShortIn (ushort val)
644                 {
645                         if (val != 100)
646                                 status = 4;
647                 }
648
649                 public void IntIn (int val)
650                 {
651                         if (val != -100)
652                                 status = 5;
653                 }
654
655                 public void UIntIn (uint val)
656                 {
657                         if (val != 100)
658                                 status = 6;
659                 }
660
661                 public void LongIn (long val)
662                 {
663                         if (val != -100)
664                                 status = 7;
665                 }
666
667                 public void ULongIn (ulong val)
668                 {
669                         if (val != 100)
670                                 status = 8;
671                 }
672
673                 public void FloatIn (float val)
674                 {
675                         if (Math.Abs (val - 3.14f) > .000001)
676                                 status = 9;
677                 }
678
679                 public void DoubleIn (double val)
680                 {
681                         if (Math.Abs (val - 3.14) > .000001)
682                                 status = 10;
683                 }
684
685                 public void ITestIn (ITest val)
686                 {
687                         if (val == null)
688                                 status = 11;
689                         if (null == val as ManagedTest)
690                                 status = 12;
691                 }
692
693                 public void ITestOut (out ITest val)
694                 {
695                         val = new ManagedTest ();
696                 }
697
698                 public ITest Test
699                 {
700                         get
701                         {
702                                 return new ManagedTest ();
703                         }
704                 }
705         }
706
707         public static int mono_test_marshal_variant_in_callback (VarEnum vt, object obj)
708         {
709                 switch (vt)
710                 {
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;
783                 }
784                 return 0;
785         }
786
787         public static int mono_test_marshal_variant_out_callback (VarEnum vt, ref object obj)
788         {
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;
826                 }
827                 return 0;
828         }
829
830         public static int TestITest (ITest itest)
831         {
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);
846                 }
847                 catch (Exception ex) {
848                         return 1;
849                 }
850                 return 0;
851         }
852
853         public static int TestITestPresSig (ITestPresSig itest)
854         {
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;
881         }
882 }