Merge pull request #2003 from esdrubal/seq_test_fix2
[mono.git] / mono / tests / libtest.c
1 #include <config.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <glib.h>
6 #include <gmodule.h>
7 #include <errno.h>
8 #include <time.h>
9 #include <math.h>
10
11 #ifdef WIN32
12 #include <windows.h>
13 #include "initguid.h"
14 #else
15 #include <pthread.h>
16 #endif
17
18 #ifdef WIN32
19 #define STDCALL __stdcall
20 #else
21 #define STDCALL
22 #endif
23
24 #ifdef __GNUC__
25 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
26 #endif
27
28 #ifdef WIN32
29 extern __declspec(dllimport) void __stdcall CoTaskMemFree(void *ptr);
30 #endif
31
32 typedef int (STDCALL *SimpleDelegate) (int a);
33
34 #if defined(WIN32) && defined (_MSC_VER)
35 #define LIBTEST_API __declspec(dllexport)
36 #elif defined(__GNUC__)
37 #define LIBTEST_API  __attribute__ ((visibility ("default")))
38 #else
39 #define LIBTEST_API
40 #endif
41
42 static void marshal_free (void *ptr)
43 {
44 #ifdef WIN32
45         CoTaskMemFree (ptr);
46 #else
47         g_free (ptr);
48 #endif
49 }
50
51 static void* marshal_alloc (gsize size)
52 {
53 #ifdef WIN32
54         return CoTaskMemAlloc (size);
55 #else
56         return g_malloc (size);
57 #endif
58 }
59
60 static void* marshal_alloc0 (gsize size)
61 {
62 #ifdef WIN32
63         void* ptr = CoTaskMemAlloc (size);
64         memset(ptr, 0, size);
65         return ptr;
66 #else
67         return g_malloc0 (size);
68 #endif
69 }
70
71 static char* marshal_strdup (const char *str)
72 {
73 #ifdef WIN32
74         int len;
75         char *buf;
76
77         if (!str)
78                 return NULL;
79
80         len = strlen (str);
81         buf = (char *) CoTaskMemAlloc (len + 1);
82         return strcpy (buf, str);
83 #else
84         return g_strdup (str);
85 #endif
86 }
87
88 static gunichar2* marshal_bstr_alloc(const gchar* str)
89 {
90 #ifdef WIN32
91         gunichar2* ret = NULL;
92         gunichar2* temp = NULL;
93         temp = g_utf8_to_utf16 (str, -1, NULL, NULL, NULL);
94         ret = SysAllocString (temp);
95         g_free (temp);
96         return ret;
97 #else
98         gchar* ret = NULL;
99         int slen = strlen (str);
100         gunichar2* temp;
101         /* allocate len + 1 utf16 characters plus 4 byte integer for length*/
102         ret = g_malloc ((slen + 1) * sizeof(gunichar2) + sizeof(guint32));
103         if (ret == NULL)
104                 return NULL;
105         temp = g_utf8_to_utf16 (str, -1, NULL, NULL, NULL);
106         memcpy (ret + sizeof(guint32), temp, slen * sizeof(gunichar2));
107         * ((guint32 *) ret) = slen * sizeof(gunichar2);
108         ret [4 + slen * sizeof(gunichar2)] = 0;
109         ret [5 + slen * sizeof(gunichar2)] = 0;
110
111         return (gunichar2*)(ret + 4);
112 #endif
113 }
114
115 #define marshal_new0(type,size)       ((type *) marshal_alloc0 (sizeof (type)* (size)))
116
117 LIBTEST_API int STDCALL
118 mono_cominterop_is_supported (void)
119 {
120 #if defined(TARGET_X86) || defined(TARGET_AMD64)
121         return 1;
122 #endif
123         return 0;
124 }
125
126 LIBTEST_API unsigned short* STDCALL
127 test_lpwstr_marshal (unsigned short* chars, long length)
128 {
129         int i = 0;
130         unsigned short *res;
131
132         res = marshal_alloc (2 * (length + 1));
133
134         // printf("test_lpwstr_marshal()\n");
135         
136         while ( i < length ) {
137                 // printf("X|%u|\n", chars[i]);
138                 res [i] = chars[i];
139                 i++;
140         }
141
142         res [i] = 0;
143
144         return res;
145 }
146
147
148 LIBTEST_API void STDCALL
149 test_lpwstr_marshal_out (unsigned short** chars)
150 {
151         int i = 0;
152         const char abc[] = "ABC";
153         glong len = strlen(abc);
154
155         *chars = marshal_alloc (2 * (len + 1));
156         
157         while ( i < len ) {
158                 (*chars) [i] = abc[i];
159                 i++;
160         }
161
162         (*chars) [i] = 0;
163 }
164
165 typedef struct {
166         int b;
167         int a;
168         int c;
169 } union_test_1_type;
170
171 LIBTEST_API int STDCALL  
172 mono_union_test_1 (union_test_1_type u1) {
173         // printf ("Got values %d %d %d\n", u1.b, u1.a, u1.c);
174         return u1.a + u1.b + u1.c;
175 }
176
177 LIBTEST_API int STDCALL  
178 mono_return_int (int a) {
179         // printf ("Got value %d\n", a);
180         return a;
181 }
182
183 LIBTEST_API float STDCALL  
184 mono_test_marshal_pass_return_float (float f) {
185         return f + 1.0;
186 }
187
188 struct ss
189 {
190         int i;
191 };
192
193 LIBTEST_API int STDCALL 
194 mono_return_int_ss (struct ss a) {
195         // printf ("Got value %d\n", a.i);
196         return a.i;
197 }
198
199 LIBTEST_API struct ss STDCALL
200 mono_return_ss (struct ss a) {
201         // printf ("Got value %d\n", a.i);
202         a.i++;
203         return a;
204 }
205
206 struct sc1
207 {
208         char c[1];
209 };
210
211 LIBTEST_API struct sc1 STDCALL
212 mono_return_sc1 (struct sc1 a) {
213         // printf ("Got value %d\n", a.c[0]);
214         a.c[0]++;
215         return a;
216 }
217
218
219 struct sc3
220 {
221         char c[3];
222 };
223
224 LIBTEST_API struct sc3 STDCALL 
225 mono_return_sc3 (struct sc3 a) {
226         // printf ("Got values %d %d %d\n", a.c[0], a.c[1], a.c[2]);
227         a.c[0]++;
228         a.c[1] += 2;
229         a.c[2] += 3;
230         return a;
231 }
232
233 struct sc5
234 {
235         char c[5];
236 };
237
238 LIBTEST_API struct sc5 STDCALL 
239 mono_return_sc5 (struct sc5 a) {
240         // printf ("Got values %d %d %d %d %d\n", a.c[0], a.c[1], a.c[2], a.c[3], a.c[4]);
241         a.c[0]++;
242         a.c[1] += 2;
243         a.c[2] += 3;
244         a.c[3] += 4;
245         a.c[4] += 5;
246         return a;
247 }
248
249 union su
250 {
251         int i1;
252         int i2;
253 };
254
255 LIBTEST_API int STDCALL  
256 mono_return_int_su (union su a) {
257         // printf ("Got value %d\n", a.i1);
258         return a.i1;
259 }
260
261 struct FI {
262         float f1;
263         float f2;
264         float f3;
265 };
266
267 struct NestedFloat {
268         struct FI fi;
269         float f4;
270 };
271
272 LIBTEST_API struct NestedFloat STDCALL
273 mono_return_nested_float (void)
274 {
275         struct NestedFloat f;
276         f.fi.f1 = 1.0;
277         f.fi.f2 = 2.0;
278         f.fi.f3 = 3.0;
279         f.f4 = 4.0;
280         return f;
281 }
282
283 LIBTEST_API int STDCALL  
284 mono_test_many_int_arguments (int a, int b, int c, int d, int e,
285                                                           int f, int g, int h, int i, int j);
286 LIBTEST_API short STDCALL 
287 mono_test_many_short_arguments (short a, short b, short c, short d, short e,
288                                                                 short f, short g, short h, short i, short j);
289 LIBTEST_API char STDCALL 
290 mono_test_many_char_arguments (char a, char b, char c, char d, char e,
291                                                            char f, char g, char h, char i, char j);
292
293 LIBTEST_API int STDCALL 
294 mono_test_many_int_arguments (int a, int b, int c, int d, int e, int f, int g, int h, int i, int j)
295 {
296         return a + b + c + d + e + f + g + h + i + j;
297 }
298
299 LIBTEST_API short STDCALL 
300 mono_test_many_short_arguments (short a, short b, short c, short d, short e, short f, short g, short h, short i, short j)
301 {
302         return a + b + c + d + e + f + g + h + i + j;
303 }
304
305 LIBTEST_API char STDCALL 
306 mono_test_many_byte_arguments (char a, char b, char c, char d, char e, char f, char g, char h, char i, char j)
307 {
308         return a + b + c + d + e + f + g + h + i + j;
309 }
310
311 LIBTEST_API float STDCALL 
312 mono_test_many_float_arguments (float a, float b, float c, float d, float e, float f, float g, float h, float i, float j)
313 {
314         return a + b + c + d + e + f + g + h + i + j;
315 }
316
317 LIBTEST_API double STDCALL 
318 mono_test_many_double_arguments (double a, double b, double c, double d, double e, double f, double g, double h, double i, double j)
319 {
320         return a + b + c + d + e + f + g + h + i + j;
321 }
322
323 LIBTEST_API double STDCALL 
324 mono_test_split_double_arguments (double a, double b, float c, double d, double e)
325 {
326         return a + b + c + d + e;
327 }
328
329 LIBTEST_API int STDCALL 
330 mono_test_puts_static (char *s)
331 {
332         // printf ("TEST %s\n", s);
333         return 1;
334 }
335
336 typedef int (STDCALL *SimpleDelegate3) (int a, int b);
337
338 LIBTEST_API int STDCALL 
339 mono_invoke_delegate (SimpleDelegate3 delegate)
340 {
341         int res;
342
343         // printf ("start invoke %p\n", delegate);
344
345         res = delegate (2, 3);
346
347         // printf ("end invoke\n");
348
349         return res;
350 }
351
352 LIBTEST_API int STDCALL
353 mono_invoke_simple_delegate (SimpleDelegate d)
354 {
355         return d (4);
356 }
357
358 LIBTEST_API int STDCALL  
359 mono_test_marshal_char (short a1)
360 {
361         if (a1 == 'a')
362                 return 0;
363         
364         return 1;
365 }
366
367 LIBTEST_API void STDCALL
368 mono_test_marshal_char_array (gunichar2 *s)
369 {
370         const char m[] = "abcdef";
371         gunichar2* s2;
372         glong len;
373
374         s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
375         
376         len = (len * 2) + 2;
377         memcpy (s, s2, len);
378
379         g_free (s2);
380 }
381
382 LIBTEST_API int STDCALL
383 mono_test_marshal_ansi_char_array (char *s)
384 {
385         const char m[] = "abcdef";
386
387         if (strncmp ("qwer", s, 4))
388                 return 1;
389
390         memcpy (s, m, sizeof (m));
391         return 0;
392 }
393
394 LIBTEST_API int STDCALL
395 mono_test_marshal_unicode_char_array (gunichar2 *s)
396 {
397         const char m[] = "abcdef";
398         const char expected[] = "qwer";
399         gunichar2 *s1, *s2;
400         glong len1, len2;
401
402         s1 = g_utf8_to_utf16 (m, -1, NULL, &len1, NULL);
403         s2 = g_utf8_to_utf16 (expected, -1, NULL, &len2, NULL);
404         len1 = (len1 * 2);
405         len2 = (len2 * 2);
406
407         if (memcmp (s, s2, len2))
408                 return 1;
409
410         memcpy (s, s1, len1);
411         return 0;
412 }
413
414 LIBTEST_API int STDCALL 
415 mono_test_empty_pinvoke (int i)
416 {
417         return i;
418 }
419
420 LIBTEST_API int STDCALL  
421 mono_test_marshal_bool_byref (int a, int *b, int c)
422 {
423     int res = *b;
424
425         *b = 1;
426
427         return res;
428 }
429
430 LIBTEST_API int STDCALL 
431 mono_test_marshal_bool_in_as_I1_U1 (char bTrue, char bFalse)
432 {
433         if (!bTrue)
434                 return 1;
435         if (bFalse)
436                 return 2;
437         return 0;
438 }
439
440 LIBTEST_API int STDCALL 
441 mono_test_marshal_bool_out_as_I1_U1 (char* bTrue, char* bFalse)
442 {
443         if (!bTrue || !bFalse)
444                 return 3;
445
446         *bTrue = 1;
447         *bFalse = 0;
448
449         return 0;
450 }
451
452 LIBTEST_API int STDCALL 
453 mono_test_marshal_bool_ref_as_I1_U1 (char* bTrue, char* bFalse)
454 {
455         if (!bTrue || !bFalse)
456                 return 4;
457
458         if (!(*bTrue))
459                 return 5;
460         if (*bFalse)
461                 return 6;
462
463         *bFalse = 1;
464         *bTrue = 0;
465
466         return 0;
467 }
468
469 LIBTEST_API int STDCALL  
470 mono_test_marshal_array (int *a1)
471 {
472         int i, sum = 0;
473
474         for (i = 0; i < 50; i++)
475                 sum += a1 [i];
476         
477         return sum;
478 }
479
480 LIBTEST_API int STDCALL  
481 mono_test_marshal_inout_array (int *a1)
482 {
483         int i, sum = 0;
484
485         for (i = 0; i < 50; i++) {
486                 sum += a1 [i];
487                 a1 [i] = 50 - a1 [i];
488         }
489         
490         return sum;
491 }
492
493 LIBTEST_API int /* cdecl */
494 mono_test_marshal_inout_array_cdecl (int *a1)
495 {
496         return mono_test_marshal_inout_array (a1);
497 }
498
499 LIBTEST_API int STDCALL  
500 mono_test_marshal_out_array (int *a1)
501 {
502         int i;
503
504         for (i = 0; i < 50; i++) {
505                 a1 [i] = i;
506         }
507         
508         return 0;
509 }
510
511 LIBTEST_API int STDCALL
512 mono_test_marshal_out_byref_array_out_size_param (int **out_arr, int *out_len)
513 {
514         int *arr;
515         int i, len;
516
517         len = 4;
518         arr = marshal_alloc (sizeof (gint32) * len);
519         for (i = 0; i < len; ++i)
520                 arr [i] = i;
521         *out_arr = arr;
522         *out_len = len;
523
524         return 0;
525 }
526
527 LIBTEST_API int STDCALL  
528 mono_test_marshal_inout_nonblittable_array (gunichar2 *a1)
529 {
530         int i, sum = 0;
531
532         for (i = 0; i < 10; i++) {
533                 a1 [i] = 'F';
534         }
535         
536         return sum;
537 }
538
539 typedef struct {
540         int a;
541         int b;
542         int c;
543         const char *d;
544         gunichar2 *d2;
545 } simplestruct;
546
547 typedef struct {
548         double x;
549         double y;
550 } point;
551
552 LIBTEST_API simplestruct STDCALL 
553 mono_test_return_vtype (int i)
554 {
555         simplestruct res;
556         static gunichar2 test2 [] = { 'T', 'E', 'S', 'T', '2', 0 };
557
558         res.a = 0;
559         res.b = 1;
560         res.c = 0;
561         res.d = "TEST";
562         res.d2 = test2;
563
564         return res;
565 }
566
567 LIBTEST_API void STDCALL
568 mono_test_delegate_struct (void)
569 {
570         // printf ("TEST\n");
571 }
572
573 typedef char* (STDCALL *ReturnStringDelegate) (const char *s);
574
575 LIBTEST_API char * STDCALL 
576 mono_test_return_string (ReturnStringDelegate func)
577 {
578         char *res;
579
580         // printf ("mono_test_return_string\n");
581
582         res = func ("TEST");
583         marshal_free (res);
584
585         // printf ("got string: %s\n", res);
586         return marshal_strdup ("12345");
587 }
588
589 typedef int (STDCALL *RefVTypeDelegate) (int a, simplestruct *ss, int b);
590
591 LIBTEST_API int STDCALL 
592 mono_test_ref_vtype (int a, simplestruct *ss, int b, RefVTypeDelegate func)
593 {
594         if (a == 1 && b == 2 && ss->a == 0 && ss->b == 1 && ss->c == 0 &&
595             !strcmp (ss->d, "TEST1")) {
596                 ss->a = 1;
597                 ss->b = 0;
598                 ss->c = 1;
599                 ss->d = "TEST2";
600
601                 return func (a, ss, b);
602         }
603
604         return 1;
605 }
606
607 typedef int (STDCALL *OutVTypeDelegate) (int a, simplestruct *ss, int b);
608
609 LIBTEST_API int STDCALL 
610 mono_test_marshal_out_struct (int a, simplestruct *ss, int b, OutVTypeDelegate func)
611 {
612         /* Check that the input pointer is ignored */
613         ss->d = (gpointer)0x12345678;
614
615         func (a, ss, b);
616
617         if (ss->a && ss->b && ss->c && !strcmp (ss->d, "TEST3"))
618                 return 0;
619         else
620                 return 1;
621 }
622
623 typedef int (STDCALL *InVTypeDelegate) (int a, simplestruct *ss, int b);
624
625 LIBTEST_API int STDCALL 
626 mono_test_marshal_in_struct (int a, simplestruct *ss, int b, InVTypeDelegate func)
627 {
628         simplestruct ss2;
629         int res;
630
631         memcpy (&ss2, ss, sizeof (simplestruct));
632
633         res = func (a, ss, b);
634         if (res) {
635                 printf ("mono_test_marshal_in_struct () failed: %d\n", res);
636                 return 1;
637         }
638
639         /* Check that no modifications is made to the struct */
640         if (ss2.a == ss->a && ss2.b == ss->b && ss2.c == ss->c && ss2.d == ss->d)
641                 return 0;
642         else
643                 return 1;
644 }
645
646 typedef struct {
647         int a;
648         SimpleDelegate func, func2, func3;
649 } DelegateStruct;
650
651 LIBTEST_API DelegateStruct STDCALL 
652 mono_test_marshal_delegate_struct (DelegateStruct ds)
653 {
654         DelegateStruct res;
655
656         res.a = ds.func (ds.a) + ds.func2 (ds.a) + (ds.func3 == NULL ? 0 : 1);
657         res.func = ds.func;
658         res.func2 = ds.func2;
659         res.func3 = NULL;
660
661         return res;
662 }
663
664 LIBTEST_API int STDCALL  
665 mono_test_marshal_struct (simplestruct ss)
666 {
667         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
668             !strcmp (ss.d, "TEST"))
669                 return 0;
670
671         return 1;
672 }
673
674 LIBTEST_API int STDCALL 
675 mono_test_marshal_byref_struct (simplestruct *ss, int a, int b, int c, char *d)
676 {
677         gboolean res = (ss->a == a && ss->b == b && ss->c == c && strcmp (ss->d, d) == 0);
678
679         marshal_free ((char*)ss->d);
680
681         ss->a = !ss->a;
682         ss->b = !ss->b;
683         ss->c = !ss->c;
684         ss->d = marshal_strdup ("DEF");
685
686         return res ? 0 : 1;
687 }
688
689 typedef struct {
690         int a;
691         int b;
692         int c;
693         char *d;
694         unsigned char e;
695         double f;
696         unsigned char g;
697         guint64 h;
698 } simplestruct2;
699
700 LIBTEST_API int STDCALL 
701 mono_test_marshal_struct2 (simplestruct2 ss)
702 {
703         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
704             !strcmp (ss.d, "TEST") && 
705             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
706                 return 0;
707
708         return 1;
709 }
710
711 /* on HP some of the struct should be on the stack and not in registers */
712 LIBTEST_API int STDCALL 
713 mono_test_marshal_struct2_2 (int i, int j, int k, simplestruct2 ss)
714 {
715         if (i != 10 || j != 11 || k != 12)
716                 return 1;
717         if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
718             !strcmp (ss.d, "TEST") && 
719             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
720                 return 0;
721
722         return 1;
723 }
724
725 LIBTEST_API int STDCALL  
726 mono_test_marshal_lpstruct (simplestruct *ss)
727 {
728         if (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
729             !strcmp (ss->d, "TEST"))
730                 return 0;
731
732         return 1;
733 }
734
735 LIBTEST_API int STDCALL  
736 mono_test_marshal_lpstruct_blittable (point *p)
737 {
738         if (p->x == 1.0 && p->y == 2.0)
739                 return 0;
740         else
741                 return 1;
742 }
743
744 LIBTEST_API int STDCALL 
745 mono_test_marshal_struct_array (simplestruct2 *ss)
746 {
747         if (! (ss[0].a == 0 && ss[0].b == 1 && ss[0].c == 0 &&
748                    !strcmp (ss[0].d, "TEST") && 
749                    ss[0].e == 99 && ss[0].f == 1.5 && ss[0].g == 42 && ss[0].h == (guint64)123))
750                 return 1;
751
752         if (! (ss[1].a == 0 && ss[1].b == 0 && ss[1].c == 0 &&
753                    !strcmp (ss[1].d, "TEST2") && 
754                    ss[1].e == 100 && ss[1].f == 2.5 && ss[1].g == 43 && ss[1].h == (guint64)124))
755                 return 1;
756
757         return 0;
758 }
759
760 typedef struct long_align_struct {
761         gint32 a;
762         gint64 b;
763         gint64 c;
764 } long_align_struct;
765
766 LIBTEST_API int STDCALL 
767 mono_test_marshal_long_align_struct_array (long_align_struct *ss)
768 {
769         return ss[0].a + ss[0].b + ss[0].c + ss[1].a + ss[1].b + ss[1].c;
770 }
771
772 LIBTEST_API simplestruct2 * STDCALL 
773 mono_test_marshal_class (int i, int j, int k, simplestruct2 *ss, int l)
774 {
775         simplestruct2 *res;
776
777         if (!ss)
778                 return NULL;
779
780         if (i != 10 || j != 11 || k != 12 || l != 14)
781                 return NULL;
782         if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
783                    !strcmp (ss->d, "TEST") && 
784                    ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
785                 return NULL;
786
787         res = marshal_new0 (simplestruct2, 1);
788         memcpy (res, ss, sizeof (simplestruct2));
789         res->d = marshal_strdup ("TEST");
790         return res;
791 }
792
793 LIBTEST_API int STDCALL 
794 mono_test_marshal_byref_class (simplestruct2 **ssp)
795 {
796         simplestruct2 *ss = *ssp;
797         simplestruct2 *res;
798         
799         if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
800                    !strcmp (ss->d, "TEST") && 
801                    ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
802                 return 1;
803
804         res = marshal_new0 (simplestruct2, 1);
805         memcpy (res, ss, sizeof (simplestruct2));
806         res->d = marshal_strdup ("TEST-RES");
807
808         *ssp = res;
809         return 0;
810 }
811
812 static void *
813 get_sp (void)
814 {
815         int i;
816         void *p;
817
818         /* Yes, this is correct, we are only trying to determine the value of the stack here */
819         p = &i;
820         return p;
821 }
822
823 LIBTEST_API int STDCALL 
824 reliable_delegate (int a)
825 {
826         return a;
827 }
828
829 /*
830  * Checks whether get_sp() works as expected. It doesn't work with gcc-2.95.3 on linux.
831  */
832 static gboolean
833 is_get_sp_reliable (void)
834 {
835         void *sp1, *sp2;
836
837         reliable_delegate(1);
838         sp1 = get_sp();
839         reliable_delegate(1);
840         sp2 = get_sp();
841         return sp1 == sp2;
842
843
844 LIBTEST_API int STDCALL 
845 mono_test_marshal_delegate (SimpleDelegate delegate)
846 {
847         void *sp1, *sp2;
848
849         /* Check that the delegate wrapper is stdcall */
850         delegate (2);
851         sp1 = get_sp ();
852         delegate (2);
853         sp2 = get_sp ();
854         if (is_get_sp_reliable())
855                 g_assert (sp1 == sp2);
856
857         return delegate (2);
858 }
859
860 static int STDCALL inc_cb (int i)
861 {
862         return i + 1;
863 }
864
865 LIBTEST_API int STDCALL 
866 mono_test_marshal_out_delegate (SimpleDelegate *delegate)
867 {
868         *delegate = inc_cb;
869
870         return 0;
871 }
872
873 LIBTEST_API SimpleDelegate STDCALL 
874 mono_test_marshal_return_delegate (SimpleDelegate delegate)
875 {
876         return delegate;
877 }
878
879 static int STDCALL
880 return_plus_one (int i)
881 {
882         return i + 1;
883 }
884
885 LIBTEST_API SimpleDelegate STDCALL 
886 mono_test_marshal_return_delegate_2 (void)
887 {
888         return return_plus_one;
889 }
890
891 typedef simplestruct (STDCALL *SimpleDelegate2) (simplestruct ss);
892
893 static gboolean
894 is_utf16_equals (gunichar2 *s1, const char *s2)
895 {
896         char *s;
897         int res;
898
899         s = g_utf16_to_utf8 (s1, -1, NULL, NULL, NULL);
900         res = strcmp (s, s2);
901         g_free (s);
902
903         return res == 0;
904 }
905
906 LIBTEST_API int STDCALL 
907 mono_test_marshal_delegate2 (SimpleDelegate2 delegate)
908 {
909         simplestruct ss, res;
910
911         ss.a = 0;
912         ss.b = 1;
913         ss.c = 0;
914         ss.d = "TEST";
915         ss.d2 = g_utf8_to_utf16 ("TEST2", -1, NULL, NULL, NULL); 
916
917         res = delegate (ss);
918         if (! (res.a && !res.b && res.c && !strcmp (res.d, "TEST-RES") && is_utf16_equals (res.d2, "TEST2-RES")))
919                 return 1;
920
921         return 0;
922 }
923
924 typedef simplestruct* (STDCALL *SimpleDelegate4) (simplestruct *ss);
925
926 LIBTEST_API int STDCALL 
927 mono_test_marshal_delegate4 (SimpleDelegate4 delegate)
928 {
929         simplestruct ss;
930         simplestruct *res;
931
932         ss.a = 0;
933         ss.b = 1;
934         ss.c = 0;
935         ss.d = "TEST";
936
937         /* Check argument */
938         res = delegate (&ss);
939         if (!res)
940                 return 1;
941
942         /* Check return value */
943         if (! (!res->a && res->b && !res->c && !strcmp (res->d, "TEST")))
944                 return 2;
945
946         /* Check NULL argument and NULL result */
947         res = delegate (NULL);
948         if (res)
949                 return 3;
950
951         return 0;
952 }
953
954 typedef int (STDCALL *SimpleDelegate5) (simplestruct **ss);
955
956 LIBTEST_API int STDCALL 
957 mono_test_marshal_delegate5 (SimpleDelegate5 delegate)
958 {
959         simplestruct ss;
960         int res;
961         simplestruct *ptr;
962
963         ss.a = 0;
964         ss.b = 1;
965         ss.c = 0;
966         ss.d = "TEST";
967
968         ptr = &ss;
969
970         res = delegate (&ptr);
971         if (res != 0)
972                 return 1;
973
974         if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
975                 return 2;
976
977         return 0;
978 }
979
980 LIBTEST_API int STDCALL 
981 mono_test_marshal_delegate6 (SimpleDelegate5 delegate)
982 {
983         delegate (NULL);
984         return 0;
985 }
986
987 typedef int (STDCALL *SimpleDelegate7) (simplestruct **ss);
988
989 LIBTEST_API int STDCALL 
990 mono_test_marshal_delegate7 (SimpleDelegate7 delegate)
991 {
992         int res;
993         simplestruct *ptr;
994
995         /* Check that the input pointer is ignored */
996         ptr = (gpointer)0x12345678;
997
998         res = delegate (&ptr);
999         if (res != 0)
1000                 return 1;
1001
1002         if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
1003                 return 2;
1004
1005         return 0;
1006 }
1007
1008 typedef int (STDCALL *InOutByvalClassDelegate) (simplestruct *ss);
1009
1010 LIBTEST_API int STDCALL 
1011 mono_test_marshal_inout_byval_class_delegate (InOutByvalClassDelegate delegate)
1012 {
1013         int res;
1014         simplestruct ss;
1015
1016         ss.a = FALSE;
1017         ss.b = TRUE;
1018         ss.c = FALSE;
1019         ss.d = g_strdup_printf ("%s", "FOO");
1020
1021         res = delegate (&ss);
1022         if (res != 0)
1023                 return 1;
1024
1025         if (!(ss.a && !ss.b && ss.c && !strcmp (ss.d, "RES")))
1026                 return 2;
1027
1028         return 0;
1029 }
1030
1031 typedef int (STDCALL *SimpleDelegate8) (gunichar2 *s);
1032
1033 LIBTEST_API int STDCALL 
1034 mono_test_marshal_delegate8 (SimpleDelegate8 delegate, gunichar2 *s)
1035 {
1036         return delegate (s);
1037 }
1038
1039 typedef int (STDCALL *return_int_fnt) (int i);
1040 typedef int (STDCALL *SimpleDelegate9) (return_int_fnt d);
1041
1042 LIBTEST_API int STDCALL 
1043 mono_test_marshal_delegate9 (SimpleDelegate9 delegate, gpointer ftn)
1044 {
1045         return delegate (ftn);
1046 }
1047
1048 static int STDCALL 
1049 return_self (int i)
1050 {
1051         return i;
1052 }
1053
1054 LIBTEST_API int STDCALL 
1055 mono_test_marshal_delegate10 (SimpleDelegate9 delegate)
1056 {
1057         return delegate (return_self);
1058 }
1059
1060 typedef int (STDCALL *PrimitiveByrefDelegate) (int *i);
1061
1062 LIBTEST_API int STDCALL 
1063 mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate delegate)
1064 {
1065         int i = 1;
1066
1067         int res = delegate (&i);
1068         if (res != 0)
1069                 return res;
1070
1071         if (i != 2)
1072                 return 2;
1073
1074         return 0;
1075 }
1076
1077 typedef int (STDCALL *return_int_delegate) (int i);
1078
1079 typedef return_int_delegate (STDCALL *ReturnDelegateDelegate) (void);
1080
1081 LIBTEST_API int STDCALL 
1082 mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d)
1083 {
1084         return (d ()) (55);
1085 }
1086
1087 LIBTEST_API int STDCALL  
1088 mono_test_marshal_stringbuilder (char *s, int n)
1089 {
1090         const char m[] = "This is my message.  Isn't it nice?";
1091
1092         if (strcmp (s, "ABCD") != 0)
1093                 return 1;
1094         strncpy(s, m, n);
1095         s [n] = '\0';
1096         return 0;
1097 }
1098
1099 LIBTEST_API int STDCALL  
1100 mono_test_marshal_stringbuilder_append (char *s, int length)
1101 {
1102         const char out_sentinel[] = "CSHARP_";
1103         const char out_len = strlen (out_sentinel);
1104
1105         for (int i=0; i < length; i++) {
1106                 s [i] = out_sentinel [i % out_len];
1107         }
1108
1109         s [length] = '\0';
1110
1111
1112         return 0;
1113 }
1114
1115 LIBTEST_API int STDCALL  
1116 mono_test_marshal_stringbuilder_default (char *s, int n)
1117 {
1118         const char m[] = "This is my message.  Isn't it nice?";
1119
1120         strncpy(s, m, n);
1121         s [n] = '\0';
1122         return 0;
1123 }
1124
1125 LIBTEST_API int STDCALL  
1126 mono_test_marshal_stringbuilder_unicode (gunichar2 *s, int n)
1127 {
1128         const char m[] = "This is my message.  Isn't it nice?";
1129         gunichar2* s2;
1130         glong len;
1131
1132         s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
1133         
1134         len = (len * 2) + 2;
1135         if (len > (n * 2))
1136                 len = n * 2;
1137         memcpy (s, s2, len);
1138
1139         g_free (s2);
1140
1141         return 0;
1142 }
1143
1144 LIBTEST_API void STDCALL
1145 mono_test_marshal_stringbuilder_out (char **s)
1146 {
1147         const char m[] = "This is my message.  Isn't it nice?";
1148         char *str;
1149
1150         str = marshal_alloc (strlen (m) + 1);
1151         memcpy (str, m, strlen (m) + 1);
1152         
1153         *s = str;
1154 }
1155
1156 LIBTEST_API int STDCALL  
1157 mono_test_marshal_stringbuilder_out_unicode (gunichar2 **s)
1158 {
1159         const char m[] = "This is my message.  Isn't it nice?";
1160         gunichar2 *s2;
1161         glong len;
1162
1163         s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
1164         
1165         len = (len * 2) + 2;
1166         *s = marshal_alloc (len);
1167         memcpy (*s, s2, len);
1168
1169         g_free (s2);
1170
1171         return 0;
1172 }
1173
1174 LIBTEST_API int STDCALL
1175 mono_test_marshal_stringbuilder_ref (char **s)
1176 {
1177         const char m[] = "This is my message.  Isn't it nice?";
1178         char *str;
1179
1180         if (strcmp (*s, "ABC"))
1181                 return 1;
1182
1183         str = marshal_alloc (strlen (m) + 1);
1184         memcpy (str, m, strlen (m) + 1);
1185         
1186         *s = str;
1187         return 0;
1188 }
1189
1190 typedef struct {
1191 #ifndef __GNUC__
1192     char a;
1193 #endif
1194 } EmptyStruct;
1195
1196 LIBTEST_API int STDCALL 
1197 mono_test_marshal_empty_string_array (char **array)
1198 {
1199         return (array == NULL) ? 0 : 1;
1200 }
1201
1202 LIBTEST_API int STDCALL 
1203 mono_test_marshal_string_array (char **array)
1204 {
1205         if (strcmp (array [0], "ABC"))
1206                 return 1;
1207         if (strcmp (array [1], "DEF"))
1208                 return 2;
1209
1210         if (array [2] != NULL)
1211                 return 3;
1212
1213         return 0;
1214 }
1215
1216 LIBTEST_API int STDCALL 
1217 mono_test_marshal_byref_string_array (char ***array)
1218 {
1219         if (*array == NULL)
1220                 return 0;
1221
1222         if (strcmp ((*array) [0], "Alpha"))
1223                 return 2;
1224         if (strcmp ((*array) [1], "Beta"))
1225                 return 2;
1226         if (strcmp ((*array) [2], "Gamma"))
1227                 return 2;
1228
1229         return 1;
1230 }
1231
1232 LIBTEST_API int STDCALL 
1233 mono_test_marshal_stringbuilder_array (char **array)
1234 {
1235         if (strcmp (array [0], "ABC"))
1236                 return 1;
1237         if (strcmp (array [1], "DEF"))
1238                 return 2;
1239
1240         strcpy (array [0], "DEF");
1241         strcpy (array [1], "ABC");
1242
1243         return 0;
1244 }
1245
1246 LIBTEST_API int STDCALL 
1247 mono_test_marshal_unicode_string_array (gunichar2 **array, char **array2)
1248 {
1249         GError *error = NULL;
1250         char *s;
1251         
1252         s = g_utf16_to_utf8 (array [0], -1, NULL, NULL, &error);
1253         if (strcmp (s, "ABC")) {
1254                 g_free (s);
1255                 return 1;
1256         }
1257         else
1258                 g_free (s);
1259
1260         s = g_utf16_to_utf8 (array [1], -1, NULL, NULL, &error);
1261         if (strcmp (s, "DEF")) {
1262                 g_free (s);
1263                 return 2;
1264         }
1265         else
1266                 g_free (s);
1267
1268         if (strcmp (array2 [0], "ABC"))
1269                 return 3;
1270
1271         if (strcmp (array2 [1], "DEF")) 
1272                 return 4;
1273
1274         return 0;
1275 }
1276
1277 /* this does not work on Redhat gcc 2.96 */
1278 LIBTEST_API int STDCALL  
1279 mono_test_empty_struct (int a, EmptyStruct es, int b)
1280 {
1281         // printf ("mono_test_empty_struct %d %d\n", a, b);
1282
1283         // Intel icc on ia64 passes 'es' in 2 registers
1284 #if defined(__ia64) && defined(__INTEL_COMPILER)
1285         return 0;
1286 #else
1287         if (a == 1 && b == 2)
1288                 return 0;
1289         return 1;
1290 #endif
1291 }
1292
1293 LIBTEST_API EmptyStruct STDCALL
1294 mono_test_return_empty_struct (int a)
1295 {
1296         EmptyStruct s;
1297
1298         g_assert (a == 42);
1299
1300         return s;
1301 }
1302
1303 typedef struct {
1304        char a[100];
1305 } ByValStrStruct;
1306
1307 LIBTEST_API ByValStrStruct * STDCALL 
1308 mono_test_byvalstr_gen (void)
1309 {
1310         ByValStrStruct *ret;
1311        
1312         ret = malloc(sizeof(ByValStrStruct));
1313         memset(ret, 'a', sizeof(ByValStrStruct)-1);
1314         ret->a[sizeof(ByValStrStruct)-1] = 0;
1315
1316         return ret;
1317 }
1318
1319 LIBTEST_API int STDCALL 
1320 mono_test_byvalstr_check (ByValStrStruct* data, char* correctString)
1321 {
1322         int ret;
1323
1324         ret = strcmp(data->a, correctString);
1325         // printf ("T1: %s\n", data->a);
1326         // printf ("T2: %s\n", correctString);
1327
1328         /* we need g_free because the allocation was performed by mono_test_byvalstr_gen */
1329         g_free (data);
1330         return (ret != 0);
1331 }
1332
1333 typedef struct {
1334         guint16 a[4];
1335         int  flag;
1336 } ByValStrStruct_Unicode;
1337
1338 LIBTEST_API int STDCALL 
1339 mono_test_byvalstr_check_unicode (ByValStrStruct_Unicode *ref, int test)
1340 {
1341         if (ref->flag != 0x1234abcd){
1342                 printf ("overwritten data");
1343                 return 1;
1344         }
1345             
1346         if (test == 1 || test == 3){
1347                 if (ref->a [0] != '1' ||
1348                     ref->a [1] != '2'   ||
1349                     ref->a [2] != '3')
1350                         return 1;
1351                 return 0;
1352         }
1353         if (test == 2){
1354                 if (ref->a [0] != '1' ||
1355                     ref->a [1] != '2')
1356                         return 1;
1357                 return 0;
1358         }
1359         return 10;
1360 }
1361
1362 LIBTEST_API int STDCALL 
1363 NameManglingAnsi (char *data)
1364 {
1365         return data [0] + data [1] + data [2];
1366 }
1367
1368 LIBTEST_API int STDCALL 
1369 NameManglingAnsiA (char *data)
1370 {
1371         g_assert_not_reached ();
1372 }
1373
1374 LIBTEST_API int STDCALL 
1375 NameManglingAnsiW (char *data)
1376 {
1377         g_assert_not_reached ();
1378 }
1379
1380 LIBTEST_API int STDCALL 
1381 NameManglingAnsi2A (char *data)
1382 {
1383         return data [0] + data [1] + data [2];
1384 }
1385
1386 LIBTEST_API int STDCALL 
1387 NameManglingAnsi2W (char *data)
1388 {
1389         g_assert_not_reached ();
1390 }
1391
1392 LIBTEST_API int STDCALL 
1393 NameManglingUnicode (char *data)
1394 {
1395         g_assert_not_reached ();
1396 }
1397
1398 LIBTEST_API int STDCALL 
1399 NameManglingUnicodeW (gunichar2 *data)
1400 {
1401         return data [0] + data [1] + data [2];
1402 }
1403
1404 LIBTEST_API int STDCALL 
1405 NameManglingUnicode2 (gunichar2 *data)
1406 {
1407         return data [0] + data [1] + data [2];
1408 }
1409
1410 LIBTEST_API int STDCALL 
1411 NameManglingAutoW (char *data)
1412 {
1413 #ifdef WIN32
1414         return (data [0] + data [1] + data [2]) == 131 ? 0 : 1;
1415 #else
1416         g_assert_not_reached ();
1417 #endif
1418 }
1419
1420 LIBTEST_API int STDCALL 
1421 NameManglingAuto (char *data)
1422 {
1423 #ifndef WIN32
1424         return (data [0] + data [1] + data [2]) == 198 ? 0 : 1;
1425 #else
1426         g_assert_not_reached ();
1427 #endif
1428 }
1429
1430 typedef int (STDCALL *intcharFunc)(const char*);
1431
1432 LIBTEST_API void STDCALL 
1433 callFunction (intcharFunc f)
1434 {
1435         f ("ABC");
1436 }
1437
1438 typedef struct {
1439         const char* str;
1440         int i;
1441 } SimpleObj;
1442
1443 LIBTEST_API int STDCALL 
1444 class_marshal_test0 (SimpleObj *obj1)
1445 {
1446         // printf ("class_marshal_test0 %s %d\n", obj1->str, obj1->i);
1447
1448         if (strcmp(obj1->str, "T1"))
1449                 return -1;
1450         if (obj1->i != 4)
1451                 return -2;
1452
1453         return 0;
1454 }
1455
1456 LIBTEST_API int STDCALL 
1457 class_marshal_test4 (SimpleObj *obj1)
1458 {
1459         if (obj1)
1460                 return -1;
1461
1462         return 0;
1463 }
1464
1465 LIBTEST_API void STDCALL
1466 class_marshal_test1 (SimpleObj **obj1)
1467 {
1468         SimpleObj *res = malloc (sizeof (SimpleObj));
1469
1470         res->str = marshal_strdup ("ABC");
1471         res->i = 5;
1472
1473         *obj1 = res;
1474 }
1475
1476 LIBTEST_API int STDCALL 
1477 class_marshal_test2 (SimpleObj **obj1)
1478 {
1479         // printf ("class_marshal_test2 %s %d\n", (*obj1)->str, (*obj1)->i);
1480
1481         if (strcmp((*obj1)->str, "ABC"))
1482                 return -1;
1483         if ((*obj1)->i != 5)
1484                 return -2;
1485
1486         return 0;
1487 }
1488
1489 LIBTEST_API int STDCALL 
1490 string_marshal_test0 (char *str)
1491 {
1492         if (strcmp (str, "TEST0"))
1493                 return -1;
1494
1495         return 0;
1496 }
1497
1498 LIBTEST_API void STDCALL
1499 string_marshal_test1 (const char **str)
1500 {
1501         *str = marshal_strdup ("TEST1");
1502 }
1503
1504 LIBTEST_API int STDCALL 
1505 string_marshal_test2 (char **str)
1506 {
1507         // printf ("string_marshal_test2 %s\n", *str);
1508
1509         if (strcmp (*str, "TEST1"))
1510                 return -1;
1511
1512         *str = marshal_strdup ("TEST2");
1513
1514         return 0;
1515 }
1516
1517 LIBTEST_API int STDCALL 
1518 string_marshal_test3 (char *str)
1519 {
1520         if (str)
1521                 return -1;
1522
1523         return 0;
1524 }
1525
1526 typedef struct {
1527         int a;
1528         int b;
1529 } BlittableClass;
1530
1531 LIBTEST_API BlittableClass* STDCALL 
1532 TestBlittableClass (BlittableClass *vl)
1533 {
1534         BlittableClass *res;
1535
1536         // printf ("TestBlittableClass %d %d\n", vl->a, vl->b);
1537
1538         if (vl) {
1539                 vl->a++;
1540                 vl->b++;
1541
1542                 res = marshal_new0 (BlittableClass, 1);
1543                 memcpy (res, vl, sizeof (BlittableClass));
1544         } else {
1545                 res = marshal_new0 (BlittableClass, 1);
1546                 res->a = 42;
1547                 res->b = 43;
1548         }
1549
1550         return res;
1551 }
1552
1553 typedef struct OSVERSIONINFO_STRUCT
1554
1555         int a; 
1556         int b; 
1557 } OSVERSIONINFO_STRUCT;
1558
1559 LIBTEST_API int STDCALL  
1560 MyGetVersionEx (OSVERSIONINFO_STRUCT *osvi)
1561 {
1562
1563         // printf ("GOT %d %d\n", osvi->a, osvi->b);
1564
1565         osvi->a += 1;
1566         osvi->b += 1;
1567
1568         return osvi->a + osvi->b;
1569 }
1570
1571 LIBTEST_API int STDCALL  
1572 BugGetVersionEx (int a, int b, int c, int d, int e, int f, int g, int h, OSVERSIONINFO_STRUCT *osvi)
1573 {
1574
1575         // printf ("GOT %d %d\n", osvi->a, osvi->b);
1576
1577         osvi->a += 1;
1578         osvi->b += 1;
1579
1580         return osvi->a + osvi->b;
1581 }
1582
1583 LIBTEST_API int STDCALL 
1584 mono_test_marshal_point (point pt)
1585 {
1586         // printf("point %g %g\n", pt.x, pt.y);
1587         if (pt.x == 1.25 && pt.y == 3.5)
1588                 return 0;
1589
1590         return 1;
1591 }
1592
1593 typedef struct {
1594         int x;
1595         double y;
1596 } mixed_point;
1597
1598 LIBTEST_API int STDCALL 
1599 mono_test_marshal_mixed_point (mixed_point pt)
1600 {
1601         // printf("mixed point %d %g\n", pt.x, pt.y);
1602         if (pt.x == 5 && pt.y == 6.75)
1603                 return 0;
1604
1605         return 1;
1606 }
1607
1608 LIBTEST_API int STDCALL 
1609 mono_test_marshal_mixed_point_2 (mixed_point *pt)
1610 {
1611         if (pt->x != 5 || pt->y != 6.75)
1612                 return 1;
1613
1614         pt->x = 10;
1615         pt->y = 12.35;
1616
1617         return 0;
1618 }
1619
1620 LIBTEST_API int STDCALL  
1621 marshal_test_ref_bool(int i, char *b1, short *b2, int *b3)
1622 {
1623     int res = 1;
1624     if (*b1 != 0 && *b1 != 1)
1625         return 1;
1626     if (*b2 != 0 && *b2 != -1) /* variant_bool */
1627         return 1;
1628     if (*b3 != 0 && *b3 != 1)
1629         return 1;
1630     if (i == ((*b1 << 2) | (-*b2 << 1) | *b3))
1631         res = 0;
1632     *b1 = !*b1;
1633     *b2 = ~*b2;
1634     *b3 = !*b3;
1635     return res;
1636 }
1637
1638 struct BoolStruct
1639 {
1640     int i;
1641     char b1;
1642     short b2; /* variant_bool */
1643     int b3;
1644 };
1645
1646 LIBTEST_API int STDCALL  
1647 marshal_test_bool_struct(struct BoolStruct *s)
1648 {
1649     int res = 1;
1650     if (s->b1 != 0 && s->b1 != 1)
1651         return 1;
1652     if (s->b2 != 0 && s->b2 != -1)
1653         return 1;
1654     if (s->b3 != 0 && s->b3 != 1)
1655         return 1;
1656     if (s->i == ((s->b1 << 2) | (-s->b2 << 1) | s->b3))
1657         res = 0;
1658     s->b1 = !s->b1;
1659     s->b2 = ~s->b2;
1660     s->b3 = !s->b3;
1661     return res;
1662 }
1663
1664 typedef struct {
1665         gint64 l;
1666 } LongStruct2;
1667
1668 typedef struct {
1669         int i;
1670         LongStruct2 l;
1671 } LongStruct;
1672
1673 LIBTEST_API int STDCALL
1674 mono_test_marshal_long_struct (LongStruct *s)
1675 {
1676         return s->i + s->l.l;
1677 }
1678
1679 LIBTEST_API void STDCALL
1680 mono_test_last_error (int err)
1681 {
1682 #ifdef WIN32
1683         SetLastError (err);
1684 #else
1685         errno = err;
1686 #endif
1687 }
1688
1689 LIBTEST_API int STDCALL 
1690 mono_test_asany (void *ptr, int what)
1691 {
1692         switch (what) {
1693         case 1:
1694                 return (*(int*)ptr == 5) ? 0 : 1;
1695         case 2:
1696                 return strcmp (ptr, "ABC") == 0 ? 0 : 1;
1697         case 3: {
1698                 simplestruct2 ss = *(simplestruct2*)ptr;
1699
1700                 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
1701             !strcmp (ss.d, "TEST") && 
1702             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
1703                         return 0;
1704                 else
1705                         return 1;
1706         }
1707         case 4: {
1708                 GError *error = NULL;
1709                 char *s;
1710
1711                 s = g_utf16_to_utf8 (ptr, -1, NULL, NULL, &error);
1712
1713                 if (!s)
1714                         return 1;
1715
1716                 if (!strcmp (s, "ABC")) {
1717                         g_free (s);
1718                         return 0;
1719                 }
1720                 else {
1721                         g_free (s);
1722                         return 1;
1723                 }
1724         }
1725         default:
1726                 g_assert_not_reached ();
1727         }
1728
1729         return 1;
1730 }
1731
1732 typedef struct
1733 {
1734         int i;
1735         int j;
1736         int k;
1737         char *s;
1738 } AsAnyStruct;
1739
1740 LIBTEST_API int STDCALL 
1741 mono_test_marshal_asany_in (void* ptr)
1742 {
1743         AsAnyStruct* asAny = ptr;
1744         int res = asAny->i + asAny->j + asAny->k;
1745
1746         return res;
1747 }
1748
1749 LIBTEST_API int STDCALL 
1750 mono_test_marshal_asany_inout (void* ptr)
1751 {
1752         AsAnyStruct* asAny = ptr;
1753         int res = asAny->i + asAny->j + asAny->k;
1754
1755         marshal_free (asAny->s);
1756
1757         asAny->i = 10;
1758         asAny->j = 20;
1759         asAny->k = 30;
1760         asAny->s = 0;
1761
1762         return res;
1763 }
1764
1765 LIBTEST_API int STDCALL 
1766 mono_test_marshal_asany_out (void* ptr)
1767 {
1768         AsAnyStruct* asAny = ptr;
1769         int res = asAny->i + asAny->j + asAny->k;
1770
1771         asAny->i = 10;
1772         asAny->j = 20;
1773         asAny->k = 30;
1774         asAny->s = 0;
1775
1776         return res;
1777 }
1778
1779 /*
1780  * AMD64 marshalling tests.
1781  */
1782
1783 typedef struct amd64_struct1 {
1784         int i;
1785         int j;
1786         int k;
1787         int l;
1788 } amd64_struct1;
1789
1790 LIBTEST_API amd64_struct1 STDCALL 
1791 mono_test_marshal_amd64_pass_return_struct1 (amd64_struct1 s)
1792 {
1793         s.i ++;
1794         s.j ++;
1795         s.k ++;
1796         s.l ++;
1797
1798         return s;
1799 }
1800
1801 LIBTEST_API amd64_struct1 STDCALL 
1802 mono_test_marshal_amd64_pass_return_struct1_many_args (amd64_struct1 s, int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8)
1803 {
1804         s.i ++;
1805         s.j ++;
1806         s.k ++;
1807         s.l += 1 + i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8;
1808
1809         return s;
1810 }
1811
1812 typedef struct amd64_struct2 {
1813         int i;
1814         int j;
1815 } amd64_struct2;
1816
1817 LIBTEST_API amd64_struct2 STDCALL 
1818 mono_test_marshal_amd64_pass_return_struct2 (amd64_struct2 s)
1819 {
1820         s.i ++;
1821         s.j ++;
1822
1823         return s;
1824 }
1825
1826 typedef struct amd64_struct3 {
1827         int i;
1828 } amd64_struct3;
1829
1830 LIBTEST_API amd64_struct3 STDCALL 
1831 mono_test_marshal_amd64_pass_return_struct3 (amd64_struct3 s)
1832 {
1833         s.i ++;
1834
1835         return s;
1836 }
1837
1838 typedef struct amd64_struct4 {
1839         double d1, d2;
1840 } amd64_struct4;
1841
1842 LIBTEST_API amd64_struct4 STDCALL 
1843 mono_test_marshal_amd64_pass_return_struct4 (amd64_struct4 s)
1844 {
1845         s.d1 ++;
1846         s.d2 ++;
1847
1848         return s;
1849 }
1850
1851 /*
1852  * IA64 marshalling tests.
1853  */
1854 typedef struct test_struct5 {
1855         float d1, d2;
1856 } test_struct5;
1857
1858 LIBTEST_API test_struct5 STDCALL 
1859 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, test_struct5 s, int i, double d3, double d4)
1860 {
1861         s.d1 += d1 + d2 + i;
1862         s.d2 += d3 + d4 + i;
1863
1864         return s;
1865 }
1866
1867 typedef struct test_struct6 {
1868         double d1, d2;
1869 } test_struct6;
1870
1871 LIBTEST_API test_struct6 STDCALL 
1872 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, test_struct6 s, int i, double d3, double d4)
1873 {
1874         s.d1 += d1 + d2 + i;
1875         s.d2 += d3 + d4;
1876
1877         return s;
1878 }
1879
1880 static guint32 custom_res [2];
1881
1882 LIBTEST_API void* STDCALL
1883 mono_test_marshal_pass_return_custom (int i, guint32 *ptr, int j)
1884 {
1885         /* ptr will be freed by CleanupNative, so make a copy */
1886         custom_res [0] = 0; /* not allocated by AllocHGlobal */
1887         custom_res [1] = ptr [1];
1888
1889         return &custom_res;
1890 }
1891
1892 LIBTEST_API int STDCALL 
1893 mono_test_marshal_pass_out_custom (int i, guint32 **ptr, int j)
1894 {
1895         custom_res [0] = 0;
1896         custom_res [1] = i + j + 10;
1897
1898         *ptr = custom_res;
1899
1900         return 0;
1901 }
1902
1903 LIBTEST_API int STDCALL 
1904 mono_test_marshal_pass_inout_custom (int i, guint32 *ptr, int j)
1905 {
1906         ptr [0] = 0;
1907         ptr [1] = i + ptr [1] + j;
1908
1909         return 0;
1910 }
1911
1912 LIBTEST_API int STDCALL 
1913 mono_test_marshal_pass_out_byval_custom (int i, guint32 *ptr, int j)
1914 {
1915         return ptr == NULL ? 0 : 1;
1916 }
1917
1918 LIBTEST_API int STDCALL 
1919 mono_test_marshal_pass_byref_custom (int i, guint32 **ptr, int j)
1920 {
1921         (*ptr)[1] += i + j;
1922
1923         return 0;
1924 }
1925
1926 LIBTEST_API void* STDCALL
1927 mono_test_marshal_pass_return_custom2 (int i, guint32 *ptr, int j)
1928 {
1929         g_assert_not_reached ();
1930
1931         return NULL;
1932 }
1933
1934 LIBTEST_API void* STDCALL
1935 mono_test_marshal_pass_return_custom_null (int i, guint32 *ptr, int j)
1936 {
1937         g_assert (ptr == NULL);
1938
1939         return NULL;
1940 }
1941
1942 typedef void *(STDCALL *PassReturnPtrDelegate) (void *ptr);
1943
1944 LIBTEST_API int STDCALL 
1945 mono_test_marshal_pass_return_custom_in_delegate (PassReturnPtrDelegate del)
1946 {
1947         guint32 buf [2];
1948         guint32 res;
1949         guint32 *ptr;
1950
1951         buf [0] = 0;
1952         buf [1] = 10;
1953
1954         ptr = del (&buf);
1955
1956         res = ptr [1];
1957
1958 #ifdef WIN32
1959         /* FIXME: Freed with FreeHGlobal */
1960 #else
1961         g_free (ptr);
1962 #endif
1963
1964         return res;
1965 }
1966
1967 LIBTEST_API int STDCALL 
1968 mono_test_marshal_pass_return_custom_null_in_delegate (PassReturnPtrDelegate del)
1969 {
1970         void *ptr = del (NULL);
1971
1972         return (ptr == NULL) ? 15 : 0;
1973 }
1974
1975 typedef void (STDCALL *CustomOutParamDelegate) (void **pptr);
1976
1977 LIBTEST_API int STDCALL 
1978 mono_test_marshal_custom_out_param_delegate (CustomOutParamDelegate del)
1979 {
1980         void* pptr = del;
1981
1982         del (&pptr);
1983
1984         if(pptr != NULL)
1985                 return 1;
1986
1987         return 0;
1988 }
1989
1990 typedef int (STDCALL *ReturnEnumDelegate) (int e);
1991
1992 LIBTEST_API int STDCALL 
1993 mono_test_marshal_return_enum_delegate (ReturnEnumDelegate func)
1994 {
1995         return func (1);
1996 }
1997
1998 typedef struct {
1999         int a, b, c;
2000         gint64 d;
2001 } BlittableStruct;
2002         
2003 typedef BlittableStruct (STDCALL *SimpleDelegate10) (BlittableStruct ss);
2004
2005 LIBTEST_API int STDCALL 
2006 mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 delegate)
2007 {
2008         BlittableStruct ss, res;
2009
2010         ss.a = 1;
2011         ss.b = 2;
2012         ss.c = 3;
2013         ss.d = 55;
2014
2015         res = delegate (ss);
2016         if (! ((res.a == -1) && (res.b == -2) && (res.c == -3) && (res.d == -55)))
2017                 return 1;
2018
2019         return 0;
2020 }
2021
2022 LIBTEST_API int STDCALL 
2023 mono_test_stdcall_name_mangling (int a, int b, int c)
2024 {
2025         return a + b + c;
2026 }
2027
2028 LIBTEST_API int
2029 mono_test_stdcall_mismatch_1 (int a, int b, int c)
2030 {
2031         return a + b + c;
2032 }
2033
2034 LIBTEST_API int STDCALL
2035 mono_test_stdcall_mismatch_2 (int a, int b, int c)
2036 {
2037         return a + b + c;
2038 }
2039
2040 /*
2041  * PASSING AND RETURNING SMALL STRUCTURES FROM DELEGATES TESTS
2042  */
2043
2044 typedef struct {
2045         int i;
2046 } SmallStruct1;
2047         
2048 typedef SmallStruct1 (STDCALL *SmallStructDelegate1) (SmallStruct1 ss);
2049
2050 LIBTEST_API int STDCALL 
2051 mono_test_marshal_small_struct_delegate1 (SmallStructDelegate1 delegate)
2052 {
2053         SmallStruct1 ss, res;
2054
2055         ss.i = 1;
2056
2057         res = delegate (ss);
2058         if (! (res.i == -1))
2059                 return 1;
2060
2061         return 0;
2062 }
2063
2064 typedef struct {
2065         gint16 i, j;
2066 } SmallStruct2;
2067         
2068 typedef SmallStruct2 (STDCALL *SmallStructDelegate2) (SmallStruct2 ss);
2069
2070 LIBTEST_API int STDCALL 
2071 mono_test_marshal_small_struct_delegate2 (SmallStructDelegate2 delegate)
2072 {
2073         SmallStruct2 ss, res;
2074
2075         ss.i = 2;
2076         ss.j = 3;
2077
2078         res = delegate (ss);
2079         if (! ((res.i == -2) && (res.j == -3)))
2080                 return 1;
2081
2082         return 0;
2083 }
2084
2085 typedef struct {
2086         gint16 i;
2087         gint8 j;
2088 } SmallStruct3;
2089         
2090 typedef SmallStruct3 (STDCALL *SmallStructDelegate3) (SmallStruct3 ss);
2091
2092 LIBTEST_API int STDCALL 
2093 mono_test_marshal_small_struct_delegate3 (SmallStructDelegate3 delegate)
2094 {
2095         SmallStruct3 ss, res;
2096
2097         ss.i = 1;
2098         ss.j = 2;
2099
2100         res = delegate (ss);
2101         if (! ((res.i == -1) && (res.j == -2)))
2102                 return 1;
2103
2104         return 0;
2105 }
2106
2107 typedef struct {
2108         gint16 i;
2109 } SmallStruct4;
2110         
2111 typedef SmallStruct4 (STDCALL *SmallStructDelegate4) (SmallStruct4 ss);
2112
2113 LIBTEST_API int STDCALL 
2114 mono_test_marshal_small_struct_delegate4 (SmallStructDelegate4 delegate)
2115 {
2116         SmallStruct4 ss, res;
2117
2118         ss.i = 1;
2119
2120         res = delegate (ss);
2121         if (! (res.i == -1))
2122                 return 1;
2123
2124         return 0;
2125 }
2126
2127 typedef struct {
2128         gint64 i;
2129 } SmallStruct5;
2130         
2131 typedef SmallStruct5 (STDCALL *SmallStructDelegate5) (SmallStruct5 ss);
2132
2133 LIBTEST_API int STDCALL 
2134 mono_test_marshal_small_struct_delegate5 (SmallStructDelegate5 delegate)
2135 {
2136         SmallStruct5 ss, res;
2137
2138         ss.i = 5;
2139
2140         res = delegate (ss);
2141         if (! (res.i == -5))
2142                 return 1;
2143
2144         return 0;
2145 }
2146
2147 typedef struct {
2148         int i, j;
2149 } SmallStruct6;
2150         
2151 typedef SmallStruct6 (STDCALL *SmallStructDelegate6) (SmallStruct6 ss);
2152
2153 LIBTEST_API int STDCALL 
2154 mono_test_marshal_small_struct_delegate6 (SmallStructDelegate6 delegate)
2155 {
2156         SmallStruct6 ss, res;
2157
2158         ss.i = 1;
2159         ss.j = 2;
2160
2161         res = delegate (ss);
2162         if (! ((res.i == -1) && (res.j == -2)))
2163                 return 1;
2164
2165         return 0;
2166 }
2167
2168 typedef struct {
2169         int i;
2170         gint16 j;
2171 } SmallStruct7;
2172         
2173 typedef SmallStruct7 (STDCALL *SmallStructDelegate7) (SmallStruct7 ss);
2174
2175 LIBTEST_API int STDCALL 
2176 mono_test_marshal_small_struct_delegate7 (SmallStructDelegate7 delegate)
2177 {
2178         SmallStruct7 ss, res;
2179
2180         ss.i = 1;
2181         ss.j = 2;
2182
2183         res = delegate (ss);
2184         if (! ((res.i == -1) && (res.j == -2)))
2185                 return 1;
2186
2187         return 0;
2188 }
2189
2190 typedef struct {
2191         float i;
2192 } SmallStruct8;
2193         
2194 typedef SmallStruct8 (STDCALL *SmallStructDelegate8) (SmallStruct8 ss);
2195
2196 LIBTEST_API int STDCALL 
2197 mono_test_marshal_small_struct_delegate8 (SmallStructDelegate8 delegate)
2198 {
2199         SmallStruct8 ss, res;
2200
2201         ss.i = 1.0;
2202
2203         res = delegate (ss);
2204         if (! ((res.i == -1.0)))
2205                 return 1;
2206
2207         return 0;
2208 }
2209
2210 typedef struct {
2211         double i;
2212 } SmallStruct9;
2213         
2214 typedef SmallStruct9 (STDCALL *SmallStructDelegate9) (SmallStruct9 ss);
2215
2216 LIBTEST_API int STDCALL 
2217 mono_test_marshal_small_struct_delegate9 (SmallStructDelegate9 delegate)
2218 {
2219         SmallStruct9 ss, res;
2220
2221         ss.i = 1.0;
2222
2223         res = delegate (ss);
2224         if (! ((res.i == -1.0)))
2225                 return 1;
2226
2227         return 0;
2228 }
2229
2230 typedef struct {
2231         float i, j;
2232 } SmallStruct10;
2233         
2234 typedef SmallStruct10 (STDCALL *SmallStructDelegate10) (SmallStruct10 ss);
2235
2236 LIBTEST_API int STDCALL 
2237 mono_test_marshal_small_struct_delegate10 (SmallStructDelegate10 delegate)
2238 {
2239         SmallStruct10 ss, res;
2240
2241         ss.i = 1.0;
2242         ss.j = 2.0;
2243
2244         res = delegate (ss);
2245         if (! ((res.i == -1.0) && (res.j == -2.0)))
2246                 return 1;
2247
2248         return 0;
2249 }
2250
2251 typedef struct {
2252         float i;
2253         int j;
2254 } SmallStruct11;
2255         
2256 typedef SmallStruct11 (STDCALL *SmallStructDelegate11) (SmallStruct11 ss);
2257
2258 LIBTEST_API int STDCALL 
2259 mono_test_marshal_small_struct_delegate11 (SmallStructDelegate11 delegate)
2260 {
2261         SmallStruct11 ss, res;
2262
2263         ss.i = 1.0;
2264         ss.j = 2;
2265
2266         res = delegate (ss);
2267         if (! ((res.i == -1.0) && (res.j == -2)))
2268                 return 1;
2269
2270         return 0;
2271 }
2272
2273 typedef int (STDCALL *ArrayDelegate) (int i, char *j, void *arr);
2274
2275 LIBTEST_API int STDCALL 
2276 mono_test_marshal_array_delegate (void *arr, int len, ArrayDelegate del)
2277 {
2278         return del (len, NULL, arr);
2279 }
2280
2281 typedef int (STDCALL *ArrayDelegateLong) (gint64 i, char *j, void *arr);
2282
2283 LIBTEST_API int STDCALL 
2284 mono_test_marshal_array_delegate_long (void *arr, gint64 len, ArrayDelegateLong del)
2285 {
2286         return del (len, NULL, arr);
2287 }
2288
2289 LIBTEST_API int STDCALL 
2290 mono_test_marshal_out_array_delegate (int *arr, int len, ArrayDelegate del)
2291 {
2292         del (len, NULL, arr);
2293
2294         if ((arr [0] != 1) || (arr [1] != 2))
2295                 return 1;
2296         else
2297                 return 0;
2298 }
2299
2300 typedef gunichar2* (STDCALL *UnicodeStringDelegate) (gunichar2 *message);
2301
2302 LIBTEST_API int STDCALL 
2303 mono_test_marshal_return_unicode_string_delegate (UnicodeStringDelegate del)
2304 {
2305         const char m[] = "abcdef";
2306         gunichar2 *s2, *res;
2307         glong len;
2308
2309         s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
2310
2311         res = del (s2);
2312
2313         marshal_free (res);
2314
2315         return 0;
2316 }
2317
2318 LIBTEST_API int STDCALL 
2319 mono_test_marshal_out_string_array_delegate (char **arr, int len, ArrayDelegate del)
2320 {
2321         del (len, NULL, arr);
2322
2323         if (!strcmp (arr [0], "ABC") && !strcmp (arr [1], "DEF"))
2324                 return 0;
2325         else
2326                 return 1;
2327 }
2328
2329 typedef int (*CdeclDelegate) (int i, int j);
2330
2331 LIBTEST_API int STDCALL 
2332 mono_test_marshal_cdecl_delegate (CdeclDelegate del)
2333 {
2334         int i;
2335
2336         for (i = 0; i < 1000; ++i)
2337                 del (1, 2);
2338
2339         return 0;
2340 }
2341
2342 typedef char** (STDCALL *ReturnStringArrayDelegate) (int i);
2343
2344 LIBTEST_API int STDCALL 
2345 mono_test_marshal_return_string_array_delegate (ReturnStringArrayDelegate d)
2346 {
2347         char **arr = d (2);
2348         int res;
2349
2350         if (arr == NULL)
2351                 return 3;
2352
2353         if (strcmp (arr [0], "ABC") || strcmp (arr [1], "DEF"))
2354                 res = 1;
2355         else
2356                 res = 0;
2357
2358         marshal_free (arr);
2359
2360         return res;
2361 }
2362
2363 typedef int (STDCALL *ByrefStringDelegate) (char **s);
2364
2365 LIBTEST_API int STDCALL 
2366 mono_test_marshal_byref_string_delegate (ByrefStringDelegate d)
2367 {
2368         char *s = (char*)"ABC";
2369         int res;
2370
2371         res = d (&s);
2372         if (res != 0)
2373                 return res;
2374
2375         if (!strcmp (s, "DEF"))
2376                 res = 0;
2377         else
2378                 res = 2;
2379
2380         marshal_free (s);
2381
2382         return res;
2383 }
2384
2385 LIBTEST_API int STDCALL 
2386 add_delegate (int i, int j)
2387 {
2388         return i + j;
2389 }
2390
2391 LIBTEST_API gpointer STDCALL 
2392 mono_test_marshal_return_fnptr (void)
2393 {
2394         return &add_delegate;
2395 }
2396
2397 LIBTEST_API int STDCALL 
2398 mono_xr (int code)
2399 {
2400         printf ("codigo %x\n", code);
2401         return code + 1234;
2402 }
2403
2404 typedef struct {
2405         int handle;
2406 } HandleRef;
2407
2408 LIBTEST_API HandleRef STDCALL 
2409 mono_xr_as_handle (int code)
2410 {
2411         HandleRef ref;
2412
2413         memset (&ref, 0, sizeof (ref));
2414
2415         return ref;
2416 }
2417  
2418 typedef struct {
2419         int   a;
2420         void *handle1;
2421         void *handle2;
2422         int   b;
2423 } HandleStructs;
2424
2425 LIBTEST_API int STDCALL 
2426 mono_safe_handle_struct_ref (HandleStructs *x)
2427 {
2428         printf ("Dingus Ref! \n");
2429         printf ("Values: %d %d %p %p\n", x->a, x->b, x->handle1, x->handle2);
2430         if (x->a != 1234)
2431                 return 1;
2432         if (x->b != 8743)
2433                 return 2;
2434
2435         if (x->handle1 != (void*) 0x7080feed)
2436                 return 3;
2437
2438         if (x->handle2 != (void*) 0x1234abcd)
2439                 return 4;
2440
2441         return 0xf00d;
2442 }
2443
2444 LIBTEST_API int STDCALL 
2445 mono_safe_handle_struct (HandleStructs x)
2446 {
2447         printf ("Dingus Standard! \n");
2448         printf ("Values: %d %d %p %p\n", x.a, x.b, x.handle1, x.handle2);
2449         if (x.a != 1234)
2450                 return 1;
2451         if (x.b != 8743)
2452                 return 2;
2453
2454         if (x.handle1 != (void*) 0x7080feed)
2455                 return 3;
2456
2457         if (x.handle2 != (void*) 0x1234abcd)
2458                 return 4;
2459         
2460         return 0xf00f;
2461 }
2462
2463 typedef struct {
2464         void *a;
2465 } TrivialHandle;
2466
2467 LIBTEST_API int STDCALL 
2468 mono_safe_handle_struct_simple (TrivialHandle x)
2469 {
2470         printf ("The value is %p\n", x.a);
2471         return ((int)(gsize)x.a) * 2;
2472 }
2473
2474 LIBTEST_API int STDCALL 
2475 mono_safe_handle_return (void)
2476 {
2477         return 0x1000f00d;
2478 }
2479
2480 LIBTEST_API void STDCALL
2481 mono_safe_handle_ref (void **handle)
2482 {
2483         if (*handle != 0){
2484                 *handle = (void *) 0xbad;
2485                 return;
2486         }
2487
2488         *handle = (void *) 0x800d;
2489 }
2490
2491 LIBTEST_API double STDCALL
2492 mono_test_marshal_date_time (double d, double *d2)
2493 {
2494         *d2 = d;
2495         return d;
2496 }
2497
2498 /*
2499  * COM INTEROP TESTS
2500  */
2501
2502 #ifndef WIN32
2503
2504 typedef struct {
2505         guint16 vt;
2506         guint16 wReserved1;
2507         guint16 wReserved2;
2508         guint16 wReserved3;
2509         union {
2510                 gint64 llVal;
2511                 gint32 lVal;
2512                 guint8  bVal;
2513                 gint16 iVal;
2514                 float  fltVal;
2515                 double dblVal;
2516                 gint16 boolVal;
2517                 gunichar2* bstrVal;
2518                 gint8 cVal;
2519                 guint16 uiVal;
2520                 guint32 ulVal;
2521                 guint64 ullVal;
2522                 gpointer byref;
2523                 struct {
2524                         gpointer pvRecord;
2525                         gpointer pRecInfo;
2526                 };
2527         };
2528 } VARIANT;
2529
2530 typedef enum {
2531         VARIANT_TRUE = -1,
2532         VARIANT_FALSE = 0
2533 } VariantBool;
2534
2535 typedef enum {
2536         VT_EMPTY = 0,
2537         VT_NULL = 1,
2538         VT_I2 = 2,
2539         VT_I4 = 3,
2540         VT_R4 = 4,
2541         VT_R8 = 5,
2542         VT_CY = 6,
2543         VT_DATE = 7,
2544         VT_BSTR = 8,
2545         VT_DISPATCH = 9,
2546         VT_ERROR = 10,
2547         VT_BOOL = 11,
2548         VT_VARIANT = 12,
2549         VT_UNKNOWN = 13,
2550         VT_DECIMAL = 14,
2551         VT_I1 = 16,
2552         VT_UI1 = 17,
2553         VT_UI2 = 18,
2554         VT_UI4 = 19,
2555         VT_I8 = 20,
2556         VT_UI8 = 21,
2557         VT_INT = 22,
2558         VT_UINT = 23,
2559         VT_VOID = 24,
2560         VT_HRESULT = 25,
2561         VT_PTR = 26,
2562         VT_SAFEARRAY = 27,
2563         VT_CARRAY = 28,
2564         VT_USERDEFINED = 29,
2565         VT_LPSTR = 30,
2566         VT_LPWSTR = 31,
2567         VT_RECORD = 36,
2568         VT_FILETIME = 64,
2569         VT_BLOB = 65,
2570         VT_STREAM = 66,
2571         VT_STORAGE = 67,
2572         VT_STREAMED_OBJECT = 68,
2573         VT_STORED_OBJECT = 69,
2574         VT_BLOB_OBJECT = 70,
2575         VT_CF = 71,
2576         VT_CLSID = 72,
2577         VT_VECTOR = 4096,
2578         VT_ARRAY = 8192,
2579         VT_BYREF = 16384
2580 } VarEnum;
2581
2582 void VariantInit(VARIANT* vt)
2583 {
2584         vt->vt = VT_EMPTY;
2585 }
2586
2587 typedef struct
2588 {
2589         guint32 a;
2590         guint16 b;
2591         guint16 c;
2592         guint8 d[8];
2593 } GUID;
2594
2595 #define S_OK 0
2596
2597 #endif
2598
2599 LIBTEST_API int STDCALL 
2600 mono_test_marshal_bstr_in(gunichar2* bstr)
2601 {
2602         gint32 result = 0;
2603         gchar* bstr_utf8 = g_utf16_to_utf8 (bstr, -1, NULL, NULL, NULL);
2604         result = strcmp("mono_test_marshal_bstr_in", bstr_utf8);
2605         g_free(bstr_utf8);
2606         if (result == 0)
2607                 return 0;
2608         return 1;
2609 }
2610
2611 LIBTEST_API int STDCALL 
2612 mono_test_marshal_bstr_out(gunichar2** bstr)
2613 {
2614         *bstr = marshal_bstr_alloc ("mono_test_marshal_bstr_out");
2615         return 0;
2616 }
2617
2618 LIBTEST_API int STDCALL 
2619 mono_test_marshal_bstr_in_null(gunichar2* bstr)
2620 {
2621         if (!bstr)
2622                 return 0;
2623         return 1;
2624 }
2625
2626 LIBTEST_API int STDCALL 
2627 mono_test_marshal_bstr_out_null(gunichar2** bstr)
2628 {
2629         *bstr = NULL;
2630         return 0;
2631 }
2632
2633 LIBTEST_API int STDCALL 
2634 mono_test_marshal_variant_in_sbyte(VARIANT variant)
2635 {
2636         if (variant.vt == VT_I1 && variant.cVal == 100)
2637                 return 0;
2638         return 1;
2639 }
2640
2641 LIBTEST_API int STDCALL 
2642 mono_test_marshal_variant_in_byte(VARIANT variant)
2643 {
2644         if (variant.vt == VT_UI1 && variant.bVal == 100)
2645                 return 0;
2646         return 1;
2647 }
2648
2649 LIBTEST_API int STDCALL 
2650 mono_test_marshal_variant_in_short(VARIANT variant)
2651 {
2652         if (variant.vt == VT_I2 && variant.iVal == 314)
2653                 return 0;
2654         return 1;
2655 }
2656
2657 LIBTEST_API int STDCALL 
2658 mono_test_marshal_variant_in_ushort(VARIANT variant)
2659 {
2660         if (variant.vt == VT_UI2 && variant.uiVal == 314)
2661                 return 0;
2662         return 1;
2663 }
2664
2665 LIBTEST_API int STDCALL 
2666 mono_test_marshal_variant_in_int(VARIANT variant)
2667 {
2668         if (variant.vt == VT_I4 && variant.lVal == 314)
2669                 return 0;
2670         return 1;
2671 }
2672
2673 LIBTEST_API int STDCALL 
2674 mono_test_marshal_variant_in_uint(VARIANT variant)
2675 {
2676         if (variant.vt == VT_UI4 && variant.ulVal == 314)
2677                 return 0;
2678         return 1;
2679 }
2680
2681 LIBTEST_API int STDCALL 
2682 mono_test_marshal_variant_in_long(VARIANT variant)
2683 {
2684         if (variant.vt == VT_I8 && variant.llVal == 314)
2685                 return 0;
2686         return 1;
2687 }
2688
2689 LIBTEST_API int STDCALL 
2690 mono_test_marshal_variant_in_ulong(VARIANT variant)
2691 {
2692         if (variant.vt == VT_UI8 && variant.ullVal == 314)
2693                 return 0;
2694         return 1;
2695 }
2696
2697 LIBTEST_API int STDCALL 
2698 mono_test_marshal_variant_in_float(VARIANT variant)
2699 {
2700         if (variant.vt == VT_R4 && (variant.fltVal - 3.14)/3.14 < .001)
2701                 return 0;
2702         return 1;
2703 }
2704
2705 LIBTEST_API int STDCALL 
2706 mono_test_marshal_variant_in_double(VARIANT variant)
2707 {
2708         if (variant.vt == VT_R8 && (variant.dblVal - 3.14)/3.14 < .001)
2709                 return 0;
2710         return 1;
2711 }
2712
2713 LIBTEST_API int STDCALL 
2714 mono_test_marshal_variant_in_bstr(VARIANT variant)
2715 {
2716         gint32 result = 0;
2717         gchar* bstr_utf8 = g_utf16_to_utf8 (variant.bstrVal, -1, NULL, NULL, NULL);
2718         result = strcmp("PI", bstr_utf8);
2719         g_free(bstr_utf8);
2720
2721         if (variant.vt == VT_BSTR && !result)
2722                 return 0;
2723         return 1;
2724 }
2725
2726 LIBTEST_API int STDCALL 
2727 mono_test_marshal_variant_in_bool_true (VARIANT variant)
2728 {
2729         if (variant.vt == VT_BOOL && variant.boolVal == VARIANT_TRUE)
2730                 return 0;
2731         return 1;
2732 }
2733
2734 LIBTEST_API int STDCALL 
2735 mono_test_marshal_variant_in_bool_false (VARIANT variant)
2736 {
2737         if (variant.vt == VT_BOOL && variant.boolVal == VARIANT_FALSE)
2738                 return 0;
2739         return 1;
2740 }
2741
2742 LIBTEST_API int STDCALL 
2743 mono_test_marshal_variant_out_sbyte(VARIANT* variant)
2744 {
2745         variant->vt = VT_I1;
2746         variant->cVal = 100;
2747
2748         return 0;
2749 }
2750
2751 LIBTEST_API int STDCALL 
2752 mono_test_marshal_variant_out_sbyte_byref(VARIANT* variant)
2753 {
2754         variant->vt = VT_I1|VT_BYREF;
2755         variant->byref = marshal_alloc(1);
2756         *((gint8*)variant->byref) = 100;
2757
2758         return 0;
2759 }
2760
2761 LIBTEST_API int STDCALL 
2762 mono_test_marshal_variant_out_byte(VARIANT* variant)
2763 {       
2764         variant->vt = VT_UI1;
2765         variant->bVal = 100;
2766
2767         return 0;
2768 }
2769
2770 LIBTEST_API int STDCALL 
2771 mono_test_marshal_variant_out_byte_byref(VARIANT* variant)
2772 {       
2773         variant->vt = VT_UI1|VT_BYREF;
2774         variant->byref = marshal_alloc(1);
2775         *((gint8*)variant->byref) = 100;
2776
2777         return 0;
2778 }
2779
2780 LIBTEST_API int STDCALL 
2781 mono_test_marshal_variant_out_short(VARIANT* variant)
2782 {
2783         variant->vt = VT_I2;
2784         variant->iVal = 314;
2785
2786         return 0;
2787 }
2788
2789 LIBTEST_API int STDCALL 
2790 mono_test_marshal_variant_out_short_byref(VARIANT* variant)
2791 {
2792         variant->vt = VT_I2|VT_BYREF;
2793         variant->byref = marshal_alloc(2);
2794         *((gint16*)variant->byref) = 314;
2795
2796         return 0;
2797 }
2798
2799 LIBTEST_API int STDCALL 
2800 mono_test_marshal_variant_out_ushort(VARIANT* variant)
2801 {
2802         variant->vt = VT_UI2;
2803         variant->uiVal = 314;
2804
2805         return 0;
2806 }
2807
2808 LIBTEST_API int STDCALL 
2809 mono_test_marshal_variant_out_ushort_byref(VARIANT* variant)
2810 {
2811         variant->vt = VT_UI2|VT_BYREF;
2812         variant->byref = marshal_alloc(2);
2813         *((guint16*)variant->byref) = 314;
2814
2815         return 0;
2816 }
2817
2818 LIBTEST_API int STDCALL 
2819 mono_test_marshal_variant_out_int(VARIANT* variant)
2820 {
2821         variant->vt = VT_I4;
2822         variant->lVal = 314;
2823
2824         return 0;
2825 }
2826
2827 LIBTEST_API int STDCALL 
2828 mono_test_marshal_variant_out_int_byref(VARIANT* variant)
2829 {
2830         variant->vt = VT_I4|VT_BYREF;
2831         variant->byref = marshal_alloc(4);
2832         *((gint32*)variant->byref) = 314;
2833
2834         return 0;
2835 }
2836
2837 LIBTEST_API int STDCALL 
2838 mono_test_marshal_variant_out_uint(VARIANT* variant)
2839 {
2840         variant->vt = VT_UI4;
2841         variant->ulVal = 314;
2842
2843         return 0;
2844 }
2845
2846 LIBTEST_API int STDCALL 
2847 mono_test_marshal_variant_out_uint_byref(VARIANT* variant)
2848 {
2849         variant->vt = VT_UI4|VT_BYREF;
2850         variant->byref = marshal_alloc(4);
2851         *((guint32*)variant->byref) = 314;
2852
2853         return 0;
2854 }
2855
2856 LIBTEST_API int STDCALL 
2857 mono_test_marshal_variant_out_long(VARIANT* variant)
2858 {
2859         variant->vt = VT_I8;
2860         variant->llVal = 314;
2861
2862         return 0;
2863 }
2864
2865 LIBTEST_API int STDCALL 
2866 mono_test_marshal_variant_out_long_byref(VARIANT* variant)
2867 {
2868         variant->vt = VT_I8|VT_BYREF;
2869         variant->byref = marshal_alloc(8);
2870         *((gint64*)variant->byref) = 314;
2871
2872         return 0;
2873 }
2874
2875 LIBTEST_API int STDCALL 
2876 mono_test_marshal_variant_out_ulong(VARIANT* variant)
2877 {
2878         variant->vt = VT_UI8;
2879         variant->ullVal = 314;
2880
2881         return 0;
2882 }
2883
2884 LIBTEST_API int STDCALL 
2885 mono_test_marshal_variant_out_ulong_byref(VARIANT* variant)
2886 {
2887         variant->vt = VT_UI8|VT_BYREF;
2888         variant->byref = marshal_alloc(8);
2889         *((guint64*)variant->byref) = 314;
2890
2891         return 0;
2892 }
2893
2894 LIBTEST_API int STDCALL 
2895 mono_test_marshal_variant_out_float(VARIANT* variant)
2896 {
2897         variant->vt = VT_R4;
2898         variant->fltVal = 3.14;
2899
2900         return 0;
2901 }
2902
2903 LIBTEST_API int STDCALL 
2904 mono_test_marshal_variant_out_float_byref(VARIANT* variant)
2905 {
2906         variant->vt = VT_R4|VT_BYREF;
2907         variant->byref = marshal_alloc(4);
2908         *((float*)variant->byref) = 3.14;
2909
2910         return 0;
2911 }
2912
2913 LIBTEST_API int STDCALL 
2914 mono_test_marshal_variant_out_double(VARIANT* variant)
2915 {
2916         variant->vt = VT_R8;
2917         variant->dblVal = 3.14;
2918
2919         return 0;
2920 }
2921
2922 LIBTEST_API int STDCALL 
2923 mono_test_marshal_variant_out_double_byref(VARIANT* variant)
2924 {
2925         variant->vt = VT_R8|VT_BYREF;
2926         variant->byref = marshal_alloc(8);
2927         *((double*)variant->byref) = 3.14;
2928
2929         return 0;
2930 }
2931
2932 LIBTEST_API int STDCALL 
2933 mono_test_marshal_variant_out_bstr(VARIANT* variant)
2934 {
2935         variant->vt = VT_BSTR;
2936         variant->bstrVal = marshal_bstr_alloc("PI");
2937
2938         return 0;
2939 }
2940
2941 LIBTEST_API int STDCALL 
2942 mono_test_marshal_variant_out_bstr_byref(VARIANT* variant)
2943 {
2944         variant->vt = VT_BSTR|VT_BYREF;
2945         variant->byref = marshal_alloc(sizeof(gpointer));
2946         *((gunichar**)variant->byref) = (gunichar*)marshal_bstr_alloc("PI");
2947
2948         return 0;
2949 }
2950
2951 LIBTEST_API int STDCALL 
2952 mono_test_marshal_variant_out_bool_true (VARIANT* variant)
2953 {
2954         variant->vt = VT_BOOL;
2955         variant->boolVal = VARIANT_TRUE;
2956
2957         return 0;
2958 }
2959
2960 LIBTEST_API int STDCALL 
2961 mono_test_marshal_variant_out_bool_true_byref (VARIANT* variant)
2962 {
2963         variant->vt = VT_BOOL|VT_BYREF;
2964         variant->byref = marshal_alloc(2);
2965         *((gint16*)variant->byref) = VARIANT_TRUE;
2966
2967         return 0;
2968 }
2969
2970 LIBTEST_API int STDCALL 
2971 mono_test_marshal_variant_out_bool_false (VARIANT* variant)
2972 {
2973         variant->vt = VT_BOOL;
2974         variant->boolVal = VARIANT_FALSE;
2975
2976         return 0;
2977 }
2978
2979 LIBTEST_API int STDCALL 
2980 mono_test_marshal_variant_out_bool_false_byref (VARIANT* variant)
2981 {
2982         variant->vt = VT_BOOL|VT_BYREF;
2983         variant->byref = marshal_alloc(2);
2984         *((gint16*)variant->byref) = VARIANT_FALSE;
2985
2986         return 0;
2987 }
2988
2989 typedef int (STDCALL *VarFunc) (int vt, VARIANT variant);
2990 typedef int (STDCALL *VarRefFunc) (int vt, VARIANT* variant);
2991
2992 LIBTEST_API int STDCALL 
2993 mono_test_marshal_variant_in_sbyte_unmanaged(VarFunc func)
2994 {
2995         VARIANT vt;
2996         vt.vt = VT_I1;
2997         vt.cVal = -100;
2998         return func (VT_I1, vt);
2999 }
3000
3001 LIBTEST_API int STDCALL 
3002 mono_test_marshal_variant_in_byte_unmanaged(VarFunc func)
3003 {
3004         VARIANT vt;
3005         vt.vt = VT_UI1;
3006         vt.bVal = 100;
3007         return func (VT_UI1, vt);
3008 }
3009
3010 LIBTEST_API int STDCALL 
3011 mono_test_marshal_variant_in_short_unmanaged(VarFunc func)
3012 {
3013         VARIANT vt;
3014         vt.vt = VT_I2;
3015         vt.iVal = -100;
3016         return func (VT_I2, vt);
3017 }
3018
3019 LIBTEST_API int STDCALL 
3020 mono_test_marshal_variant_in_ushort_unmanaged(VarFunc func)
3021 {
3022         VARIANT vt;
3023         vt.vt = VT_UI2;
3024         vt.uiVal = 100;
3025         return func (VT_UI2, vt);
3026 }
3027
3028 LIBTEST_API int STDCALL 
3029 mono_test_marshal_variant_in_int_unmanaged(VarFunc func)
3030 {
3031         VARIANT vt;
3032         vt.vt = VT_I4;
3033         vt.lVal = -100;
3034         return func (VT_I4, vt);
3035 }
3036
3037 LIBTEST_API int STDCALL 
3038 mono_test_marshal_variant_in_uint_unmanaged(VarFunc func)
3039 {
3040         VARIANT vt;
3041         vt.vt = VT_UI4;
3042         vt.ulVal = 100;
3043         return func (VT_UI4, vt);
3044 }
3045
3046 LIBTEST_API int STDCALL 
3047 mono_test_marshal_variant_in_long_unmanaged(VarFunc func)
3048 {
3049         VARIANT vt;
3050         vt.vt = VT_I8;
3051         vt.llVal = -100;
3052         return func (VT_I8, vt);
3053 }
3054
3055 LIBTEST_API int STDCALL 
3056 mono_test_marshal_variant_in_ulong_unmanaged(VarFunc func)
3057 {
3058         VARIANT vt;
3059         vt.vt = VT_UI8;
3060         vt.ullVal = 100;
3061         return func (VT_UI8, vt);
3062 }
3063
3064 LIBTEST_API int STDCALL 
3065 mono_test_marshal_variant_in_float_unmanaged(VarFunc func)
3066 {
3067         VARIANT vt;
3068         vt.vt = VT_R4;
3069         vt.fltVal = 3.14;
3070         return func (VT_R4, vt);
3071 }
3072
3073 LIBTEST_API int STDCALL 
3074 mono_test_marshal_variant_in_double_unmanaged(VarFunc func)
3075 {
3076         VARIANT vt;
3077         vt.vt = VT_R8;
3078         vt.dblVal = 3.14;
3079         return func (VT_R8, vt);
3080 }
3081
3082 LIBTEST_API int STDCALL 
3083 mono_test_marshal_variant_in_bstr_unmanaged(VarFunc func)
3084 {
3085         VARIANT vt;
3086         vt.vt = VT_BSTR;
3087         vt.bstrVal = marshal_bstr_alloc("PI");
3088         return func (VT_BSTR, vt);
3089 }
3090
3091 LIBTEST_API int STDCALL 
3092 mono_test_marshal_variant_in_bool_true_unmanaged(VarFunc func)
3093 {
3094         VARIANT vt;
3095         vt.vt = VT_BOOL;
3096         vt.boolVal = VARIANT_TRUE;
3097         return func (VT_BOOL, vt);
3098 }
3099
3100 LIBTEST_API int STDCALL 
3101 mono_test_marshal_variant_in_bool_false_unmanaged(VarFunc func)
3102 {
3103         VARIANT vt;
3104         vt.vt = VT_BOOL;
3105         vt.boolVal = VARIANT_FALSE;
3106         return func (VT_BOOL, vt);
3107 }
3108
3109 LIBTEST_API int STDCALL 
3110 mono_test_marshal_variant_out_sbyte_unmanaged(VarRefFunc func)
3111 {
3112         VARIANT vt;
3113         VariantInit (&vt);
3114         func (VT_I1, &vt);
3115         if (vt.vt == VT_I1 && vt.cVal == -100)
3116                 return 0;
3117         return 1;
3118 }
3119
3120 LIBTEST_API int STDCALL 
3121 mono_test_marshal_variant_out_byte_unmanaged(VarRefFunc func)
3122 {
3123         VARIANT vt;
3124         VariantInit (&vt);
3125         func (VT_UI1, &vt);
3126         if (vt.vt == VT_UI1 && vt.bVal == 100)
3127                 return 0;
3128         return 1;
3129 }
3130
3131 LIBTEST_API int STDCALL 
3132 mono_test_marshal_variant_out_short_unmanaged(VarRefFunc func)
3133 {
3134         VARIANT vt;
3135         VariantInit (&vt);
3136         func (VT_I2, &vt);
3137         if (vt.vt == VT_I2 && vt.iVal == -100)
3138                 return 0;
3139         return 1;
3140 }
3141
3142 LIBTEST_API int STDCALL 
3143 mono_test_marshal_variant_out_ushort_unmanaged(VarRefFunc func)
3144 {
3145         VARIANT vt;
3146         VariantInit (&vt);
3147         func (VT_UI2, &vt);
3148         if (vt.vt == VT_UI2 && vt.uiVal == 100)
3149                 return 0;
3150         return 1;
3151 }
3152
3153 LIBTEST_API int STDCALL 
3154 mono_test_marshal_variant_out_int_unmanaged(VarRefFunc func)
3155 {
3156         VARIANT vt;
3157         VariantInit (&vt);
3158         func (VT_I4, &vt);
3159         if (vt.vt == VT_I4 && vt.lVal == -100)
3160                 return 0;
3161         return 1;
3162 }
3163
3164 LIBTEST_API int STDCALL 
3165 mono_test_marshal_variant_out_uint_unmanaged(VarRefFunc func)
3166 {
3167         VARIANT vt;
3168         VariantInit (&vt);
3169         func (VT_UI4, &vt);
3170         if (vt.vt == VT_UI4 && vt.ulVal == 100)
3171                 return 0;
3172         return 1;
3173 }
3174
3175 LIBTEST_API int STDCALL 
3176 mono_test_marshal_variant_out_long_unmanaged(VarRefFunc func)
3177 {
3178         VARIANT vt;
3179         VariantInit (&vt);
3180         func (VT_I8, &vt);
3181         if (vt.vt == VT_I8 && vt.llVal == -100)
3182                 return 0;
3183         return 1;
3184 }
3185
3186 LIBTEST_API int STDCALL 
3187 mono_test_marshal_variant_out_ulong_unmanaged(VarRefFunc func)
3188 {
3189         VARIANT vt;
3190         VariantInit (&vt);
3191         func (VT_UI8, &vt);
3192         if (vt.vt == VT_UI8 && vt.ullVal == 100)
3193                 return 0;
3194         return 1;
3195 }
3196
3197 LIBTEST_API int STDCALL 
3198 mono_test_marshal_variant_out_float_unmanaged(VarRefFunc func)
3199 {
3200         VARIANT vt;
3201         VariantInit (&vt);
3202         func (VT_R4, &vt);
3203         if (vt.vt == VT_R4 && fabs (vt.fltVal - 3.14f) < 1e-10)
3204                 return 0;
3205         return 1;
3206 }
3207
3208 LIBTEST_API int STDCALL 
3209 mono_test_marshal_variant_out_double_unmanaged(VarRefFunc func)
3210 {
3211         VARIANT vt;
3212         VariantInit (&vt);
3213         func (VT_R8, &vt);
3214         if (vt.vt == VT_R8 && fabs (vt.dblVal - 3.14) < 1e-10)
3215                 return 0;
3216         return 1;
3217 }
3218
3219 LIBTEST_API int STDCALL 
3220 mono_test_marshal_variant_out_bstr_unmanaged(VarRefFunc func)
3221 {
3222         VARIANT vt;
3223         gchar* bstr_utf8;
3224         gint32 result = 0;
3225
3226
3227         VariantInit (&vt);
3228         func (VT_BSTR, &vt);
3229         bstr_utf8 = g_utf16_to_utf8 (vt.bstrVal, -1, NULL, NULL, NULL);
3230         result = strcmp("PI", bstr_utf8);
3231         g_free(bstr_utf8);
3232         if (vt.vt == VT_BSTR && !result)
3233                 return 0;
3234         return 1;
3235 }
3236
3237 LIBTEST_API int STDCALL 
3238 mono_test_marshal_variant_out_bool_true_unmanaged(VarRefFunc func)
3239 {
3240         VARIANT vt;
3241         VariantInit (&vt);
3242         func (VT_BOOL, &vt);
3243         if (vt.vt == VT_BOOL && vt.boolVal == VARIANT_TRUE)
3244                 return 0;
3245         return 1;
3246 }
3247
3248 LIBTEST_API int STDCALL 
3249 mono_test_marshal_variant_out_bool_false_unmanaged(VarRefFunc func)
3250 {
3251         VARIANT vt;
3252         VariantInit (&vt);
3253         func (VT_BOOL, &vt);
3254         if (vt.vt == VT_BOOL && vt.boolVal == VARIANT_TRUE)
3255                 return 0;
3256         return 1;
3257 }
3258
3259 typedef struct MonoComObject MonoComObject;
3260
3261 typedef struct
3262 {
3263         int (STDCALL *QueryInterface)(MonoComObject* pUnk, gpointer riid, gpointer* ppv);
3264         int (STDCALL *AddRef)(MonoComObject* pUnk);
3265         int (STDCALL *Release)(MonoComObject* pUnk);
3266         int (STDCALL *get_ITest)(MonoComObject* pUnk, MonoComObject* *ppUnk);
3267         int (STDCALL *SByteIn)(MonoComObject* pUnk, char a);
3268         int (STDCALL *ByteIn)(MonoComObject* pUnk, unsigned char a);
3269         int (STDCALL *ShortIn)(MonoComObject* pUnk, short a);
3270         int (STDCALL *UShortIn)(MonoComObject* pUnk, unsigned short a);
3271         int (STDCALL *IntIn)(MonoComObject* pUnk, int a);
3272         int (STDCALL *UIntIn)(MonoComObject* pUnk, unsigned int a);
3273         int (STDCALL *LongIn)(MonoComObject* pUnk, gint64 a);
3274         int (STDCALL *ULongIn)(MonoComObject* pUnk, guint64 a);
3275         int (STDCALL *FloatIn)(MonoComObject* pUnk, float a);
3276         int (STDCALL *DoubleIn)(MonoComObject* pUnk, double a);
3277         int (STDCALL *ITestIn)(MonoComObject* pUnk, MonoComObject* pUnk2);
3278         int (STDCALL *ITestOut)(MonoComObject* pUnk, MonoComObject* *ppUnk);
3279 } MonoIUnknown;
3280
3281 struct MonoComObject
3282 {
3283         MonoIUnknown* vtbl;
3284         int m_ref;
3285 };
3286
3287 static GUID IID_ITest = {0, 0, 0, {0,0,0,0,0,0,0,1}};
3288 static GUID IID_IMonoUnknown = {0, 0, 0, {0xc0,0,0,0,0,0,0,0x46}};
3289 static GUID IID_IMonoDispatch = {0x00020400, 0, 0, {0xc0,0,0,0,0,0,0,0x46}};
3290
3291 LIBTEST_API int STDCALL
3292 MonoQueryInterface(MonoComObject* pUnk, gpointer riid, gpointer* ppv)
3293 {
3294
3295         *ppv = NULL;
3296         if (!memcmp(riid, &IID_IMonoUnknown, sizeof(GUID))) {
3297                 *ppv = pUnk;
3298                 return S_OK;
3299         }
3300         else if (!memcmp(riid, &IID_ITest, sizeof(GUID))) {
3301                 *ppv = pUnk;
3302                 return S_OK;
3303         }
3304         else if (!memcmp(riid, &IID_IMonoDispatch, sizeof(GUID))) {
3305                 *ppv = pUnk;
3306                 return S_OK;
3307         }
3308         return 0x80004002; //E_NOINTERFACE;
3309 }
3310
3311 LIBTEST_API int STDCALL 
3312 MonoAddRef(MonoComObject* pUnk)
3313 {
3314         return ++(pUnk->m_ref);
3315 }
3316
3317 LIBTEST_API int STDCALL 
3318 MonoRelease(MonoComObject* pUnk)
3319 {
3320         return --(pUnk->m_ref);
3321 }
3322
3323 LIBTEST_API int STDCALL 
3324 SByteIn(MonoComObject* pUnk, char a)
3325 {
3326         return S_OK;
3327 }
3328
3329 LIBTEST_API int STDCALL 
3330 ByteIn(MonoComObject* pUnk, unsigned char a)
3331 {
3332         return S_OK;
3333 }
3334
3335 LIBTEST_API int STDCALL 
3336 ShortIn(MonoComObject* pUnk, short a)
3337 {
3338         return S_OK;
3339 }
3340
3341 LIBTEST_API int STDCALL 
3342 UShortIn(MonoComObject* pUnk, unsigned short a)
3343 {
3344         return S_OK;
3345 }
3346
3347 LIBTEST_API int STDCALL 
3348 IntIn(MonoComObject* pUnk, int a)
3349 {
3350         return S_OK;
3351 }
3352
3353 LIBTEST_API int STDCALL 
3354 UIntIn(MonoComObject* pUnk, unsigned int a)
3355 {
3356         return S_OK;
3357 }
3358
3359 LIBTEST_API int STDCALL 
3360 LongIn(MonoComObject* pUnk, gint64 a)
3361 {
3362         return S_OK;
3363 }
3364
3365 LIBTEST_API int STDCALL 
3366 ULongIn(MonoComObject* pUnk, guint64 a)
3367 {
3368         return S_OK;
3369 }
3370
3371 LIBTEST_API int STDCALL 
3372 FloatIn(MonoComObject* pUnk, float a)
3373 {
3374         return S_OK;
3375 }
3376
3377 LIBTEST_API int STDCALL 
3378 DoubleIn(MonoComObject* pUnk, double a)
3379 {
3380         return S_OK;
3381 }
3382
3383 LIBTEST_API int STDCALL 
3384 ITestIn(MonoComObject* pUnk, MonoComObject *pUnk2)
3385 {
3386         return S_OK;
3387 }
3388
3389 LIBTEST_API int STDCALL 
3390 ITestOut(MonoComObject* pUnk, MonoComObject* *ppUnk)
3391 {
3392         return S_OK;
3393 }
3394
3395 static void create_com_object (MonoComObject** pOut);
3396
3397 LIBTEST_API int STDCALL 
3398 get_ITest(MonoComObject* pUnk, MonoComObject* *ppUnk)
3399 {
3400         create_com_object (ppUnk);
3401         return S_OK;
3402 }
3403
3404 static void create_com_object (MonoComObject** pOut)
3405 {
3406         *pOut = marshal_new0 (MonoComObject, 1);
3407         (*pOut)->vtbl = marshal_new0 (MonoIUnknown, 1);
3408
3409         (*pOut)->m_ref = 1;
3410         (*pOut)->vtbl->QueryInterface = MonoQueryInterface;
3411         (*pOut)->vtbl->AddRef = MonoAddRef;
3412         (*pOut)->vtbl->Release = MonoRelease;
3413         (*pOut)->vtbl->SByteIn = SByteIn;
3414         (*pOut)->vtbl->ByteIn = ByteIn;
3415         (*pOut)->vtbl->ShortIn = ShortIn;
3416         (*pOut)->vtbl->UShortIn = UShortIn;
3417         (*pOut)->vtbl->IntIn = IntIn;
3418         (*pOut)->vtbl->UIntIn = UIntIn;
3419         (*pOut)->vtbl->LongIn = LongIn;
3420         (*pOut)->vtbl->ULongIn = ULongIn;
3421         (*pOut)->vtbl->FloatIn = FloatIn;
3422         (*pOut)->vtbl->DoubleIn = DoubleIn;
3423         (*pOut)->vtbl->ITestIn = ITestIn;
3424         (*pOut)->vtbl->ITestOut = ITestOut;
3425         (*pOut)->vtbl->get_ITest = get_ITest;
3426 }
3427
3428 static MonoComObject* same_object = NULL;
3429
3430 LIBTEST_API int STDCALL 
3431 mono_test_marshal_com_object_create(MonoComObject* *pUnk)
3432 {
3433         create_com_object (pUnk);
3434
3435         if (!same_object)
3436                 same_object = *pUnk;
3437
3438         return 0;
3439 }
3440
3441 LIBTEST_API int STDCALL 
3442 mono_test_marshal_com_object_same(MonoComObject* *pUnk)
3443 {
3444         *pUnk = same_object;
3445
3446         return 0;
3447 }
3448
3449 LIBTEST_API int STDCALL 
3450 mono_test_marshal_com_object_destroy(MonoComObject *pUnk)
3451 {
3452         int ref = --(pUnk->m_ref);
3453         g_free(pUnk->vtbl);
3454         g_free(pUnk);
3455
3456         return ref;
3457 }
3458
3459 LIBTEST_API int STDCALL 
3460 mono_test_marshal_com_object_ref_count(MonoComObject *pUnk)
3461 {
3462         return pUnk->m_ref;
3463 }
3464
3465 LIBTEST_API int STDCALL 
3466 mono_test_marshal_ccw_itest (MonoComObject *pUnk)
3467 {
3468         int hr = 0;
3469         MonoComObject* pTest;
3470
3471         if (!pUnk)
3472                 return 1;
3473
3474         hr = pUnk->vtbl->SByteIn (pUnk, -100);
3475         if (hr != 0)
3476                 return 2;
3477         hr = pUnk->vtbl->ByteIn (pUnk, 100);
3478         if (hr != 0)
3479                 return 3;
3480         hr = pUnk->vtbl->ShortIn (pUnk, -100);
3481         if (hr != 0)
3482                 return 4;
3483         hr = pUnk->vtbl->UShortIn (pUnk, 100);
3484         if (hr != 0)
3485                 return 5;
3486         hr = pUnk->vtbl->IntIn (pUnk, -100);
3487         if (hr != 0)
3488                 return 6;
3489         hr = pUnk->vtbl->UIntIn (pUnk, 100);
3490         if (hr != 0)
3491                 return 7;
3492         hr = pUnk->vtbl->LongIn (pUnk, -100);
3493         if (hr != 0)
3494                 return 8;
3495         hr = pUnk->vtbl->ULongIn (pUnk, 100);
3496         if (hr != 0)
3497                 return 9;
3498         hr = pUnk->vtbl->FloatIn (pUnk, 3.14f);
3499         if (hr != 0)
3500                 return 10;
3501         hr = pUnk->vtbl->DoubleIn (pUnk, 3.14);
3502         if (hr != 0)
3503                 return 11;
3504         hr = pUnk->vtbl->ITestIn (pUnk, pUnk);
3505         if (hr != 0)
3506                 return 12;
3507         hr = pUnk->vtbl->ITestOut (pUnk, &pTest);
3508         if (hr != 0)
3509                 return 13;
3510
3511         return 0;
3512 }
3513
3514 /*
3515  * mono_method_get_unmanaged_thunk tests
3516  */
3517
3518 #if defined(__GNUC__) && ((defined(__i386__) && (defined(__linux__) || defined (__APPLE__)) || defined (__FreeBSD__) || defined(__OpenBSD__)) || (defined(__ppc__) && defined(__APPLE__)))
3519 #define ALIGN(size) __attribute__ ((aligned(size)))
3520 #else
3521 #define ALIGN(size)
3522 #endif
3523
3524
3525 /* thunks.cs:TestStruct */
3526 typedef struct _TestStruct {
3527         int A;
3528         double B;
3529 } TestStruct;
3530
3531 /* Searches for mono symbols in all loaded modules */
3532 static gpointer
3533 lookup_mono_symbol (const char *symbol_name)
3534 {
3535         gpointer symbol;
3536         if (g_module_symbol (g_module_open (NULL, G_MODULE_BIND_LAZY), symbol_name, &symbol))
3537                 return symbol;
3538         else
3539                 return NULL;
3540 }
3541
3542 LIBTEST_API gpointer STDCALL
3543 mono_test_marshal_lookup_symbol (const char *symbol_name)
3544 {
3545         return lookup_mono_symbol (symbol_name);
3546 }
3547
3548 /**
3549  * test_method_thunk:
3550  *
3551  * @test_id: the test number
3552  * @test_method_handle: MonoMethod* of the C# test method
3553  * @create_object_method_handle: MonoMethod* of thunks.cs:Test.CreateObject
3554  */
3555 LIBTEST_API int STDCALL  
3556 test_method_thunk (int test_id, gpointer test_method_handle, gpointer create_object_method_handle)
3557 {
3558         gpointer (*mono_method_get_unmanaged_thunk)(gpointer)
3559                 = lookup_mono_symbol ("mono_method_get_unmanaged_thunk");
3560
3561         gpointer (*mono_string_new_wrapper)(const char *)
3562                 = lookup_mono_symbol ("mono_string_new_wrapper");
3563
3564         char* (*mono_string_to_utf8)(gpointer)
3565                 = lookup_mono_symbol ("mono_string_to_utf8");
3566
3567         gpointer (*mono_object_unbox)(gpointer)
3568                 = lookup_mono_symbol ("mono_object_unbox");
3569
3570         gpointer test_method, ex = NULL;
3571         gpointer (STDCALL *CreateObject)(gpointer*);
3572
3573         if (!mono_method_get_unmanaged_thunk)
3574                 return 1;
3575
3576         test_method =  mono_method_get_unmanaged_thunk (test_method_handle);
3577         if (!test_method)
3578                 return 2;
3579
3580         CreateObject = mono_method_get_unmanaged_thunk (create_object_method_handle);
3581         if (!CreateObject)
3582                 return 3;
3583
3584
3585         switch (test_id) {
3586
3587         case 0: {
3588                 /* thunks.cs:Test.Test0 */
3589                 void (STDCALL *F)(gpointer*) = test_method;
3590                 F (&ex);
3591                 break;
3592         }
3593
3594         case 1: {
3595                 /* thunks.cs:Test.Test1 */
3596                 int (STDCALL *F)(gpointer*) = test_method;
3597                 if (F (&ex) != 42)
3598                         return 4;
3599                 break;
3600         }
3601
3602         case 2: {
3603                 /* thunks.cs:Test.Test2 */
3604                 gpointer (STDCALL *F)(gpointer, gpointer*) = test_method;
3605                 gpointer str = mono_string_new_wrapper ("foo");
3606                 if (str != F (str, &ex))
3607                         return 4;
3608                 break;
3609         }
3610
3611         case 3: {
3612                 /* thunks.cs:Test.Test3 */
3613                 gpointer (STDCALL *F)(gpointer, gpointer, gpointer*);
3614                 gpointer obj;
3615                 gpointer str;
3616
3617                 F = test_method;
3618                 obj = CreateObject (&ex);
3619                 str = mono_string_new_wrapper ("bar");
3620
3621                 if (str != F (obj, str, &ex))
3622                         return 4;
3623                 break;
3624         }
3625
3626         case 4: {
3627                 /* thunks.cs:Test.Test4 */
3628                 int (STDCALL *F)(gpointer, gpointer, int, gpointer*);
3629                 gpointer obj;
3630                 gpointer str;
3631
3632                 F = test_method;
3633                 obj = CreateObject (&ex);
3634                 str = mono_string_new_wrapper ("bar");
3635
3636                 if (42 != F (obj, str, 42, &ex))
3637                         return 4;
3638
3639                 break;
3640         }
3641
3642         case 5: {
3643                 /* thunks.cs:Test.Test5 */
3644                 int (STDCALL *F)(gpointer, gpointer, int, gpointer*);
3645                 gpointer obj;
3646                 gpointer str;
3647
3648                 F = test_method;
3649                 obj = CreateObject (&ex);
3650                 str = mono_string_new_wrapper ("bar");
3651
3652                 F (obj, str, 42, &ex);
3653                 if (!ex)
3654                     return 4;
3655
3656                 break;
3657         }
3658
3659         case 6: {
3660                 /* thunks.cs:Test.Test6 */
3661                 int (STDCALL *F)(gpointer, guint8, gint16, gint32, gint64, float, double,
3662                                  gpointer, gpointer*);
3663                 gpointer obj;
3664                 gpointer str = mono_string_new_wrapper ("Test6");
3665                 int res;
3666
3667                 F = test_method;
3668                 obj = CreateObject (&ex);
3669
3670                 res = F (obj, 254, 32700, -245378, 6789600, 3.1415, 3.1415, str, &ex);
3671                 if (ex)
3672                         return 4;
3673
3674                 if (!res)
3675                         return 5;
3676
3677                 break;
3678         }
3679
3680         case 7: {
3681                 /* thunks.cs:Test.Test7 */
3682                 gint64 (STDCALL *F)(gpointer*) = test_method;
3683                 if (F (&ex) != G_MAXINT64)
3684                         return 4;
3685                 break;
3686         }
3687
3688         case 8: {
3689                 /* thunks.cs:Test.Test8 */
3690                 void (STDCALL *F)(guint8*, gint16*, gint32*, gint64*, float*, double*,
3691                                  gpointer*, gpointer*);
3692
3693                 guint8 a1;
3694                 gint16 a2;
3695                 gint32 a3;
3696                 gint64 a4;
3697                 float a5;
3698                 double a6;
3699                 gpointer a7;
3700
3701                 F = test_method;
3702
3703                 F (&a1, &a2, &a3, &a4, &a5, &a6, &a7, &ex);
3704                 if (ex)
3705                         return 4;
3706
3707                 if (!(a1 == 254 &&
3708                       a2 == 32700 &&
3709                       a3 == -245378 &&
3710                       a4 == 6789600 &&
3711                       (fabs (a5 - 3.1415) < 0.001) &&
3712                       (fabs (a6 - 3.1415) < 0.001) &&
3713                       strcmp (mono_string_to_utf8 (a7), "Test8") == 0))
3714                         return 5;
3715
3716                 break;
3717         }
3718
3719         case 9: {
3720                 /* thunks.cs:Test.Test9 */
3721                 void (STDCALL *F)(guint8*, gint16*, gint32*, gint64*, float*, double*,
3722                                  gpointer*, gpointer*);
3723
3724                 guint8 a1;
3725                 gint16 a2;
3726                 gint32 a3;
3727                 gint64 a4;
3728                 float a5;
3729                 double a6;
3730                 gpointer a7;
3731
3732                 F = test_method;
3733
3734                 F (&a1, &a2, &a3, &a4, &a5, &a6, &a7, &ex);
3735                 if (!ex)
3736                         return 4;
3737
3738                 break;
3739         }
3740
3741         case 10: {
3742                 /* thunks.cs:Test.Test10 */
3743                 void (STDCALL *F)(gpointer*, gpointer*);
3744
3745                 gpointer obj1, obj2;
3746
3747                 obj1 = obj2 = CreateObject (&ex);
3748                 if (ex)
3749                         return 4;
3750
3751                 F = test_method;
3752
3753                 F (&obj1, &ex);
3754                 if (ex)
3755                         return 5;
3756
3757                 if (obj1 == obj2)
3758                         return 6;
3759
3760                 break;
3761         }
3762
3763         case 100: {
3764                 /* thunks.cs:TestStruct.Test0 */
3765                 int (STDCALL *F)(gpointer*, gpointer*);
3766
3767                 gpointer obj;
3768                 TestStruct *a1;
3769                 int res;
3770
3771                 obj = CreateObject (&ex);
3772                 if (ex)
3773                         return 4;
3774
3775                 if (!obj)
3776                         return 5;
3777
3778                 a1 = mono_object_unbox (obj);
3779                 if (!a1)
3780                         return 6;
3781
3782                 a1->A = 42;
3783                 a1->B = 3.1415;
3784
3785                 F = test_method;
3786
3787                 res = F (obj, &ex);
3788                 if (ex)
3789                         return 7;
3790
3791                 if (!res)
3792                         return 8;
3793
3794                 /* check whether the call was really by value */
3795                 if (a1->A != 42 || a1->B != 3.1415)
3796                         return 9;
3797
3798                 break;
3799         }
3800
3801         case 101: {
3802                 /* thunks.cs:TestStruct.Test1 */
3803                 void (STDCALL *F)(gpointer, gpointer*);
3804
3805                 TestStruct *a1;
3806                 gpointer obj;
3807
3808                 obj = CreateObject (&ex);
3809                 if (ex)
3810                         return 4;
3811
3812                 if (!obj)
3813                         return 5;
3814
3815                 a1 = mono_object_unbox (obj);
3816                 if (!a1)
3817                         return 6;
3818
3819                 F = test_method;
3820
3821                 F (obj, &ex);
3822                 if (ex)
3823                         return 7;
3824
3825                 if (a1->A != 42)
3826                         return 8;
3827
3828                 if (!(fabs (a1->B - 3.1415) < 0.001))
3829                         return 9;
3830
3831                 break;
3832         }
3833
3834         case 102: {
3835                 /* thunks.cs:TestStruct.Test2 */
3836                 gpointer (STDCALL *F)(gpointer*);
3837
3838                 TestStruct *a1;
3839                 gpointer obj;
3840
3841                 F = test_method;
3842
3843                 obj = F (&ex);
3844                 if (ex)
3845                         return 4;
3846
3847                 if (!obj)
3848                         return 5;
3849
3850                 a1 = mono_object_unbox (obj);
3851
3852                 if (a1->A != 42)
3853                         return 5;
3854
3855                 if (!(fabs (a1->B - 3.1415) < 0.001))
3856                         return 6;
3857
3858                 break;
3859         }
3860
3861         case 103: {
3862                 /* thunks.cs:TestStruct.Test3 */
3863                 void (STDCALL *F)(gpointer, gpointer*);
3864
3865                 TestStruct *a1;
3866                 gpointer obj;
3867
3868                 obj = CreateObject (&ex);
3869                 if (ex)
3870                         return 4;
3871
3872                 if (!obj)
3873                         return 5;
3874                 
3875                 a1 = mono_object_unbox (obj);
3876
3877                 if (!a1)
3878                         return 6;
3879
3880                 a1->A = 42;
3881                 a1->B = 3.1415;
3882
3883                 F = test_method;
3884
3885                 F (obj, &ex);
3886                 if (ex)
3887                         return 4;
3888
3889                 if (a1->A != 1)
3890                         return 5;
3891
3892                 if (a1->B != 17)
3893                         return 6;
3894
3895                 break;
3896         }
3897
3898         default:
3899                 return 9;
3900
3901         }
3902
3903         return 0;
3904 }
3905
3906 typedef struct 
3907 {
3908         char a;
3909 } winx64_struct1;
3910
3911 LIBTEST_API int STDCALL  
3912 mono_test_Winx64_struct1_in (winx64_struct1 var)
3913 {
3914         if (var.a != 123)
3915                 return 1;
3916         return 0;
3917 }
3918
3919 typedef struct
3920 {
3921         char a;
3922         char b;
3923 } winx64_struct2;
3924
3925 LIBTEST_API int STDCALL  
3926 mono_test_Winx64_struct2_in (winx64_struct2 var)
3927 {
3928         if (var.a != 4)
3929                 return 1;
3930         if (var.b != 5)
3931                 return 2;
3932         return 0;
3933 }
3934
3935
3936 typedef struct
3937 {
3938         char a;
3939         char b;
3940         short c;
3941 } winx64_struct3;
3942
3943 LIBTEST_API int STDCALL  
3944 mono_test_Winx64_struct3_in (winx64_struct3 var)
3945 {
3946         if (var.a != 4)
3947                 return 1;
3948         if (var.b != 5)
3949                 return 2;
3950         if (var.c != 0x1234)
3951                 return 3;
3952         return 0;
3953 }
3954
3955 typedef struct
3956 {
3957         char a;
3958         char b;
3959         short c;
3960         unsigned int d;
3961 } winx64_struct4;
3962
3963 LIBTEST_API int STDCALL  
3964 mono_test_Winx64_struct4_in (winx64_struct4 var)
3965 {
3966         if (var.a != 4)
3967                 return 1;
3968         if (var.b != 5)
3969                 return 2;
3970         if (var.c != 0x1234)
3971                 return 3;
3972         if (var.d != 0x87654321)
3973                 return 4;
3974         return 0;
3975 }
3976
3977 typedef struct
3978 {
3979         char a;
3980         char b;
3981         char c;
3982 } winx64_struct5;
3983
3984 LIBTEST_API int STDCALL  
3985 mono_test_Winx64_struct5_in (winx64_struct5 var)
3986 {
3987         if (var.a != 4)
3988                 return 1;
3989         if (var.b != 5)
3990                 return 2;
3991         if (var.c != 6)
3992                 return 3;
3993         return 0;
3994 }
3995
3996 typedef struct
3997 {
3998         winx64_struct1 a;
3999         short b;
4000         char c;
4001 } winx64_struct6;
4002
4003 LIBTEST_API int STDCALL  
4004 mono_test_Winx64_struct6_in (winx64_struct6 var)
4005 {
4006         if (var.a.a != 4)
4007                 return 1;
4008         if (var.b != 5)
4009                 return 2;
4010         if (var.c != 6)
4011                 return 3;
4012         return 0;
4013 }
4014
4015 LIBTEST_API int STDCALL  
4016 mono_test_Winx64_structs_in1 (winx64_struct1 var1,
4017                          winx64_struct2 var2,
4018                          winx64_struct3 var3,
4019                          winx64_struct4 var4)
4020 {
4021         if (var1.a != 123)
4022                 return 1;
4023         
4024         if (var2.a != 4)
4025                 return 2;
4026         if (var2.b != 5)
4027                 return 3;
4028         
4029         if (var3.a != 4)
4030                 return 4;
4031         if (var3.b != 5)
4032                 return 2;
4033         if (var3.c != 0x1234)
4034                 return 5;
4035         
4036         if (var4.a != 4)
4037                 return 6;
4038         if (var4.b != 5)
4039                 return 7;
4040         if (var4.c != 0x1234)
4041                 return 8;
4042         if (var4.d != 0x87654321)
4043                 return 9;
4044         return 0;
4045 }
4046
4047 LIBTEST_API int STDCALL  
4048 mono_test_Winx64_structs_in2 (winx64_struct1 var1,
4049                          winx64_struct1 var2,
4050                          winx64_struct1 var3,
4051                          winx64_struct1 var4,
4052                          winx64_struct1 var5)
4053 {
4054         if (var1.a != 1)
4055                 return 1;
4056         if (var2.a != 2)
4057                 return 2;
4058         if (var3.a != 3)
4059                 return 3;
4060         if (var4.a != 4)
4061                 return 4;
4062         if (var5.a != 5)
4063                 return 5;
4064         
4065         return 0;
4066 }
4067
4068 LIBTEST_API int STDCALL  
4069 mono_test_Winx64_structs_in3 (winx64_struct1 var1,
4070                          winx64_struct5 var2,
4071                          winx64_struct1 var3,
4072                          winx64_struct5 var4,
4073                          winx64_struct1 var5,
4074                          winx64_struct5 var6)
4075 {
4076         if (var1.a != 1)
4077                 return 1;
4078         
4079         if (var2.a != 2)
4080                 return 2;
4081         if (var2.b != 3)
4082                 return 2;
4083         if (var2.c != 4)
4084                 return 4;
4085         
4086         if (var3.a != 5)
4087                 return 5;
4088         
4089         if (var4.a != 6)
4090                 return 6;
4091         if (var4.b != 7)
4092                 return 7;
4093         if (var4.c != 8)
4094                 return 8;
4095         
4096         if (var5.a != 9)
4097                 return 9;
4098
4099         if (var6.a != 10)
4100                 return 10;
4101         if (var6.b != 11)
4102                 return 11;
4103         if (var6.c != 12)
4104                 return 12;
4105         
4106         return 0;
4107 }
4108
4109 LIBTEST_API winx64_struct1 STDCALL  
4110 mono_test_Winx64_struct1_ret (void)
4111 {
4112         winx64_struct1 ret;
4113         ret.a = 123;
4114         return ret;
4115 }
4116
4117 LIBTEST_API winx64_struct2 STDCALL  
4118 mono_test_Winx64_struct2_ret (void)
4119 {
4120         winx64_struct2 ret;
4121         ret.a = 4;
4122         ret.b = 5;
4123         return ret;
4124 }
4125
4126 LIBTEST_API winx64_struct3 STDCALL  
4127 mono_test_Winx64_struct3_ret (void)
4128 {
4129         winx64_struct3 ret;
4130         ret.a = 4;
4131         ret.b = 5;
4132         ret.c = 0x1234;
4133         return ret;
4134 }
4135
4136 LIBTEST_API winx64_struct4 STDCALL  
4137 mono_test_Winx64_struct4_ret (void)
4138 {
4139         winx64_struct4 ret;
4140         ret.a = 4;
4141         ret.b = 5;
4142         ret.c = 0x1234;
4143         ret.d = 0x87654321;
4144         return ret;
4145 }
4146
4147 LIBTEST_API winx64_struct5 STDCALL  
4148 mono_test_Winx64_struct5_ret (void)
4149 {
4150         winx64_struct5 ret;
4151         ret.a = 4;
4152         ret.b = 5;
4153         ret.c = 6;
4154         return ret;
4155 }
4156
4157 LIBTEST_API winx64_struct1 STDCALL  
4158 mono_test_Winx64_struct1_ret_5_args (char a, char b, char c, char d, char e)
4159 {
4160         winx64_struct1 ret;
4161         ret.a = a + b + c + d + e;
4162         return ret;
4163 }
4164
4165 LIBTEST_API winx64_struct5 STDCALL
4166 mono_test_Winx64_struct5_ret6_args (char a, char b, char c, char d, char e)
4167 {
4168         winx64_struct5 ret;
4169         ret.a = a + b;
4170         ret.b = c + d;
4171         ret.c = e;
4172         return ret;
4173 }
4174
4175 typedef struct
4176 {
4177         float a;
4178         float b;
4179 } winx64_floatStruct;
4180
4181 LIBTEST_API int STDCALL  
4182 mono_test_Winx64_floatStruct (winx64_floatStruct a)
4183 {
4184         if (a.a > 5.6 || a.a < 5.4)
4185                 return 1;
4186
4187         if (a.b > 9.6 || a.b < 9.4)
4188                 return 2;
4189         
4190         return 0;
4191 }
4192
4193 typedef struct
4194 {
4195         double a;
4196 } winx64_doubleStruct;
4197
4198 LIBTEST_API int STDCALL  
4199 mono_test_Winx64_doubleStruct (winx64_doubleStruct a)
4200 {
4201         if (a.a > 5.6 || a.a < 5.4)
4202                 return 1;
4203         
4204         return 0;
4205 }
4206
4207 typedef int (STDCALL *managed_struct1_delegate) (winx64_struct1 a);
4208
4209 LIBTEST_API int STDCALL 
4210 mono_test_managed_Winx64_struct1_in(managed_struct1_delegate func)
4211 {
4212         winx64_struct1 val;
4213         val.a = 5;
4214         return func (val);
4215 }
4216
4217 typedef int (STDCALL *managed_struct5_delegate) (winx64_struct5 a);
4218
4219 LIBTEST_API int STDCALL 
4220 mono_test_managed_Winx64_struct5_in(managed_struct5_delegate func)
4221 {
4222         winx64_struct5 val;
4223         val.a = 5;
4224         val.b = 0x10;
4225         val.c = 0x99;
4226         return func (val);
4227 }
4228
4229 typedef int (STDCALL *managed_struct1_struct5_delegate) (winx64_struct1 a, winx64_struct5 b,
4230                                                          winx64_struct1 c, winx64_struct5 d,
4231                                                          winx64_struct1 e, winx64_struct5 f);
4232
4233 LIBTEST_API int STDCALL 
4234 mono_test_managed_Winx64_struct1_struct5_in(managed_struct1_struct5_delegate func)
4235 {
4236         winx64_struct1 a, c, e;
4237         winx64_struct5 b, d, f;
4238         a.a = 1;
4239         b.a = 2; b.b = 3; b.c = 4;
4240         c.a = 5;
4241         d.a = 6; d.b = 7; d.c = 8;
4242         e.a = 9;
4243         f.a = 10; f.b = 11; f.c = 12;
4244
4245         return func (a, b, c, d, e, f);
4246 }
4247
4248 typedef winx64_struct1 (STDCALL *managed_struct1_ret_delegate) (void);
4249
4250 LIBTEST_API int STDCALL 
4251 mono_test_Winx64_struct1_ret_managed (managed_struct1_ret_delegate func)
4252 {
4253         winx64_struct1 ret;
4254
4255         ret = func ();
4256
4257         if (ret.a != 0x45)
4258                 return 1;
4259         
4260         return 0;
4261 }
4262
4263 typedef winx64_struct5 (STDCALL *managed_struct5_ret_delegate) (void);
4264
4265 LIBTEST_API int STDCALL 
4266 mono_test_Winx64_struct5_ret_managed (managed_struct5_ret_delegate func)
4267 {
4268         winx64_struct5 ret;
4269
4270         ret = func ();
4271
4272         if (ret.a != 0x12)
4273                 return 1;
4274         if (ret.b != 0x34)
4275                 return 2;
4276         if (ret.c != 0x56)
4277                 return 3;
4278         
4279         return 0;
4280 }
4281
4282 LIBTEST_API int STDCALL 
4283 mono_test_marshal_bool_in (int arg, unsigned int expected, unsigned int bDefaultMarsh, unsigned int bBoolCustMarsh,
4284                            char bI1CustMarsh, unsigned char bU1CustMarsh, short bVBCustMarsh)
4285 {
4286         switch (arg) {
4287         case 1: 
4288                 if (bDefaultMarsh != expected)
4289                         return 1;
4290                 break;
4291         case 2: 
4292                 if (bBoolCustMarsh != expected)
4293                         return 2;
4294                 break;
4295         case 3: 
4296                 if (bI1CustMarsh != expected)
4297                         return 3;
4298                 break;
4299         case 4: 
4300                 if (bU1CustMarsh != expected)
4301                         return 4;
4302                 break;
4303         case 5: 
4304                 if (bVBCustMarsh != expected)
4305                         return 5;
4306                 break;
4307         default:
4308                 return 999;             
4309         }
4310         return 0;
4311 }
4312
4313 LIBTEST_API int STDCALL 
4314 mono_test_marshal_bool_out (int arg, unsigned int testVal, unsigned int* bDefaultMarsh, unsigned int* bBoolCustMarsh,
4315                            char* bI1CustMarsh, unsigned char* bU1CustMarsh, unsigned short* bVBCustMarsh)
4316 {
4317         switch (arg) {
4318         case 1: 
4319                 if (!bDefaultMarsh)
4320                         return 1;
4321                 *bDefaultMarsh = testVal;
4322                 break;  
4323         case 2: 
4324                 if (!bBoolCustMarsh)
4325                         return 2;
4326                 *bBoolCustMarsh = testVal;
4327                 break;  
4328         case 3: 
4329                 if (!bI1CustMarsh)
4330                         return 3;
4331                 *bI1CustMarsh = (char)testVal;
4332                 break;  
4333         case 4: 
4334                 if (!bU1CustMarsh)
4335                         return 4;
4336                 *bU1CustMarsh = (unsigned char)testVal;
4337                 break;  
4338         case 5: 
4339                 if (!bVBCustMarsh)
4340                         return 5;
4341                 *bVBCustMarsh = (unsigned short)testVal;
4342                 break;  
4343         default:
4344                 return 999;
4345         }
4346         return 0;
4347 }
4348
4349 LIBTEST_API int STDCALL 
4350 mono_test_marshal_bool_ref (int arg, unsigned int expected, unsigned int testVal, unsigned int* bDefaultMarsh,
4351                             unsigned int* bBoolCustMarsh, char* bI1CustMarsh, unsigned char* bU1CustMarsh, 
4352                             unsigned short* bVBCustMarsh)
4353 {
4354         switch (arg) {
4355         case 1: 
4356                 if (!bDefaultMarsh)
4357                         return 1;
4358                 if (*bDefaultMarsh != expected)
4359                         return 2;
4360                 *bDefaultMarsh = testVal;
4361                 break;
4362         case 2: 
4363                 if (!bBoolCustMarsh)
4364                         return 3;
4365                 if (*bBoolCustMarsh != expected)
4366                         return 4;
4367                 *bBoolCustMarsh = testVal;
4368                 break;
4369         case 3: 
4370                 if (!bI1CustMarsh)
4371                         return 5;
4372                 if (*bI1CustMarsh != expected)
4373                         return 6;
4374                 *bI1CustMarsh = (char)testVal;
4375                 break;
4376         case 4: 
4377                 if (!bU1CustMarsh)
4378                         return 7;
4379                 if (*bU1CustMarsh != expected)
4380                         return 8;
4381                 *bU1CustMarsh = (unsigned char)testVal;
4382                 break;
4383         case 5: 
4384                 if (!bVBCustMarsh)
4385                         return 9;
4386                 if (*bVBCustMarsh != expected)
4387                         return 10;
4388                 *bVBCustMarsh = (unsigned short)testVal;
4389                 break;
4390         default:
4391                 return 999;             
4392         }
4393         return 0;
4394 }
4395
4396
4397 typedef int (STDCALL *MarshalBoolInDelegate) (int arg, unsigned int expected, unsigned int bDefaultMarsh,
4398         unsigned int bBoolCustMarsh, char bI1CustMarsh, unsigned char bU1CustMarsh, unsigned short bVBCustMarsh);
4399
4400 LIBTEST_API int STDCALL 
4401 mono_test_managed_marshal_bool_in (int arg, unsigned int expected, unsigned int testVal, MarshalBoolInDelegate pfcn)
4402 {
4403         if (!pfcn)
4404                 return 0x9900;
4405
4406         switch (arg) {
4407         case 1:
4408                 return pfcn (arg, expected, testVal, 0, 0, 0, 0);
4409         case 2:
4410                 return pfcn (arg, expected, 0, testVal,  0, 0, 0);
4411         case 3:
4412                 return pfcn (arg, expected, 0, 0, testVal, 0, 0);
4413         case 4:
4414                 return pfcn (arg, expected, 0, 0, 0, testVal, 0);
4415         case 5:
4416                 return pfcn (arg, expected, 0, 0, 0, 0, testVal);
4417         default:
4418                 return 0x9800;
4419         }
4420
4421         return 0;
4422 }
4423
4424 typedef int (STDCALL *MarshalBoolOutDelegate) (int arg, unsigned int expected, unsigned int* bDefaultMarsh,
4425         unsigned int* bBoolCustMarsh, char* bI1CustMarsh, unsigned char* bU1CustMarsh, unsigned short* bVBCustMarsh);
4426
4427 LIBTEST_API int STDCALL 
4428 mono_test_managed_marshal_bool_out (int arg, unsigned int expected, unsigned int testVal, MarshalBoolOutDelegate pfcn)
4429 {
4430         int ret;
4431         unsigned int lDefaultMarsh, lBoolCustMarsh;
4432         char lI1CustMarsh = 0;
4433         unsigned char lU1CustMarsh = 0;
4434         unsigned short lVBCustMarsh = 0;
4435         lDefaultMarsh = lBoolCustMarsh = 0;
4436
4437         if (!pfcn)
4438                 return 0x9900;
4439
4440         switch (arg) {
4441         case 1: {
4442                 unsigned int ltVal = 0;
4443                 ret = pfcn (arg, testVal, &ltVal, &lBoolCustMarsh, &lI1CustMarsh, &lU1CustMarsh, &lVBCustMarsh);
4444                 if (ret)
4445                         return 0x0100 + ret;
4446                 if (expected != ltVal)
4447                         return 0x0200;
4448                 break;
4449         }
4450         case 2: {
4451                 unsigned int ltVal = 0;
4452                 ret = pfcn (arg, testVal, &lDefaultMarsh, &ltVal, &lI1CustMarsh, &lU1CustMarsh, &lVBCustMarsh);
4453                 if (ret)
4454                         return 0x0300 + ret;
4455                 if (expected != ltVal)
4456                         return 0x0400;
4457                 break;
4458         }
4459         case 3: {
4460                 char ltVal = 0;
4461                 ret = pfcn (arg, testVal, &lDefaultMarsh, &lBoolCustMarsh, &ltVal, &lU1CustMarsh, &lVBCustMarsh);
4462                 if (ret)
4463                         return 0x0500 + ret;
4464                 if (expected != ltVal)
4465                         return 0x0600;
4466                 break;
4467         }
4468         case 4: {
4469                 unsigned char ltVal = 0;
4470                 ret = pfcn (arg, testVal, &lDefaultMarsh, &lBoolCustMarsh, &lI1CustMarsh, &ltVal, &lVBCustMarsh);
4471                 if (ret)
4472                         return 0x0700 + ret;
4473                 if (expected != ltVal)
4474                         return 0x0800;
4475                 break;
4476         }
4477         case 5: {
4478                 unsigned short ltVal = 0;
4479                 ret = pfcn (arg, testVal, &lDefaultMarsh, &lBoolCustMarsh, &lI1CustMarsh, &lU1CustMarsh, &ltVal);
4480                 if (ret)
4481                         return 0x0900 + ret;
4482                 if (expected != ltVal)
4483                         return 0x1000;
4484                 break;
4485         }
4486         default:
4487                 return 0x9800;
4488         }
4489
4490         return 0;
4491 }
4492
4493 typedef int (STDCALL *MarshalBoolRefDelegate) (int arg, unsigned int expected, unsigned int testVal, unsigned int* bDefaultMarsh,
4494         unsigned int* bBoolCustMarsh, char* bI1CustMarsh, unsigned char* bU1CustMarsh, unsigned short* bVBCustMarsh);
4495
4496 LIBTEST_API int STDCALL 
4497 mono_test_managed_marshal_bool_ref (int arg, unsigned int expected, unsigned int testVal, unsigned int outExpected,
4498                                     unsigned int outTestVal, MarshalBoolRefDelegate pfcn)
4499 {
4500         int ret;
4501         unsigned int lDefaultMarsh, lBoolCustMarsh;
4502         char lI1CustMarsh = 0;
4503         unsigned char lU1CustMarsh = 0;
4504         unsigned short lVBCustMarsh = 0;
4505         lDefaultMarsh = lBoolCustMarsh = 0;
4506
4507         if (!pfcn)
4508                 return 0x9900;
4509
4510         switch (arg) {
4511         case 1:
4512         {
4513                 unsigned int ltestVal = testVal;
4514                 ret = pfcn (arg, expected, outTestVal, &ltestVal, &lBoolCustMarsh, &lI1CustMarsh, &lU1CustMarsh, &lVBCustMarsh);
4515                 if (ret)
4516                         return 0x0100 + ret;
4517                 if (outExpected != ltestVal)
4518                         return 0x0200;
4519                 break;
4520         }
4521         case 2:
4522         {
4523                 unsigned int ltestVal = testVal;
4524                 ret = pfcn (arg, expected, outTestVal, &lDefaultMarsh, &ltestVal, &lI1CustMarsh, &lU1CustMarsh, &lVBCustMarsh);
4525                 if (ret)
4526                         return 0x0300 + ret;
4527                 if (outExpected != ltestVal)
4528                         return 0x0400;
4529                 break;
4530         }
4531         case 3:
4532         {
4533                 char ltestVal = testVal;
4534                 ret = pfcn (arg, expected, outTestVal, &lDefaultMarsh, &lBoolCustMarsh, &ltestVal, &lU1CustMarsh, &lVBCustMarsh);
4535                 if (ret)
4536                         return 0x0500 + ret;
4537                 if (outExpected != ltestVal)
4538                         return 0x0600;
4539                 break;
4540         }
4541         case 4:
4542         {
4543                 unsigned char ltestVal = testVal;
4544                 ret = pfcn (arg, expected, outTestVal, &lDefaultMarsh, &lBoolCustMarsh, &lI1CustMarsh, &ltestVal, &lVBCustMarsh);
4545                 if (ret)
4546                         return 0x0700 + ret;
4547                 if (outExpected != ltestVal)
4548                         return 0x0800;
4549                 break;
4550         }
4551         case 5:
4552         {
4553                 unsigned short ltestVal = testVal;
4554                 ret = pfcn (arg, expected, outTestVal, &lDefaultMarsh, &lBoolCustMarsh, &lI1CustMarsh, &lU1CustMarsh, &ltestVal);
4555                 if (ret)
4556                         return 0x0900 + ret;
4557                 if (outExpected != ltestVal)
4558                         return 0x1000;
4559                 break;
4560         }
4561         default:
4562                 return 0x9800;
4563         }
4564
4565         return 0;
4566 }
4567
4568 #ifdef WIN32
4569
4570 LIBTEST_API int STDCALL 
4571 mono_test_marshal_safearray_out_1dim_vt_bstr_empty (SAFEARRAY** safearray)
4572 {
4573         /* Create an empty one-dimensional array of variants */
4574         SAFEARRAY *pSA;
4575         SAFEARRAYBOUND dimensions [1];
4576
4577         dimensions [0].lLbound = 0;
4578         dimensions [0].cElements = 0;
4579
4580         pSA= SafeArrayCreate (VT_VARIANT, 1, dimensions);
4581         *safearray = pSA;
4582         return S_OK;
4583 }
4584
4585 LIBTEST_API int STDCALL 
4586 mono_test_marshal_safearray_out_1dim_vt_bstr (SAFEARRAY** safearray)
4587 {
4588         /* Create a one-dimensional array of 10 variants filled with "0" to "9" */
4589         SAFEARRAY *pSA;
4590         SAFEARRAYBOUND dimensions [1];
4591         long i;
4592         gchar buffer [20];
4593         HRESULT hr = S_OK;
4594         long indices [1];
4595
4596         dimensions [0].lLbound = 0;
4597         dimensions [0].cElements = 10;
4598
4599         pSA= SafeArrayCreate (VT_VARIANT, 1, dimensions);
4600         for (i= dimensions [0].lLbound; i< (dimensions [0].cElements + dimensions [0].lLbound); i++) {
4601                 VARIANT vOut;
4602                 VariantInit (&vOut);
4603                 vOut.vt = VT_BSTR;
4604                 _ltoa (i,buffer,10);
4605                 vOut.bstrVal= marshal_bstr_alloc (buffer);
4606                 indices [0] = i;
4607                 if ((hr = SafeArrayPutElement (pSA, indices, &vOut)) != S_OK) {
4608                         VariantClear (&vOut);
4609                         SafeArrayDestroy (pSA);
4610                         return hr;
4611                 }
4612                 VariantClear (&vOut);
4613         }
4614         *safearray = pSA;
4615         return hr;
4616 }
4617
4618 LIBTEST_API int STDCALL 
4619 mono_test_marshal_safearray_out_2dim_vt_i4 (SAFEARRAY** safearray)
4620 {
4621         /* Create a two-dimensional array of 4x3 variants filled with 11, 12, 13, etc. */
4622         SAFEARRAY *pSA;
4623         SAFEARRAYBOUND dimensions [2];
4624         long i, j;
4625         HRESULT hr = S_OK;
4626         long indices [2];
4627
4628         dimensions [0].lLbound = 0;
4629         dimensions [0].cElements = 4;
4630         dimensions [1].lLbound = 0;
4631         dimensions [1].cElements = 3;
4632
4633         pSA= SafeArrayCreate(VT_VARIANT, 2, dimensions);
4634         for (i= dimensions [0].lLbound; i< (dimensions [0].cElements + dimensions [0].lLbound); i++) {
4635                 for (j= dimensions [1].lLbound; j< (dimensions [1].cElements + dimensions [1].lLbound); j++) {
4636                         VARIANT vOut;
4637                         VariantInit (&vOut);
4638                         vOut.vt = VT_I4;
4639                         vOut.lVal = (i+1)*10+(j+1);
4640                         indices [0] = i;
4641                         indices [1] = j;
4642                         if ((hr = SafeArrayPutElement (pSA, indices, &vOut)) != S_OK) {
4643                                 VariantClear (&vOut);
4644                                 SafeArrayDestroy (pSA);
4645                                 return hr;
4646                         }
4647                         VariantClear (&vOut);  // does a deep destroy of source VARIANT 
4648                 }
4649         }
4650         *safearray = pSA;
4651         return hr;
4652 }
4653
4654 LIBTEST_API int STDCALL 
4655 mono_test_marshal_safearray_out_4dim_vt_i4 (SAFEARRAY** safearray)
4656 {
4657         /* Create a four-dimensional array of 10x3x6x7 variants filled with their indices */
4658         /* Also use non zero lower bounds                                                 */
4659         SAFEARRAY *pSA;
4660         SAFEARRAYBOUND dimensions [4];
4661         long i;
4662         HRESULT hr = S_OK;
4663         VARIANT *pData;
4664
4665         dimensions [0].lLbound = 15;
4666         dimensions [0].cElements = 10;
4667         dimensions [1].lLbound = 20;
4668         dimensions [1].cElements = 3;
4669         dimensions [2].lLbound = 5;
4670         dimensions [2].cElements = 6;
4671         dimensions [3].lLbound = 12;
4672         dimensions [3].cElements = 7;
4673
4674         pSA= SafeArrayCreate (VT_VARIANT, 4, dimensions);
4675
4676         SafeArrayAccessData (pSA, (void **)&pData);
4677
4678         for (i= 0; i< 10*3*6*7; i++) {
4679                 VariantInit(&pData [i]);
4680                 pData [i].vt = VT_I4;
4681                 pData [i].lVal = i;
4682         }
4683         SafeArrayUnaccessData (pSA);
4684         *safearray = pSA;
4685         return hr;
4686 }
4687
4688 LIBTEST_API int STDCALL 
4689 mono_test_marshal_safearray_in_byval_1dim_empty (SAFEARRAY* safearray)
4690 {
4691         /* Check that array is one dimensional and empty */
4692
4693         UINT dim;
4694         long lbound, ubound;
4695         
4696         dim = SafeArrayGetDim (safearray);
4697         if (dim != 1)
4698                 return 1;
4699
4700         SafeArrayGetLBound (safearray, 1, &lbound);
4701         SafeArrayGetUBound (safearray, 1, &ubound);
4702
4703         if ((lbound > 0) || (ubound > 0))
4704                 return 1;
4705
4706         return 0;
4707 }
4708
4709 LIBTEST_API int STDCALL 
4710 mono_test_marshal_safearray_in_byval_1dim_vt_i4 (SAFEARRAY* safearray)
4711 {
4712         /* Check that array is one dimensional containing integers from 1 to 10 */
4713
4714         UINT dim;
4715         long lbound, ubound;
4716         VARIANT *pData; 
4717         long i;
4718         int result=0;
4719
4720         dim = SafeArrayGetDim (safearray);
4721         if (dim != 1)
4722                 return 1;
4723
4724         SafeArrayGetLBound (safearray, 1, &lbound);
4725         SafeArrayGetUBound (safearray, 1, &ubound);
4726
4727         if ((lbound != 0) || (ubound != 9))
4728                 return 1;
4729
4730         SafeArrayAccessData (safearray, (void **)&pData);
4731         for (i= lbound; i <= ubound; i++) {
4732                 if ((VariantChangeType (&pData [i], &pData [i], VARIANT_NOUSEROVERRIDE, VT_I4) != S_OK) || (pData [i].lVal != i + 1))
4733                         result = 1;
4734         }
4735         SafeArrayUnaccessData (safearray);
4736
4737         return result;
4738 }
4739
4740 LIBTEST_API int STDCALL 
4741 mono_test_marshal_safearray_in_byval_1dim_vt_mixed (SAFEARRAY* safearray)
4742 {
4743         /* Check that array is one dimensional containing integers mixed with strings from 0 to 12 */
4744
4745         UINT dim;
4746         long lbound, ubound;
4747         VARIANT *pData; 
4748         long i;
4749         long indices [1];
4750         VARIANT element;
4751         int result=0;
4752
4753         VariantInit (&element);
4754
4755         dim = SafeArrayGetDim (safearray);
4756         if (dim != 1)
4757                 return 1;
4758
4759         SafeArrayGetLBound (safearray, 1, &lbound);
4760         SafeArrayGetUBound (safearray, 1, &ubound);
4761                 
4762         if ((lbound != 0) || (ubound != 12))
4763                 return 1;
4764
4765         SafeArrayAccessData (safearray, (void **)&pData);
4766         for (i= lbound; i <= ubound; i++) {
4767                 if ((i%2 == 0) && (pData [i].vt != VT_I4))
4768                         result = 1;
4769                 if ((i%2 == 1) && (pData [i].vt != VT_BSTR))
4770                         result = 1;
4771                 if ((VariantChangeType (&pData [i], &pData [i], VARIANT_NOUSEROVERRIDE, VT_I4) != S_OK) || (pData [i].lVal != i))
4772                         result = 1;
4773         }
4774         SafeArrayUnaccessData (safearray);
4775
4776         /* Change the first element of the array to verify that [in] parameters are not marshalled back to the managed side */
4777
4778         indices [0] = 0;
4779         element.vt = VT_I4;
4780         element.lVal = 333;
4781         SafeArrayPutElement (safearray, indices, &element);
4782         VariantClear (&element);
4783
4784         return result;
4785 }
4786
4787 LIBTEST_API int STDCALL 
4788 mono_test_marshal_safearray_in_byval_2dim_vt_i4 (SAFEARRAY* safearray)
4789 {
4790         /* Check that array is one dimensional containing integers mixed with strings from 0 to 12 */
4791
4792         UINT dim;
4793         long lbound1, ubound1, lbound2, ubound2;
4794         long i, j, failed;
4795         long indices [2];
4796         VARIANT element;
4797
4798         VariantInit (&element);
4799
4800         dim = SafeArrayGetDim (safearray);
4801         if (dim != 2)
4802                 return 1;
4803
4804         SafeArrayGetLBound (safearray, 1, &lbound1);
4805         SafeArrayGetUBound (safearray, 1, &ubound1);
4806
4807         if ((lbound1 != 0) || (ubound1 != 1))
4808                 return 1;
4809
4810         SafeArrayGetLBound (safearray, 2, &lbound2);
4811         SafeArrayGetUBound (safearray, 2, &ubound2);
4812
4813         if ((lbound2 != 0) || (ubound2 != 3)) {
4814                 return 1;
4815         }
4816
4817         for (i= lbound1; i <= ubound1; i++) {
4818                 indices [0] = i;
4819                 for (j= lbound2; j <= ubound2; j++) {
4820                         indices [1] = j;
4821                         if (SafeArrayGetElement (safearray, indices, &element) != S_OK)
4822                                 return 1;
4823                         failed = ((element.vt != VT_I4) || (element.lVal != 10*(i+1)+(j+1)));
4824                         VariantClear (&element);
4825                         if (failed)
4826                                 return 1;
4827                 }
4828         }
4829
4830         /* Change the first element of the array to verify that [in] parameters are not marshalled back to the managed side */
4831
4832         indices [0] = 0;
4833         indices [1] = 0;
4834         element.vt = VT_I4;
4835         element.lVal = 333;
4836         SafeArrayPutElement (safearray, indices, &element);
4837         VariantClear (&element);
4838
4839         return 0;
4840 }
4841
4842 LIBTEST_API int STDCALL 
4843 mono_test_marshal_safearray_in_byval_3dim_vt_bstr (SAFEARRAY* safearray)
4844 {
4845         /* Check that array is one dimensional containing integers mixed with strings from 0 to 12 */
4846
4847         UINT dim;
4848         long lbound1, ubound1, lbound2, ubound2, lbound3, ubound3;
4849         long i, j, k, failed;
4850         long indices [3];
4851         VARIANT element;
4852
4853         VariantInit (&element);
4854
4855         dim = SafeArrayGetDim (safearray);
4856         if (dim != 3)
4857                 return 1;
4858
4859         SafeArrayGetLBound (safearray, 1, &lbound1);
4860         SafeArrayGetUBound (safearray, 1, &ubound1);
4861
4862         if ((lbound1 != 0) || (ubound1 != 1))
4863                 return 1;
4864
4865         SafeArrayGetLBound (safearray, 2, &lbound2);
4866         SafeArrayGetUBound (safearray, 2, &ubound2);
4867
4868         if ((lbound2 != 0) || (ubound2 != 1))
4869                 return 1;
4870
4871         SafeArrayGetLBound (safearray, 3, &lbound3);
4872         SafeArrayGetUBound (safearray, 3, &ubound3);
4873
4874         if ((lbound3 != 0) || (ubound3 != 2))
4875                 return 1;
4876
4877         for (i= lbound1; i <= ubound1; i++) {
4878                 indices [0] = i;
4879                 for (j= lbound2; j <= ubound2; j++) {
4880                         indices [1] = j;
4881                 for (k= lbound3; k <= ubound3; k++) {
4882                                 indices [2] = k;
4883                                 if (SafeArrayGetElement (safearray, indices, &element) != S_OK)
4884                                         return 1;
4885                                 failed = ((element.vt != VT_BSTR) 
4886                                         || (VariantChangeType (&element, &element, VARIANT_NOUSEROVERRIDE, VT_I4) != S_OK) 
4887                                         || (element.lVal != 100*(i+1)+10*(j+1)+(k+1)));
4888                                 VariantClear (&element);
4889                                 if (failed)
4890                                         return 1;
4891                         }
4892                 }
4893         }
4894
4895         /* Change the first element of the array to verify that [in] parameters are not marshalled back to the managed side */
4896
4897         indices [0] = 0;
4898         indices [1] = 0;
4899         indices [2] = 0;
4900         element.vt = VT_BSTR;
4901         element.bstrVal = SysAllocString(L"Should not be copied");
4902         SafeArrayPutElement (safearray, indices, &element);
4903         VariantClear (&element);
4904
4905         return 0;
4906 }
4907
4908 LIBTEST_API int STDCALL 
4909 mono_test_marshal_safearray_in_byref_3dim_vt_bstr (SAFEARRAY** safearray)
4910 {
4911         return mono_test_marshal_safearray_in_byval_3dim_vt_bstr (*safearray);
4912 }
4913
4914 LIBTEST_API int STDCALL 
4915 mono_test_marshal_safearray_in_out_byref_1dim_empty (SAFEARRAY** safearray)
4916 {
4917         /* Check that the input array is what is expected and change it so the caller can check */
4918         /* correct marshalling back to managed code                                             */
4919
4920         UINT dim;
4921         long lbound, ubound;
4922         SAFEARRAYBOUND dimensions [1];
4923         long i;
4924         wchar_t buffer [20];
4925         HRESULT hr = S_OK;
4926         long indices [1];
4927
4928         /* Check that in array is one dimensional and empty */
4929
4930         dim = SafeArrayGetDim (*safearray);
4931         if (dim != 1) {
4932                 return 1;
4933         }
4934
4935         SafeArrayGetLBound (*safearray, 1, &lbound);
4936         SafeArrayGetUBound (*safearray, 1, &ubound);
4937                 
4938         if ((lbound > 0) || (ubound > 0)) {
4939                 return 1;
4940         }
4941
4942         /* Re-dimension the array and return a one-dimensional array of 8 variants filled with "0" to "7" */
4943
4944         dimensions [0].lLbound = 0;
4945         dimensions [0].cElements = 8;
4946
4947         hr = SafeArrayRedim (*safearray, dimensions);
4948         if (hr != S_OK)
4949                 return 1;
4950
4951         for (i= dimensions [0].lLbound; i< (dimensions [0].lLbound + dimensions [0].cElements); i++) {
4952                 VARIANT vOut;
4953                 VariantInit (&vOut);
4954                 vOut.vt = VT_BSTR;
4955                 _ltow (i,buffer,10);
4956                 vOut.bstrVal = SysAllocString (buffer);
4957                 indices [0] = i;
4958                 if ((hr = SafeArrayPutElement (*safearray, indices, &vOut)) != S_OK) {
4959                         VariantClear (&vOut);
4960                         SafeArrayDestroy (*safearray);
4961                         return hr;
4962                 }
4963                 VariantClear (&vOut);
4964         }
4965         return hr;
4966 }
4967
4968 LIBTEST_API int STDCALL 
4969 mono_test_marshal_safearray_in_out_byref_3dim_vt_bstr (SAFEARRAY** safearray)
4970 {
4971         /* Check that the input array is what is expected and change it so the caller can check */
4972         /* correct marshalling back to managed code                                             */
4973
4974         UINT dim;
4975         long lbound1, ubound1, lbound2, ubound2, lbound3, ubound3;
4976         SAFEARRAYBOUND dimensions [1];
4977         long i, j, k, failed;
4978         wchar_t buffer [20];
4979         HRESULT hr = S_OK;
4980         long indices [3];
4981         VARIANT element;
4982
4983         VariantInit (&element);
4984
4985         /* Check that in array is three dimensional and contains the expected values */
4986
4987         dim = SafeArrayGetDim (*safearray);
4988         if (dim != 3)
4989                 return 1;
4990
4991         SafeArrayGetLBound (*safearray, 1, &lbound1);
4992         SafeArrayGetUBound (*safearray, 1, &ubound1);
4993
4994         if ((lbound1 != 0) || (ubound1 != 1))
4995                 return 1;
4996
4997         SafeArrayGetLBound (*safearray, 2, &lbound2);
4998         SafeArrayGetUBound (*safearray, 2, &ubound2);
4999
5000         if ((lbound2 != 0) || (ubound2 != 1))
5001                 return 1;
5002
5003         SafeArrayGetLBound (*safearray, 3, &lbound3);
5004         SafeArrayGetUBound (*safearray, 3, &ubound3);
5005
5006         if ((lbound3 != 0) || (ubound3 != 2))
5007                 return 1;
5008
5009         for (i= lbound1; i <= ubound1; i++) {
5010                 indices [0] = i;
5011                 for (j= lbound2; j <= ubound2; j++) {
5012                         indices [1] = j;
5013                         for (k= lbound3; k <= ubound3; k++) {
5014                                 indices [2] = k;
5015                                 if (SafeArrayGetElement (*safearray, indices, &element) != S_OK)
5016                                         return 1;
5017                                 failed = ((element.vt != VT_BSTR) 
5018                                         || (VariantChangeType (&element, &element, VARIANT_NOUSEROVERRIDE, VT_I4) != S_OK) 
5019                                         || (element.lVal != 100*(i+1)+10*(j+1)+(k+1)));
5020                                 VariantClear (&element);
5021                                 if (failed)
5022                                         return 1;
5023                         }
5024                 }
5025         }
5026
5027         hr = SafeArrayDestroy (*safearray);
5028         if (hr != S_OK)
5029                 return 1;
5030
5031         /* Return a new one-dimensional array of 8 variants filled with "0" to "7" */
5032
5033         dimensions [0].lLbound = 0;
5034         dimensions [0].cElements = 8;
5035
5036         *safearray = SafeArrayCreate (VT_VARIANT, 1, dimensions);
5037
5038         for (i= dimensions [0].lLbound; i< (dimensions [0].lLbound + dimensions [0].cElements); i++) {
5039                 VARIANT vOut;
5040                 VariantInit (&vOut);
5041                 vOut.vt = VT_BSTR;
5042                 _ltow (i,buffer,10);
5043                 vOut.bstrVal = SysAllocString (buffer);
5044                 indices [0] = i;
5045                 if ((hr = SafeArrayPutElement (*safearray, indices, &vOut)) != S_OK) {
5046                         VariantClear (&vOut);
5047                         SafeArrayDestroy (*safearray);
5048                         return hr;
5049                 }
5050                 VariantClear (&vOut);
5051         }
5052         return hr;
5053 }
5054
5055 LIBTEST_API int STDCALL 
5056 mono_test_marshal_safearray_in_out_byref_1dim_vt_i4 (SAFEARRAY** safearray)
5057 {
5058         /* Check that the input array is what is expected and change it so the caller can check */
5059         /* correct marshalling back to managed code                                             */
5060
5061         UINT dim;
5062         long lbound1, ubound1;
5063         long i, failed;
5064         HRESULT hr = S_OK;
5065         long indices [1];
5066         VARIANT element;
5067         
5068         VariantInit (&element);
5069
5070         /* Check that in array is one dimensional and contains the expected value */
5071
5072         dim = SafeArrayGetDim (*safearray);
5073         if (dim != 1)
5074                 return 1;
5075
5076         SafeArrayGetLBound (*safearray, 1, &lbound1);
5077         SafeArrayGetUBound (*safearray, 1, &ubound1);
5078
5079         ubound1 = 1;
5080         if ((lbound1 != 0) || (ubound1 != 1))
5081                 return 1;
5082         ubound1 = 0;
5083
5084         for (i= lbound1; i <= ubound1; i++) {
5085                 indices [0] = i;
5086                 if (SafeArrayGetElement (*safearray, indices, &element) != S_OK)
5087                         return 1;
5088                 failed = (element.vt != VT_I4) || (element.lVal != i+1);
5089                 VariantClear (&element);
5090                 if (failed)
5091                         return 1;
5092         }
5093
5094         /* Change one of the elements of the array to verify that [out] parameter is marshalled back to the managed side */
5095
5096         indices [0] = 0;
5097         element.vt = VT_I4;
5098         element.lVal = -1;
5099         SafeArrayPutElement (*safearray, indices, &element);
5100         VariantClear (&element);
5101
5102         return hr;
5103 }
5104
5105 LIBTEST_API int STDCALL 
5106 mono_test_marshal_safearray_in_out_byval_1dim_vt_i4 (SAFEARRAY* safearray)
5107 {
5108         /* Check that the input array is what is expected and change it so the caller can check */
5109         /* correct marshalling back to managed code                                             */
5110
5111         UINT dim;
5112         long lbound1, ubound1;
5113         SAFEARRAYBOUND dimensions [1];
5114         long i, failed;
5115         HRESULT hr = S_OK;
5116         long indices [1];
5117         VARIANT element;
5118
5119         VariantInit (&element);
5120
5121         /* Check that in array is one dimensional and contains the expected value */
5122
5123         dim = SafeArrayGetDim (safearray);
5124         if (dim != 1)
5125                 return 1;
5126
5127         SafeArrayGetLBound (safearray, 1, &lbound1);
5128         SafeArrayGetUBound (safearray, 1, &ubound1);
5129                 
5130         if ((lbound1 != 0) || (ubound1 != 0))
5131                 return 1;
5132
5133         for (i= lbound1; i <= ubound1; i++) {
5134                 indices [0] = i;
5135                 if (SafeArrayGetElement (safearray, indices, &element) != S_OK)
5136                         return 1;
5137                 failed = (element.vt != VT_I4) || (element.lVal != i+1);
5138                 VariantClear (&element);
5139                 if (failed)
5140                         return 1;
5141         }
5142
5143         /* Change the array to verify how [out] parameter is marshalled back to the managed side */
5144
5145         /* Redimension the array */
5146         dimensions [0].lLbound = lbound1;
5147         dimensions [0].cElements = 2;
5148         hr = SafeArrayRedim(safearray, dimensions);
5149
5150         indices [0] = 0;
5151         element.vt = VT_I4;
5152         element.lVal = 12345;
5153         SafeArrayPutElement (safearray, indices, &element);
5154         VariantClear (&element);
5155
5156         indices [0] = 1;
5157         element.vt = VT_I4;
5158         element.lVal = -12345;
5159         SafeArrayPutElement (safearray, indices, &element);
5160         VariantClear (&element);
5161
5162         return hr;
5163 }
5164
5165 LIBTEST_API int STDCALL 
5166 mono_test_marshal_safearray_in_out_byval_3dim_vt_bstr (SAFEARRAY* safearray)
5167 {
5168         /* Check that the input array is what is expected and change it so the caller can check */
5169         /* correct marshalling back to managed code                                             */
5170
5171         UINT dim;
5172         long lbound1, ubound1, lbound2, ubound2, lbound3, ubound3;
5173         long i, j, k, failed;
5174         HRESULT hr = S_OK;
5175         long indices [3];
5176         VARIANT element;
5177
5178         VariantInit (&element);
5179
5180         /* Check that in array is three dimensional and contains the expected values */
5181
5182         dim = SafeArrayGetDim (safearray);
5183         if (dim != 3)
5184                 return 1;
5185
5186         SafeArrayGetLBound (safearray, 1, &lbound1);
5187         SafeArrayGetUBound (safearray, 1, &ubound1);
5188
5189         if ((lbound1 != 0) || (ubound1 != 1))
5190                 return 1;
5191
5192         SafeArrayGetLBound (safearray, 2, &lbound2);
5193         SafeArrayGetUBound (safearray, 2, &ubound2);
5194
5195         if ((lbound2 != 0) || (ubound2 != 1))
5196                 return 1;
5197
5198         SafeArrayGetLBound (safearray, 3, &lbound3);
5199         SafeArrayGetUBound (safearray, 3, &ubound3);
5200
5201         if ((lbound3 != 0) || (ubound3 != 2))
5202                 return 1;
5203
5204         for (i= lbound1; i <= ubound1; i++) {
5205                 indices [0] = i;
5206                 for (j= lbound2; j <= ubound2; j++) {
5207                         indices [1] = j;
5208                         for (k= lbound3; k <= ubound3; k++) {
5209                                 indices [2] = k;
5210                                 if (SafeArrayGetElement (safearray, indices, &element) != S_OK)
5211                                         return 1;
5212                                 failed = ((element.vt != VT_BSTR) 
5213                                         || (VariantChangeType (&element, &element, VARIANT_NOUSEROVERRIDE, VT_I4) != S_OK) 
5214                                         || (element.lVal != 100*(i+1)+10*(j+1)+(k+1)));
5215                                 VariantClear (&element);
5216                                 if (failed)
5217                                         return 1;
5218                         }
5219                 }
5220         }
5221
5222         /* Change the elements of the array to verify that [out] parameter is marshalled back to the managed side */
5223
5224         indices [0] = 1;
5225         indices [1] = 1;
5226         indices [2] = 2;
5227         element.vt = VT_I4;
5228         element.lVal = 333;
5229         SafeArrayPutElement (safearray, indices, &element);
5230         VariantClear (&element);
5231
5232         indices [0] = 1;
5233         indices [1] = 1;
5234         indices [2] = 1;
5235         element.vt = VT_I4;
5236         element.lVal = 111;
5237         SafeArrayPutElement (safearray, indices, &element);
5238         VariantClear (&element);
5239
5240         indices [0] = 0;
5241         indices [1] = 1;
5242         indices [2] = 0;
5243         element.vt = VT_BSTR;
5244         element.bstrVal = marshal_bstr_alloc("ABCDEFG");
5245         SafeArrayPutElement (safearray, indices, &element);
5246         VariantClear (&element);
5247
5248         return hr;
5249 }
5250
5251 LIBTEST_API int STDCALL 
5252 mono_test_marshal_safearray_mixed(
5253                 SAFEARRAY  *safearray1,
5254                 SAFEARRAY **safearray2,
5255                 SAFEARRAY  *safearray3,
5256                 SAFEARRAY **safearray4
5257                 )
5258 {
5259         HRESULT hr = S_OK;
5260
5261         /* Initialize out parameters */
5262         *safearray2 = NULL;
5263
5264         /* array1: Check that in array is one dimensional and contains the expected value */
5265         hr = mono_test_marshal_safearray_in_out_byval_1dim_vt_i4 (safearray1);
5266
5267         /* array2: Fill in with some values to check on the managed side */
5268         if (hr == S_OK)
5269                 hr = mono_test_marshal_safearray_out_1dim_vt_bstr (safearray2);
5270
5271         /* array3: Check that in array is one dimensional and contains the expected value */
5272         if (hr == S_OK)
5273                 hr = mono_test_marshal_safearray_in_byval_1dim_vt_mixed(safearray3);
5274
5275         /* array4: Check input values and fill in with some values to check on the managed side */
5276         if (hr == S_OK)
5277                 hr = mono_test_marshal_safearray_in_out_byref_3dim_vt_bstr(safearray4);
5278
5279         return hr;
5280 }
5281
5282 #endif
5283
5284 static int call_managed_res;
5285
5286 static void
5287 call_managed (gpointer arg)
5288 {
5289         SimpleDelegate del = arg;
5290
5291         call_managed_res = del (42);
5292 }
5293
5294 LIBTEST_API int STDCALL 
5295 mono_test_marshal_thread_attach (SimpleDelegate del)
5296 {
5297 #ifdef WIN32
5298         return 43;
5299 #else
5300         int res;
5301         pthread_t t;
5302
5303         res = pthread_create (&t, NULL, (gpointer)call_managed, del);
5304         g_assert (res == 0);
5305         pthread_join (t, NULL);
5306
5307         return call_managed_res;
5308 #endif
5309 }
5310
5311 typedef int (STDCALL *Callback) (void);
5312
5313 static Callback callback;
5314
5315 LIBTEST_API void STDCALL 
5316 mono_test_marshal_set_callback (Callback cb)
5317 {
5318         callback = cb;
5319 }
5320
5321 LIBTEST_API int STDCALL 
5322 mono_test_marshal_call_callback (void)
5323 {
5324         return callback ();
5325 }
5326
5327 LIBTEST_API int STDCALL
5328 mono_test_marshal_lpstr (char *str)
5329 {
5330         return strcmp ("ABC", str);
5331 }
5332
5333 LIBTEST_API int STDCALL
5334 mono_test_marshal_lpwstr (gunichar2 *str)
5335 {
5336         char *s;
5337         int res;
5338
5339         s = g_utf16_to_utf8 (str, -1, NULL, NULL, NULL);
5340         res = strcmp ("ABC", s);
5341         g_free (s);
5342
5343         return res;
5344 }
5345
5346 LIBTEST_API char* STDCALL
5347 mono_test_marshal_return_lpstr (void)
5348 {
5349         char *res = marshal_alloc (4);
5350         strcpy (res, "XYZ");
5351         return res;
5352 }
5353
5354
5355 LIBTEST_API gunichar2* STDCALL
5356 mono_test_marshal_return_lpwstr (void)
5357 {
5358         gunichar2 *res = marshal_alloc (8);
5359         gunichar2* tmp = g_utf8_to_utf16 ("XYZ", -1, NULL, NULL, NULL);
5360
5361         memcpy (res, tmp, 8);
5362         g_free (tmp);
5363
5364         return res;
5365 }
5366
5367 typedef struct {
5368         double d;
5369 } SingleDoubleStruct;
5370
5371 LIBTEST_API SingleDoubleStruct STDCALL
5372 mono_test_marshal_return_single_double_struct (void)
5373 {
5374         SingleDoubleStruct res;
5375
5376         res.d = 3.0;
5377
5378         return res;
5379 }
5380
5381
5382 #ifndef TARGET_X86
5383
5384 LIBTEST_API int STDCALL
5385 mono_test_has_thiscall (void)
5386 {
5387         return 1;
5388 }
5389
5390 LIBTEST_API int
5391 _mono_test_native_thiscall1 (int arg)
5392 {
5393         return arg;
5394 }
5395
5396 LIBTEST_API int
5397 _mono_test_native_thiscall2 (int arg, int arg2)
5398 {
5399         return arg + (arg2^1);
5400 }
5401
5402 LIBTEST_API int
5403 _mono_test_native_thiscall3 (int arg, int arg2, int arg3)
5404 {
5405         return arg + (arg2^1) + (arg3^2);
5406 }
5407
5408 #elif defined(__GNUC__)
5409
5410 LIBTEST_API int STDCALL
5411 mono_test_has_thiscall (void)
5412 {
5413         return 1;
5414 }
5415
5416 #define def_asm_fn(name) \
5417         "\t.align 4\n" \
5418         "\t.globl _" #name "\n" \
5419         "_" #name ":\n" \
5420         "\t.globl __" #name "\n" \
5421         "__" #name ":\n"
5422
5423 asm(".text\n"
5424
5425 def_asm_fn(mono_test_native_thiscall1)
5426 "\tmovl %ecx,%eax\n"
5427 "\tret\n"
5428
5429 def_asm_fn(mono_test_native_thiscall2)
5430 "\tmovl %ecx,%eax\n"
5431 "\tmovl 4(%esp),%ecx\n"
5432 "\txorl $1,%ecx\n"
5433 "\taddl %ecx,%eax\n"
5434 "\tret $4\n"
5435
5436 def_asm_fn(mono_test_native_thiscall3)
5437 "\tmovl %ecx,%eax\n"
5438 "\tmovl 4(%esp),%ecx\n"
5439 "\txorl $1,%ecx\n"
5440 "\taddl %ecx,%eax\n"
5441 "\tmovl 8(%esp),%ecx\n"
5442 "\txorl $2,%ecx\n"
5443 "\taddl %ecx,%eax\n"
5444 "\tret $8\n"
5445
5446 );
5447
5448 #else
5449
5450 LIBTEST_API int STDCALL
5451 mono_test_has_thiscall (void)
5452 {
5453         return 0;
5454 }
5455
5456 #endif
5457