Merge pull request #1624 from esdrubal/getprocesstimes
[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 typedef struct {
1294        char a[100];
1295 } ByValStrStruct;
1296
1297 LIBTEST_API ByValStrStruct * STDCALL 
1298 mono_test_byvalstr_gen (void)
1299 {
1300         ByValStrStruct *ret;
1301        
1302         ret = malloc(sizeof(ByValStrStruct));
1303         memset(ret, 'a', sizeof(ByValStrStruct)-1);
1304         ret->a[sizeof(ByValStrStruct)-1] = 0;
1305
1306         return ret;
1307 }
1308
1309 LIBTEST_API int STDCALL 
1310 mono_test_byvalstr_check (ByValStrStruct* data, char* correctString)
1311 {
1312         int ret;
1313
1314         ret = strcmp(data->a, correctString);
1315         // printf ("T1: %s\n", data->a);
1316         // printf ("T2: %s\n", correctString);
1317
1318         /* we need g_free because the allocation was performed by mono_test_byvalstr_gen */
1319         g_free (data);
1320         return (ret != 0);
1321 }
1322
1323 typedef struct {
1324         guint16 a[4];
1325         int  flag;
1326 } ByValStrStruct_Unicode;
1327
1328 LIBTEST_API int STDCALL 
1329 mono_test_byvalstr_check_unicode (ByValStrStruct_Unicode *ref, int test)
1330 {
1331         if (ref->flag != 0x1234abcd){
1332                 printf ("overwritten data");
1333                 return 1;
1334         }
1335             
1336         if (test == 1 || test == 3){
1337                 if (ref->a [0] != '1' ||
1338                     ref->a [1] != '2'   ||
1339                     ref->a [2] != '3')
1340                         return 1;
1341                 return 0;
1342         }
1343         if (test == 2){
1344                 if (ref->a [0] != '1' ||
1345                     ref->a [1] != '2')
1346                         return 1;
1347                 return 0;
1348         }
1349         return 10;
1350 }
1351
1352 LIBTEST_API int STDCALL 
1353 NameManglingAnsi (char *data)
1354 {
1355         return data [0] + data [1] + data [2];
1356 }
1357
1358 LIBTEST_API int STDCALL 
1359 NameManglingAnsiA (char *data)
1360 {
1361         g_assert_not_reached ();
1362 }
1363
1364 LIBTEST_API int STDCALL 
1365 NameManglingAnsiW (char *data)
1366 {
1367         g_assert_not_reached ();
1368 }
1369
1370 LIBTEST_API int STDCALL 
1371 NameManglingAnsi2A (char *data)
1372 {
1373         return data [0] + data [1] + data [2];
1374 }
1375
1376 LIBTEST_API int STDCALL 
1377 NameManglingAnsi2W (char *data)
1378 {
1379         g_assert_not_reached ();
1380 }
1381
1382 LIBTEST_API int STDCALL 
1383 NameManglingUnicode (char *data)
1384 {
1385         g_assert_not_reached ();
1386 }
1387
1388 LIBTEST_API int STDCALL 
1389 NameManglingUnicodeW (gunichar2 *data)
1390 {
1391         return data [0] + data [1] + data [2];
1392 }
1393
1394 LIBTEST_API int STDCALL 
1395 NameManglingUnicode2 (gunichar2 *data)
1396 {
1397         return data [0] + data [1] + data [2];
1398 }
1399
1400 LIBTEST_API int STDCALL 
1401 NameManglingAutoW (char *data)
1402 {
1403 #ifdef WIN32
1404         return (data [0] + data [1] + data [2]) == 131 ? 0 : 1;
1405 #else
1406         g_assert_not_reached ();
1407 #endif
1408 }
1409
1410 LIBTEST_API int STDCALL 
1411 NameManglingAuto (char *data)
1412 {
1413 #ifndef WIN32
1414         return (data [0] + data [1] + data [2]) == 198 ? 0 : 1;
1415 #else
1416         g_assert_not_reached ();
1417 #endif
1418 }
1419
1420 typedef int (STDCALL *intcharFunc)(const char*);
1421
1422 LIBTEST_API void STDCALL 
1423 callFunction (intcharFunc f)
1424 {
1425         f ("ABC");
1426 }
1427
1428 typedef struct {
1429         const char* str;
1430         int i;
1431 } SimpleObj;
1432
1433 LIBTEST_API int STDCALL 
1434 class_marshal_test0 (SimpleObj *obj1)
1435 {
1436         // printf ("class_marshal_test0 %s %d\n", obj1->str, obj1->i);
1437
1438         if (strcmp(obj1->str, "T1"))
1439                 return -1;
1440         if (obj1->i != 4)
1441                 return -2;
1442
1443         return 0;
1444 }
1445
1446 LIBTEST_API int STDCALL 
1447 class_marshal_test4 (SimpleObj *obj1)
1448 {
1449         if (obj1)
1450                 return -1;
1451
1452         return 0;
1453 }
1454
1455 LIBTEST_API void STDCALL
1456 class_marshal_test1 (SimpleObj **obj1)
1457 {
1458         SimpleObj *res = malloc (sizeof (SimpleObj));
1459
1460         res->str = marshal_strdup ("ABC");
1461         res->i = 5;
1462
1463         *obj1 = res;
1464 }
1465
1466 LIBTEST_API int STDCALL 
1467 class_marshal_test2 (SimpleObj **obj1)
1468 {
1469         // printf ("class_marshal_test2 %s %d\n", (*obj1)->str, (*obj1)->i);
1470
1471         if (strcmp((*obj1)->str, "ABC"))
1472                 return -1;
1473         if ((*obj1)->i != 5)
1474                 return -2;
1475
1476         return 0;
1477 }
1478
1479 LIBTEST_API int STDCALL 
1480 string_marshal_test0 (char *str)
1481 {
1482         if (strcmp (str, "TEST0"))
1483                 return -1;
1484
1485         return 0;
1486 }
1487
1488 LIBTEST_API void STDCALL
1489 string_marshal_test1 (const char **str)
1490 {
1491         *str = marshal_strdup ("TEST1");
1492 }
1493
1494 LIBTEST_API int STDCALL 
1495 string_marshal_test2 (char **str)
1496 {
1497         // printf ("string_marshal_test2 %s\n", *str);
1498
1499         if (strcmp (*str, "TEST1"))
1500                 return -1;
1501
1502         *str = marshal_strdup ("TEST2");
1503
1504         return 0;
1505 }
1506
1507 LIBTEST_API int STDCALL 
1508 string_marshal_test3 (char *str)
1509 {
1510         if (str)
1511                 return -1;
1512
1513         return 0;
1514 }
1515
1516 typedef struct {
1517         int a;
1518         int b;
1519 } BlittableClass;
1520
1521 LIBTEST_API BlittableClass* STDCALL 
1522 TestBlittableClass (BlittableClass *vl)
1523 {
1524         BlittableClass *res;
1525
1526         // printf ("TestBlittableClass %d %d\n", vl->a, vl->b);
1527
1528         if (vl) {
1529                 vl->a++;
1530                 vl->b++;
1531
1532                 res = marshal_new0 (BlittableClass, 1);
1533                 memcpy (res, vl, sizeof (BlittableClass));
1534         } else {
1535                 res = marshal_new0 (BlittableClass, 1);
1536                 res->a = 42;
1537                 res->b = 43;
1538         }
1539
1540         return res;
1541 }
1542
1543 typedef struct OSVERSIONINFO_STRUCT
1544
1545         int a; 
1546         int b; 
1547 } OSVERSIONINFO_STRUCT;
1548
1549 LIBTEST_API int STDCALL  
1550 MyGetVersionEx (OSVERSIONINFO_STRUCT *osvi)
1551 {
1552
1553         // printf ("GOT %d %d\n", osvi->a, osvi->b);
1554
1555         osvi->a += 1;
1556         osvi->b += 1;
1557
1558         return osvi->a + osvi->b;
1559 }
1560
1561 LIBTEST_API int STDCALL  
1562 BugGetVersionEx (int a, int b, int c, int d, int e, int f, int g, int h, OSVERSIONINFO_STRUCT *osvi)
1563 {
1564
1565         // printf ("GOT %d %d\n", osvi->a, osvi->b);
1566
1567         osvi->a += 1;
1568         osvi->b += 1;
1569
1570         return osvi->a + osvi->b;
1571 }
1572
1573 LIBTEST_API int STDCALL 
1574 mono_test_marshal_point (point pt)
1575 {
1576         // printf("point %g %g\n", pt.x, pt.y);
1577         if (pt.x == 1.25 && pt.y == 3.5)
1578                 return 0;
1579
1580         return 1;
1581 }
1582
1583 typedef struct {
1584         int x;
1585         double y;
1586 } mixed_point;
1587
1588 LIBTEST_API int STDCALL 
1589 mono_test_marshal_mixed_point (mixed_point pt)
1590 {
1591         // printf("mixed point %d %g\n", pt.x, pt.y);
1592         if (pt.x == 5 && pt.y == 6.75)
1593                 return 0;
1594
1595         return 1;
1596 }
1597
1598 LIBTEST_API int STDCALL 
1599 mono_test_marshal_mixed_point_2 (mixed_point *pt)
1600 {
1601         if (pt->x != 5 || pt->y != 6.75)
1602                 return 1;
1603
1604         pt->x = 10;
1605         pt->y = 12.35;
1606
1607         return 0;
1608 }
1609
1610 LIBTEST_API int STDCALL  
1611 marshal_test_ref_bool(int i, char *b1, short *b2, int *b3)
1612 {
1613     int res = 1;
1614     if (*b1 != 0 && *b1 != 1)
1615         return 1;
1616     if (*b2 != 0 && *b2 != -1) /* variant_bool */
1617         return 1;
1618     if (*b3 != 0 && *b3 != 1)
1619         return 1;
1620     if (i == ((*b1 << 2) | (-*b2 << 1) | *b3))
1621         res = 0;
1622     *b1 = !*b1;
1623     *b2 = ~*b2;
1624     *b3 = !*b3;
1625     return res;
1626 }
1627
1628 struct BoolStruct
1629 {
1630     int i;
1631     char b1;
1632     short b2; /* variant_bool */
1633     int b3;
1634 };
1635
1636 LIBTEST_API int STDCALL  
1637 marshal_test_bool_struct(struct BoolStruct *s)
1638 {
1639     int res = 1;
1640     if (s->b1 != 0 && s->b1 != 1)
1641         return 1;
1642     if (s->b2 != 0 && s->b2 != -1)
1643         return 1;
1644     if (s->b3 != 0 && s->b3 != 1)
1645         return 1;
1646     if (s->i == ((s->b1 << 2) | (-s->b2 << 1) | s->b3))
1647         res = 0;
1648     s->b1 = !s->b1;
1649     s->b2 = ~s->b2;
1650     s->b3 = !s->b3;
1651     return res;
1652 }
1653
1654 typedef struct {
1655         gint64 l;
1656 } LongStruct2;
1657
1658 typedef struct {
1659         int i;
1660         LongStruct2 l;
1661 } LongStruct;
1662
1663 LIBTEST_API int STDCALL
1664 mono_test_marshal_long_struct (LongStruct *s)
1665 {
1666         return s->i + s->l.l;
1667 }
1668
1669 LIBTEST_API void STDCALL
1670 mono_test_last_error (int err)
1671 {
1672 #ifdef WIN32
1673         SetLastError (err);
1674 #else
1675         errno = err;
1676 #endif
1677 }
1678
1679 LIBTEST_API int STDCALL 
1680 mono_test_asany (void *ptr, int what)
1681 {
1682         switch (what) {
1683         case 1:
1684                 return (*(int*)ptr == 5) ? 0 : 1;
1685         case 2:
1686                 return strcmp (ptr, "ABC") == 0 ? 0 : 1;
1687         case 3: {
1688                 simplestruct2 ss = *(simplestruct2*)ptr;
1689
1690                 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
1691             !strcmp (ss.d, "TEST") && 
1692             ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
1693                         return 0;
1694                 else
1695                         return 1;
1696         }
1697         case 4: {
1698                 GError *error = NULL;
1699                 char *s;
1700
1701                 s = g_utf16_to_utf8 (ptr, -1, NULL, NULL, &error);
1702
1703                 if (!s)
1704                         return 1;
1705
1706                 if (!strcmp (s, "ABC")) {
1707                         g_free (s);
1708                         return 0;
1709                 }
1710                 else {
1711                         g_free (s);
1712                         return 1;
1713                 }
1714         }
1715         default:
1716                 g_assert_not_reached ();
1717         }
1718
1719         return 1;
1720 }
1721
1722 typedef struct
1723 {
1724         int i;
1725         int j;
1726         int k;
1727         char *s;
1728 } AsAnyStruct;
1729
1730 LIBTEST_API int STDCALL 
1731 mono_test_marshal_asany_in (void* ptr)
1732 {
1733         AsAnyStruct* asAny = ptr;
1734         int res = asAny->i + asAny->j + asAny->k;
1735
1736         return res;
1737 }
1738
1739 LIBTEST_API int STDCALL 
1740 mono_test_marshal_asany_inout (void* ptr)
1741 {
1742         AsAnyStruct* asAny = ptr;
1743         int res = asAny->i + asAny->j + asAny->k;
1744
1745         marshal_free (asAny->s);
1746
1747         asAny->i = 10;
1748         asAny->j = 20;
1749         asAny->k = 30;
1750         asAny->s = 0;
1751
1752         return res;
1753 }
1754
1755 LIBTEST_API int STDCALL 
1756 mono_test_marshal_asany_out (void* ptr)
1757 {
1758         AsAnyStruct* asAny = ptr;
1759         int res = asAny->i + asAny->j + asAny->k;
1760
1761         asAny->i = 10;
1762         asAny->j = 20;
1763         asAny->k = 30;
1764         asAny->s = 0;
1765
1766         return res;
1767 }
1768
1769 /*
1770  * AMD64 marshalling tests.
1771  */
1772
1773 typedef struct amd64_struct1 {
1774         int i;
1775         int j;
1776         int k;
1777         int l;
1778 } amd64_struct1;
1779
1780 LIBTEST_API amd64_struct1 STDCALL 
1781 mono_test_marshal_amd64_pass_return_struct1 (amd64_struct1 s)
1782 {
1783         s.i ++;
1784         s.j ++;
1785         s.k ++;
1786         s.l ++;
1787
1788         return s;
1789 }
1790
1791 LIBTEST_API amd64_struct1 STDCALL 
1792 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)
1793 {
1794         s.i ++;
1795         s.j ++;
1796         s.k ++;
1797         s.l += 1 + i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8;
1798
1799         return s;
1800 }
1801
1802 typedef struct amd64_struct2 {
1803         int i;
1804         int j;
1805 } amd64_struct2;
1806
1807 LIBTEST_API amd64_struct2 STDCALL 
1808 mono_test_marshal_amd64_pass_return_struct2 (amd64_struct2 s)
1809 {
1810         s.i ++;
1811         s.j ++;
1812
1813         return s;
1814 }
1815
1816 typedef struct amd64_struct3 {
1817         int i;
1818 } amd64_struct3;
1819
1820 LIBTEST_API amd64_struct3 STDCALL 
1821 mono_test_marshal_amd64_pass_return_struct3 (amd64_struct3 s)
1822 {
1823         s.i ++;
1824
1825         return s;
1826 }
1827
1828 typedef struct amd64_struct4 {
1829         double d1, d2;
1830 } amd64_struct4;
1831
1832 LIBTEST_API amd64_struct4 STDCALL 
1833 mono_test_marshal_amd64_pass_return_struct4 (amd64_struct4 s)
1834 {
1835         s.d1 ++;
1836         s.d2 ++;
1837
1838         return s;
1839 }
1840
1841 /*
1842  * IA64 marshalling tests.
1843  */
1844 typedef struct test_struct5 {
1845         float d1, d2;
1846 } test_struct5;
1847
1848 LIBTEST_API test_struct5 STDCALL 
1849 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, test_struct5 s, int i, double d3, double d4)
1850 {
1851         s.d1 += d1 + d2 + i;
1852         s.d2 += d3 + d4 + i;
1853
1854         return s;
1855 }
1856
1857 typedef struct test_struct6 {
1858         double d1, d2;
1859 } test_struct6;
1860
1861 LIBTEST_API test_struct6 STDCALL 
1862 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, test_struct6 s, int i, double d3, double d4)
1863 {
1864         s.d1 += d1 + d2 + i;
1865         s.d2 += d3 + d4;
1866
1867         return s;
1868 }
1869
1870 static guint32 custom_res [2];
1871
1872 LIBTEST_API void* STDCALL
1873 mono_test_marshal_pass_return_custom (int i, guint32 *ptr, int j)
1874 {
1875         /* ptr will be freed by CleanupNative, so make a copy */
1876         custom_res [0] = 0; /* not allocated by AllocHGlobal */
1877         custom_res [1] = ptr [1];
1878
1879         return &custom_res;
1880 }
1881
1882 LIBTEST_API int STDCALL 
1883 mono_test_marshal_pass_out_custom (int i, guint32 **ptr, int j)
1884 {
1885         custom_res [0] = 0;
1886         custom_res [1] = i + j + 10;
1887
1888         *ptr = custom_res;
1889
1890         return 0;
1891 }
1892
1893 LIBTEST_API int STDCALL 
1894 mono_test_marshal_pass_inout_custom (int i, guint32 *ptr, int j)
1895 {
1896         ptr [0] = 0;
1897         ptr [1] = i + ptr [1] + j;
1898
1899         return 0;
1900 }
1901
1902 LIBTEST_API int STDCALL 
1903 mono_test_marshal_pass_out_byval_custom (int i, guint32 *ptr, int j)
1904 {
1905         return ptr == NULL ? 0 : 1;
1906 }
1907
1908 LIBTEST_API int STDCALL 
1909 mono_test_marshal_pass_byref_custom (int i, guint32 **ptr, int j)
1910 {
1911         (*ptr)[1] += i + j;
1912
1913         return 0;
1914 }
1915
1916 LIBTEST_API void* STDCALL
1917 mono_test_marshal_pass_return_custom2 (int i, guint32 *ptr, int j)
1918 {
1919         g_assert_not_reached ();
1920
1921         return NULL;
1922 }
1923
1924 LIBTEST_API void* STDCALL
1925 mono_test_marshal_pass_return_custom_null (int i, guint32 *ptr, int j)
1926 {
1927         g_assert (ptr == NULL);
1928
1929         return NULL;
1930 }
1931
1932 typedef void *(STDCALL *PassReturnPtrDelegate) (void *ptr);
1933
1934 LIBTEST_API int STDCALL 
1935 mono_test_marshal_pass_return_custom_in_delegate (PassReturnPtrDelegate del)
1936 {
1937         guint32 buf [2];
1938         guint32 res;
1939         guint32 *ptr;
1940
1941         buf [0] = 0;
1942         buf [1] = 10;
1943
1944         ptr = del (&buf);
1945
1946         res = ptr [1];
1947
1948 #ifdef WIN32
1949         /* FIXME: Freed with FreeHGlobal */
1950 #else
1951         g_free (ptr);
1952 #endif
1953
1954         return res;
1955 }
1956
1957 LIBTEST_API int STDCALL 
1958 mono_test_marshal_pass_return_custom_null_in_delegate (PassReturnPtrDelegate del)
1959 {
1960         void *ptr = del (NULL);
1961
1962         return (ptr == NULL) ? 15 : 0;
1963 }
1964
1965 typedef void (STDCALL *CustomOutParamDelegate) (void **pptr);
1966
1967 LIBTEST_API int STDCALL 
1968 mono_test_marshal_custom_out_param_delegate (CustomOutParamDelegate del)
1969 {
1970         void* pptr = del;
1971
1972         del (&pptr);
1973
1974         if(pptr != NULL)
1975                 return 1;
1976
1977         return 0;
1978 }
1979
1980 typedef int (STDCALL *ReturnEnumDelegate) (int e);
1981
1982 LIBTEST_API int STDCALL 
1983 mono_test_marshal_return_enum_delegate (ReturnEnumDelegate func)
1984 {
1985         return func (1);
1986 }
1987
1988 typedef struct {
1989         int a, b, c;
1990         gint64 d;
1991 } BlittableStruct;
1992         
1993 typedef BlittableStruct (STDCALL *SimpleDelegate10) (BlittableStruct ss);
1994
1995 LIBTEST_API int STDCALL 
1996 mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 delegate)
1997 {
1998         BlittableStruct ss, res;
1999
2000         ss.a = 1;
2001         ss.b = 2;
2002         ss.c = 3;
2003         ss.d = 55;
2004
2005         res = delegate (ss);
2006         if (! ((res.a == -1) && (res.b == -2) && (res.c == -3) && (res.d == -55)))
2007                 return 1;
2008
2009         return 0;
2010 }
2011
2012 LIBTEST_API int STDCALL 
2013 mono_test_stdcall_name_mangling (int a, int b, int c)
2014 {
2015         return a + b + c;
2016 }
2017
2018 LIBTEST_API int
2019 mono_test_stdcall_mismatch_1 (int a, int b, int c)
2020 {
2021         return a + b + c;
2022 }
2023
2024 LIBTEST_API int STDCALL
2025 mono_test_stdcall_mismatch_2 (int a, int b, int c)
2026 {
2027         return a + b + c;
2028 }
2029
2030 /*
2031  * PASSING AND RETURNING SMALL STRUCTURES FROM DELEGATES TESTS
2032  */
2033
2034 typedef struct {
2035         int i;
2036 } SmallStruct1;
2037         
2038 typedef SmallStruct1 (STDCALL *SmallStructDelegate1) (SmallStruct1 ss);
2039
2040 LIBTEST_API int STDCALL 
2041 mono_test_marshal_small_struct_delegate1 (SmallStructDelegate1 delegate)
2042 {
2043         SmallStruct1 ss, res;
2044
2045         ss.i = 1;
2046
2047         res = delegate (ss);
2048         if (! (res.i == -1))
2049                 return 1;
2050
2051         return 0;
2052 }
2053
2054 typedef struct {
2055         gint16 i, j;
2056 } SmallStruct2;
2057         
2058 typedef SmallStruct2 (STDCALL *SmallStructDelegate2) (SmallStruct2 ss);
2059
2060 LIBTEST_API int STDCALL 
2061 mono_test_marshal_small_struct_delegate2 (SmallStructDelegate2 delegate)
2062 {
2063         SmallStruct2 ss, res;
2064
2065         ss.i = 2;
2066         ss.j = 3;
2067
2068         res = delegate (ss);
2069         if (! ((res.i == -2) && (res.j == -3)))
2070                 return 1;
2071
2072         return 0;
2073 }
2074
2075 typedef struct {
2076         gint16 i;
2077         gint8 j;
2078 } SmallStruct3;
2079         
2080 typedef SmallStruct3 (STDCALL *SmallStructDelegate3) (SmallStruct3 ss);
2081
2082 LIBTEST_API int STDCALL 
2083 mono_test_marshal_small_struct_delegate3 (SmallStructDelegate3 delegate)
2084 {
2085         SmallStruct3 ss, res;
2086
2087         ss.i = 1;
2088         ss.j = 2;
2089
2090         res = delegate (ss);
2091         if (! ((res.i == -1) && (res.j == -2)))
2092                 return 1;
2093
2094         return 0;
2095 }
2096
2097 typedef struct {
2098         gint16 i;
2099 } SmallStruct4;
2100         
2101 typedef SmallStruct4 (STDCALL *SmallStructDelegate4) (SmallStruct4 ss);
2102
2103 LIBTEST_API int STDCALL 
2104 mono_test_marshal_small_struct_delegate4 (SmallStructDelegate4 delegate)
2105 {
2106         SmallStruct4 ss, res;
2107
2108         ss.i = 1;
2109
2110         res = delegate (ss);
2111         if (! (res.i == -1))
2112                 return 1;
2113
2114         return 0;
2115 }
2116
2117 typedef struct {
2118         gint64 i;
2119 } SmallStruct5;
2120         
2121 typedef SmallStruct5 (STDCALL *SmallStructDelegate5) (SmallStruct5 ss);
2122
2123 LIBTEST_API int STDCALL 
2124 mono_test_marshal_small_struct_delegate5 (SmallStructDelegate5 delegate)
2125 {
2126         SmallStruct5 ss, res;
2127
2128         ss.i = 5;
2129
2130         res = delegate (ss);
2131         if (! (res.i == -5))
2132                 return 1;
2133
2134         return 0;
2135 }
2136
2137 typedef struct {
2138         int i, j;
2139 } SmallStruct6;
2140         
2141 typedef SmallStruct6 (STDCALL *SmallStructDelegate6) (SmallStruct6 ss);
2142
2143 LIBTEST_API int STDCALL 
2144 mono_test_marshal_small_struct_delegate6 (SmallStructDelegate6 delegate)
2145 {
2146         SmallStruct6 ss, res;
2147
2148         ss.i = 1;
2149         ss.j = 2;
2150
2151         res = delegate (ss);
2152         if (! ((res.i == -1) && (res.j == -2)))
2153                 return 1;
2154
2155         return 0;
2156 }
2157
2158 typedef struct {
2159         int i;
2160         gint16 j;
2161 } SmallStruct7;
2162         
2163 typedef SmallStruct7 (STDCALL *SmallStructDelegate7) (SmallStruct7 ss);
2164
2165 LIBTEST_API int STDCALL 
2166 mono_test_marshal_small_struct_delegate7 (SmallStructDelegate7 delegate)
2167 {
2168         SmallStruct7 ss, res;
2169
2170         ss.i = 1;
2171         ss.j = 2;
2172
2173         res = delegate (ss);
2174         if (! ((res.i == -1) && (res.j == -2)))
2175                 return 1;
2176
2177         return 0;
2178 }
2179
2180 typedef struct {
2181         float i;
2182 } SmallStruct8;
2183         
2184 typedef SmallStruct8 (STDCALL *SmallStructDelegate8) (SmallStruct8 ss);
2185
2186 LIBTEST_API int STDCALL 
2187 mono_test_marshal_small_struct_delegate8 (SmallStructDelegate8 delegate)
2188 {
2189         SmallStruct8 ss, res;
2190
2191         ss.i = 1.0;
2192
2193         res = delegate (ss);
2194         if (! ((res.i == -1.0)))
2195                 return 1;
2196
2197         return 0;
2198 }
2199
2200 typedef struct {
2201         double i;
2202 } SmallStruct9;
2203         
2204 typedef SmallStruct9 (STDCALL *SmallStructDelegate9) (SmallStruct9 ss);
2205
2206 LIBTEST_API int STDCALL 
2207 mono_test_marshal_small_struct_delegate9 (SmallStructDelegate9 delegate)
2208 {
2209         SmallStruct9 ss, res;
2210
2211         ss.i = 1.0;
2212
2213         res = delegate (ss);
2214         if (! ((res.i == -1.0)))
2215                 return 1;
2216
2217         return 0;
2218 }
2219
2220 typedef struct {
2221         float i, j;
2222 } SmallStruct10;
2223         
2224 typedef SmallStruct10 (STDCALL *SmallStructDelegate10) (SmallStruct10 ss);
2225
2226 LIBTEST_API int STDCALL 
2227 mono_test_marshal_small_struct_delegate10 (SmallStructDelegate10 delegate)
2228 {
2229         SmallStruct10 ss, res;
2230
2231         ss.i = 1.0;
2232         ss.j = 2.0;
2233
2234         res = delegate (ss);
2235         if (! ((res.i == -1.0) && (res.j == -2.0)))
2236                 return 1;
2237
2238         return 0;
2239 }
2240
2241 typedef struct {
2242         float i;
2243         int j;
2244 } SmallStruct11;
2245         
2246 typedef SmallStruct11 (STDCALL *SmallStructDelegate11) (SmallStruct11 ss);
2247
2248 LIBTEST_API int STDCALL 
2249 mono_test_marshal_small_struct_delegate11 (SmallStructDelegate11 delegate)
2250 {
2251         SmallStruct11 ss, res;
2252
2253         ss.i = 1.0;
2254         ss.j = 2;
2255
2256         res = delegate (ss);
2257         if (! ((res.i == -1.0) && (res.j == -2)))
2258                 return 1;
2259
2260         return 0;
2261 }
2262
2263 typedef int (STDCALL *ArrayDelegate) (int i, char *j, void *arr);
2264
2265 LIBTEST_API int STDCALL 
2266 mono_test_marshal_array_delegate (void *arr, int len, ArrayDelegate del)
2267 {
2268         return del (len, NULL, arr);
2269 }
2270
2271 typedef int (STDCALL *ArrayDelegateLong) (gint64 i, char *j, void *arr);
2272
2273 LIBTEST_API int STDCALL 
2274 mono_test_marshal_array_delegate_long (void *arr, gint64 len, ArrayDelegateLong del)
2275 {
2276         return del (len, NULL, arr);
2277 }
2278
2279 LIBTEST_API int STDCALL 
2280 mono_test_marshal_out_array_delegate (int *arr, int len, ArrayDelegate del)
2281 {
2282         del (len, NULL, arr);
2283
2284         if ((arr [0] != 1) || (arr [1] != 2))
2285                 return 1;
2286         else
2287                 return 0;
2288 }
2289
2290 typedef gunichar2* (STDCALL *UnicodeStringDelegate) (gunichar2 *message);
2291
2292 LIBTEST_API int STDCALL 
2293 mono_test_marshal_return_unicode_string_delegate (UnicodeStringDelegate del)
2294 {
2295         const char m[] = "abcdef";
2296         gunichar2 *s2, *res;
2297         glong len;
2298
2299         s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
2300
2301         res = del (s2);
2302
2303         marshal_free (res);
2304
2305         return 0;
2306 }
2307
2308 LIBTEST_API int STDCALL 
2309 mono_test_marshal_out_string_array_delegate (char **arr, int len, ArrayDelegate del)
2310 {
2311         del (len, NULL, arr);
2312
2313         if (!strcmp (arr [0], "ABC") && !strcmp (arr [1], "DEF"))
2314                 return 0;
2315         else
2316                 return 1;
2317 }
2318
2319 typedef int (*CdeclDelegate) (int i, int j);
2320
2321 LIBTEST_API int STDCALL 
2322 mono_test_marshal_cdecl_delegate (CdeclDelegate del)
2323 {
2324         int i;
2325
2326         for (i = 0; i < 1000; ++i)
2327                 del (1, 2);
2328
2329         return 0;
2330 }
2331
2332 typedef char** (STDCALL *ReturnStringArrayDelegate) (int i);
2333
2334 LIBTEST_API int STDCALL 
2335 mono_test_marshal_return_string_array_delegate (ReturnStringArrayDelegate d)
2336 {
2337         char **arr = d (2);
2338         int res;
2339
2340         if (arr == NULL)
2341                 return 3;
2342
2343         if (strcmp (arr [0], "ABC") || strcmp (arr [1], "DEF"))
2344                 res = 1;
2345         else
2346                 res = 0;
2347
2348         marshal_free (arr);
2349
2350         return res;
2351 }
2352
2353 typedef int (STDCALL *ByrefStringDelegate) (char **s);
2354
2355 LIBTEST_API int STDCALL 
2356 mono_test_marshal_byref_string_delegate (ByrefStringDelegate d)
2357 {
2358         char *s = (char*)"ABC";
2359         int res;
2360
2361         res = d (&s);
2362         if (res != 0)
2363                 return res;
2364
2365         if (!strcmp (s, "DEF"))
2366                 res = 0;
2367         else
2368                 res = 2;
2369
2370         marshal_free (s);
2371
2372         return res;
2373 }
2374
2375 LIBTEST_API int STDCALL 
2376 add_delegate (int i, int j)
2377 {
2378         return i + j;
2379 }
2380
2381 LIBTEST_API gpointer STDCALL 
2382 mono_test_marshal_return_fnptr (void)
2383 {
2384         return &add_delegate;
2385 }
2386
2387 LIBTEST_API int STDCALL 
2388 mono_xr (int code)
2389 {
2390         printf ("codigo %x\n", code);
2391         return code + 1234;
2392 }
2393
2394 typedef struct {
2395         int handle;
2396 } HandleRef;
2397
2398 LIBTEST_API HandleRef STDCALL 
2399 mono_xr_as_handle (int code)
2400 {
2401         HandleRef ref;
2402
2403         memset (&ref, 0, sizeof (ref));
2404
2405         return ref;
2406 }
2407  
2408 typedef struct {
2409         int   a;
2410         void *handle1;
2411         void *handle2;
2412         int   b;
2413 } HandleStructs;
2414
2415 LIBTEST_API int STDCALL 
2416 mono_safe_handle_struct_ref (HandleStructs *x)
2417 {
2418         printf ("Dingus Ref! \n");
2419         printf ("Values: %d %d %p %p\n", x->a, x->b, x->handle1, x->handle2);
2420         if (x->a != 1234)
2421                 return 1;
2422         if (x->b != 8743)
2423                 return 2;
2424
2425         if (x->handle1 != (void*) 0x7080feed)
2426                 return 3;
2427
2428         if (x->handle2 != (void*) 0x1234abcd)
2429                 return 4;
2430
2431         return 0xf00d;
2432 }
2433
2434 LIBTEST_API int STDCALL 
2435 mono_safe_handle_struct (HandleStructs x)
2436 {
2437         printf ("Dingus Standard! \n");
2438         printf ("Values: %d %d %p %p\n", x.a, x.b, x.handle1, x.handle2);
2439         if (x.a != 1234)
2440                 return 1;
2441         if (x.b != 8743)
2442                 return 2;
2443
2444         if (x.handle1 != (void*) 0x7080feed)
2445                 return 3;
2446
2447         if (x.handle2 != (void*) 0x1234abcd)
2448                 return 4;
2449         
2450         return 0xf00f;
2451 }
2452
2453 typedef struct {
2454         void *a;
2455 } TrivialHandle;
2456
2457 LIBTEST_API int STDCALL 
2458 mono_safe_handle_struct_simple (TrivialHandle x)
2459 {
2460         printf ("The value is %p\n", x.a);
2461         return ((int)(gsize)x.a) * 2;
2462 }
2463
2464 LIBTEST_API int STDCALL 
2465 mono_safe_handle_return (void)
2466 {
2467         return 0x1000f00d;
2468 }
2469
2470 LIBTEST_API void STDCALL
2471 mono_safe_handle_ref (void **handle)
2472 {
2473         if (*handle != 0){
2474                 *handle = (void *) 0xbad;
2475                 return;
2476         }
2477
2478         *handle = (void *) 0x800d;
2479 }
2480
2481 LIBTEST_API double STDCALL
2482 mono_test_marshal_date_time (double d, double *d2)
2483 {
2484         *d2 = d;
2485         return d;
2486 }
2487
2488 /*
2489  * COM INTEROP TESTS
2490  */
2491
2492 #ifndef WIN32
2493
2494 typedef struct {
2495         guint16 vt;
2496         guint16 wReserved1;
2497         guint16 wReserved2;
2498         guint16 wReserved3;
2499         union {
2500                 gint64 llVal;
2501                 gint32 lVal;
2502                 guint8  bVal;
2503                 gint16 iVal;
2504                 float  fltVal;
2505                 double dblVal;
2506                 gint16 boolVal;
2507                 gunichar2* bstrVal;
2508                 gint8 cVal;
2509                 guint16 uiVal;
2510                 guint32 ulVal;
2511                 guint64 ullVal;
2512                 gpointer byref;
2513                 struct {
2514                         gpointer pvRecord;
2515                         gpointer pRecInfo;
2516                 };
2517         };
2518 } VARIANT;
2519
2520 typedef enum {
2521         VARIANT_TRUE = -1,
2522         VARIANT_FALSE = 0
2523 } VariantBool;
2524
2525 typedef enum {
2526         VT_EMPTY = 0,
2527         VT_NULL = 1,
2528         VT_I2 = 2,
2529         VT_I4 = 3,
2530         VT_R4 = 4,
2531         VT_R8 = 5,
2532         VT_CY = 6,
2533         VT_DATE = 7,
2534         VT_BSTR = 8,
2535         VT_DISPATCH = 9,
2536         VT_ERROR = 10,
2537         VT_BOOL = 11,
2538         VT_VARIANT = 12,
2539         VT_UNKNOWN = 13,
2540         VT_DECIMAL = 14,
2541         VT_I1 = 16,
2542         VT_UI1 = 17,
2543         VT_UI2 = 18,
2544         VT_UI4 = 19,
2545         VT_I8 = 20,
2546         VT_UI8 = 21,
2547         VT_INT = 22,
2548         VT_UINT = 23,
2549         VT_VOID = 24,
2550         VT_HRESULT = 25,
2551         VT_PTR = 26,
2552         VT_SAFEARRAY = 27,
2553         VT_CARRAY = 28,
2554         VT_USERDEFINED = 29,
2555         VT_LPSTR = 30,
2556         VT_LPWSTR = 31,
2557         VT_RECORD = 36,
2558         VT_FILETIME = 64,
2559         VT_BLOB = 65,
2560         VT_STREAM = 66,
2561         VT_STORAGE = 67,
2562         VT_STREAMED_OBJECT = 68,
2563         VT_STORED_OBJECT = 69,
2564         VT_BLOB_OBJECT = 70,
2565         VT_CF = 71,
2566         VT_CLSID = 72,
2567         VT_VECTOR = 4096,
2568         VT_ARRAY = 8192,
2569         VT_BYREF = 16384
2570 } VarEnum;
2571
2572 void VariantInit(VARIANT* vt)
2573 {
2574         vt->vt = VT_EMPTY;
2575 }
2576
2577 typedef struct
2578 {
2579         guint32 a;
2580         guint16 b;
2581         guint16 c;
2582         guint8 d[8];
2583 } GUID;
2584
2585 #define S_OK 0
2586
2587 #endif
2588
2589 LIBTEST_API int STDCALL 
2590 mono_test_marshal_bstr_in(gunichar2* bstr)
2591 {
2592         gint32 result = 0;
2593         gchar* bstr_utf8 = g_utf16_to_utf8 (bstr, -1, NULL, NULL, NULL);
2594         result = strcmp("mono_test_marshal_bstr_in", bstr_utf8);
2595         g_free(bstr_utf8);
2596         if (result == 0)
2597                 return 0;
2598         return 1;
2599 }
2600
2601 LIBTEST_API int STDCALL 
2602 mono_test_marshal_bstr_out(gunichar2** bstr)
2603 {
2604         *bstr = marshal_bstr_alloc ("mono_test_marshal_bstr_out");
2605         return 0;
2606 }
2607
2608 LIBTEST_API int STDCALL 
2609 mono_test_marshal_bstr_in_null(gunichar2* bstr)
2610 {
2611         if (!bstr)
2612                 return 0;
2613         return 1;
2614 }
2615
2616 LIBTEST_API int STDCALL 
2617 mono_test_marshal_bstr_out_null(gunichar2** bstr)
2618 {
2619         *bstr = NULL;
2620         return 0;
2621 }
2622
2623 LIBTEST_API int STDCALL 
2624 mono_test_marshal_variant_in_sbyte(VARIANT variant)
2625 {
2626         if (variant.vt == VT_I1 && variant.cVal == 100)
2627                 return 0;
2628         return 1;
2629 }
2630
2631 LIBTEST_API int STDCALL 
2632 mono_test_marshal_variant_in_byte(VARIANT variant)
2633 {
2634         if (variant.vt == VT_UI1 && variant.bVal == 100)
2635                 return 0;
2636         return 1;
2637 }
2638
2639 LIBTEST_API int STDCALL 
2640 mono_test_marshal_variant_in_short(VARIANT variant)
2641 {
2642         if (variant.vt == VT_I2 && variant.iVal == 314)
2643                 return 0;
2644         return 1;
2645 }
2646
2647 LIBTEST_API int STDCALL 
2648 mono_test_marshal_variant_in_ushort(VARIANT variant)
2649 {
2650         if (variant.vt == VT_UI2 && variant.uiVal == 314)
2651                 return 0;
2652         return 1;
2653 }
2654
2655 LIBTEST_API int STDCALL 
2656 mono_test_marshal_variant_in_int(VARIANT variant)
2657 {
2658         if (variant.vt == VT_I4 && variant.lVal == 314)
2659                 return 0;
2660         return 1;
2661 }
2662
2663 LIBTEST_API int STDCALL 
2664 mono_test_marshal_variant_in_uint(VARIANT variant)
2665 {
2666         if (variant.vt == VT_UI4 && variant.ulVal == 314)
2667                 return 0;
2668         return 1;
2669 }
2670
2671 LIBTEST_API int STDCALL 
2672 mono_test_marshal_variant_in_long(VARIANT variant)
2673 {
2674         if (variant.vt == VT_I8 && variant.llVal == 314)
2675                 return 0;
2676         return 1;
2677 }
2678
2679 LIBTEST_API int STDCALL 
2680 mono_test_marshal_variant_in_ulong(VARIANT variant)
2681 {
2682         if (variant.vt == VT_UI8 && variant.ullVal == 314)
2683                 return 0;
2684         return 1;
2685 }
2686
2687 LIBTEST_API int STDCALL 
2688 mono_test_marshal_variant_in_float(VARIANT variant)
2689 {
2690         if (variant.vt == VT_R4 && (variant.fltVal - 3.14)/3.14 < .001)
2691                 return 0;
2692         return 1;
2693 }
2694
2695 LIBTEST_API int STDCALL 
2696 mono_test_marshal_variant_in_double(VARIANT variant)
2697 {
2698         if (variant.vt == VT_R8 && (variant.dblVal - 3.14)/3.14 < .001)
2699                 return 0;
2700         return 1;
2701 }
2702
2703 LIBTEST_API int STDCALL 
2704 mono_test_marshal_variant_in_bstr(VARIANT variant)
2705 {
2706         gint32 result = 0;
2707         gchar* bstr_utf8 = g_utf16_to_utf8 (variant.bstrVal, -1, NULL, NULL, NULL);
2708         result = strcmp("PI", bstr_utf8);
2709         g_free(bstr_utf8);
2710
2711         if (variant.vt == VT_BSTR && !result)
2712                 return 0;
2713         return 1;
2714 }
2715
2716 LIBTEST_API int STDCALL 
2717 mono_test_marshal_variant_in_bool_true (VARIANT variant)
2718 {
2719         if (variant.vt == VT_BOOL && variant.boolVal == VARIANT_TRUE)
2720                 return 0;
2721         return 1;
2722 }
2723
2724 LIBTEST_API int STDCALL 
2725 mono_test_marshal_variant_in_bool_false (VARIANT variant)
2726 {
2727         if (variant.vt == VT_BOOL && variant.boolVal == VARIANT_FALSE)
2728                 return 0;
2729         return 1;
2730 }
2731
2732 LIBTEST_API int STDCALL 
2733 mono_test_marshal_variant_out_sbyte(VARIANT* variant)
2734 {
2735         variant->vt = VT_I1;
2736         variant->cVal = 100;
2737
2738         return 0;
2739 }
2740
2741 LIBTEST_API int STDCALL 
2742 mono_test_marshal_variant_out_sbyte_byref(VARIANT* variant)
2743 {
2744         variant->vt = VT_I1|VT_BYREF;
2745         variant->byref = marshal_alloc(1);
2746         *((gint8*)variant->byref) = 100;
2747
2748         return 0;
2749 }
2750
2751 LIBTEST_API int STDCALL 
2752 mono_test_marshal_variant_out_byte(VARIANT* variant)
2753 {       
2754         variant->vt = VT_UI1;
2755         variant->bVal = 100;
2756
2757         return 0;
2758 }
2759
2760 LIBTEST_API int STDCALL 
2761 mono_test_marshal_variant_out_byte_byref(VARIANT* variant)
2762 {       
2763         variant->vt = VT_UI1|VT_BYREF;
2764         variant->byref = marshal_alloc(1);
2765         *((gint8*)variant->byref) = 100;
2766
2767         return 0;
2768 }
2769
2770 LIBTEST_API int STDCALL 
2771 mono_test_marshal_variant_out_short(VARIANT* variant)
2772 {
2773         variant->vt = VT_I2;
2774         variant->iVal = 314;
2775
2776         return 0;
2777 }
2778
2779 LIBTEST_API int STDCALL 
2780 mono_test_marshal_variant_out_short_byref(VARIANT* variant)
2781 {
2782         variant->vt = VT_I2|VT_BYREF;
2783         variant->byref = marshal_alloc(2);
2784         *((gint16*)variant->byref) = 314;
2785
2786         return 0;
2787 }
2788
2789 LIBTEST_API int STDCALL 
2790 mono_test_marshal_variant_out_ushort(VARIANT* variant)
2791 {
2792         variant->vt = VT_UI2;
2793         variant->uiVal = 314;
2794
2795         return 0;
2796 }
2797
2798 LIBTEST_API int STDCALL 
2799 mono_test_marshal_variant_out_ushort_byref(VARIANT* variant)
2800 {
2801         variant->vt = VT_UI2|VT_BYREF;
2802         variant->byref = marshal_alloc(2);
2803         *((guint16*)variant->byref) = 314;
2804
2805         return 0;
2806 }
2807
2808 LIBTEST_API int STDCALL 
2809 mono_test_marshal_variant_out_int(VARIANT* variant)
2810 {
2811         variant->vt = VT_I4;
2812         variant->lVal = 314;
2813
2814         return 0;
2815 }
2816
2817 LIBTEST_API int STDCALL 
2818 mono_test_marshal_variant_out_int_byref(VARIANT* variant)
2819 {
2820         variant->vt = VT_I4|VT_BYREF;
2821         variant->byref = marshal_alloc(4);
2822         *((gint32*)variant->byref) = 314;
2823
2824         return 0;
2825 }
2826
2827 LIBTEST_API int STDCALL 
2828 mono_test_marshal_variant_out_uint(VARIANT* variant)
2829 {
2830         variant->vt = VT_UI4;
2831         variant->ulVal = 314;
2832
2833         return 0;
2834 }
2835
2836 LIBTEST_API int STDCALL 
2837 mono_test_marshal_variant_out_uint_byref(VARIANT* variant)
2838 {
2839         variant->vt = VT_UI4|VT_BYREF;
2840         variant->byref = marshal_alloc(4);
2841         *((guint32*)variant->byref) = 314;
2842
2843         return 0;
2844 }
2845
2846 LIBTEST_API int STDCALL 
2847 mono_test_marshal_variant_out_long(VARIANT* variant)
2848 {
2849         variant->vt = VT_I8;
2850         variant->llVal = 314;
2851
2852         return 0;
2853 }
2854
2855 LIBTEST_API int STDCALL 
2856 mono_test_marshal_variant_out_long_byref(VARIANT* variant)
2857 {
2858         variant->vt = VT_I8|VT_BYREF;
2859         variant->byref = marshal_alloc(8);
2860         *((gint64*)variant->byref) = 314;
2861
2862         return 0;
2863 }
2864
2865 LIBTEST_API int STDCALL 
2866 mono_test_marshal_variant_out_ulong(VARIANT* variant)
2867 {
2868         variant->vt = VT_UI8;
2869         variant->ullVal = 314;
2870
2871         return 0;
2872 }
2873
2874 LIBTEST_API int STDCALL 
2875 mono_test_marshal_variant_out_ulong_byref(VARIANT* variant)
2876 {
2877         variant->vt = VT_UI8|VT_BYREF;
2878         variant->byref = marshal_alloc(8);
2879         *((guint64*)variant->byref) = 314;
2880
2881         return 0;
2882 }
2883
2884 LIBTEST_API int STDCALL 
2885 mono_test_marshal_variant_out_float(VARIANT* variant)
2886 {
2887         variant->vt = VT_R4;
2888         variant->fltVal = 3.14;
2889
2890         return 0;
2891 }
2892
2893 LIBTEST_API int STDCALL 
2894 mono_test_marshal_variant_out_float_byref(VARIANT* variant)
2895 {
2896         variant->vt = VT_R4|VT_BYREF;
2897         variant->byref = marshal_alloc(4);
2898         *((float*)variant->byref) = 3.14;
2899
2900         return 0;
2901 }
2902
2903 LIBTEST_API int STDCALL 
2904 mono_test_marshal_variant_out_double(VARIANT* variant)
2905 {
2906         variant->vt = VT_R8;
2907         variant->dblVal = 3.14;
2908
2909         return 0;
2910 }
2911
2912 LIBTEST_API int STDCALL 
2913 mono_test_marshal_variant_out_double_byref(VARIANT* variant)
2914 {
2915         variant->vt = VT_R8|VT_BYREF;
2916         variant->byref = marshal_alloc(8);
2917         *((double*)variant->byref) = 3.14;
2918
2919         return 0;
2920 }
2921
2922 LIBTEST_API int STDCALL 
2923 mono_test_marshal_variant_out_bstr(VARIANT* variant)
2924 {
2925         variant->vt = VT_BSTR;
2926         variant->bstrVal = marshal_bstr_alloc("PI");
2927
2928         return 0;
2929 }
2930
2931 LIBTEST_API int STDCALL 
2932 mono_test_marshal_variant_out_bstr_byref(VARIANT* variant)
2933 {
2934         variant->vt = VT_BSTR|VT_BYREF;
2935         variant->byref = marshal_alloc(sizeof(gpointer));
2936         *((gunichar**)variant->byref) = (gunichar*)marshal_bstr_alloc("PI");
2937
2938         return 0;
2939 }
2940
2941 LIBTEST_API int STDCALL 
2942 mono_test_marshal_variant_out_bool_true (VARIANT* variant)
2943 {
2944         variant->vt = VT_BOOL;
2945         variant->boolVal = VARIANT_TRUE;
2946
2947         return 0;
2948 }
2949
2950 LIBTEST_API int STDCALL 
2951 mono_test_marshal_variant_out_bool_true_byref (VARIANT* variant)
2952 {
2953         variant->vt = VT_BOOL|VT_BYREF;
2954         variant->byref = marshal_alloc(2);
2955         *((gint16*)variant->byref) = VARIANT_TRUE;
2956
2957         return 0;
2958 }
2959
2960 LIBTEST_API int STDCALL 
2961 mono_test_marshal_variant_out_bool_false (VARIANT* variant)
2962 {
2963         variant->vt = VT_BOOL;
2964         variant->boolVal = VARIANT_FALSE;
2965
2966         return 0;
2967 }
2968
2969 LIBTEST_API int STDCALL 
2970 mono_test_marshal_variant_out_bool_false_byref (VARIANT* variant)
2971 {
2972         variant->vt = VT_BOOL|VT_BYREF;
2973         variant->byref = marshal_alloc(2);
2974         *((gint16*)variant->byref) = VARIANT_FALSE;
2975
2976         return 0;
2977 }
2978
2979 typedef int (STDCALL *VarFunc) (int vt, VARIANT variant);
2980 typedef int (STDCALL *VarRefFunc) (int vt, VARIANT* variant);
2981
2982 LIBTEST_API int STDCALL 
2983 mono_test_marshal_variant_in_sbyte_unmanaged(VarFunc func)
2984 {
2985         VARIANT vt;
2986         vt.vt = VT_I1;
2987         vt.cVal = -100;
2988         return func (VT_I1, vt);
2989 }
2990
2991 LIBTEST_API int STDCALL 
2992 mono_test_marshal_variant_in_byte_unmanaged(VarFunc func)
2993 {
2994         VARIANT vt;
2995         vt.vt = VT_UI1;
2996         vt.bVal = 100;
2997         return func (VT_UI1, vt);
2998 }
2999
3000 LIBTEST_API int STDCALL 
3001 mono_test_marshal_variant_in_short_unmanaged(VarFunc func)
3002 {
3003         VARIANT vt;
3004         vt.vt = VT_I2;
3005         vt.iVal = -100;
3006         return func (VT_I2, vt);
3007 }
3008
3009 LIBTEST_API int STDCALL 
3010 mono_test_marshal_variant_in_ushort_unmanaged(VarFunc func)
3011 {
3012         VARIANT vt;
3013         vt.vt = VT_UI2;
3014         vt.uiVal = 100;
3015         return func (VT_UI2, vt);
3016 }
3017
3018 LIBTEST_API int STDCALL 
3019 mono_test_marshal_variant_in_int_unmanaged(VarFunc func)
3020 {
3021         VARIANT vt;
3022         vt.vt = VT_I4;
3023         vt.lVal = -100;
3024         return func (VT_I4, vt);
3025 }
3026
3027 LIBTEST_API int STDCALL 
3028 mono_test_marshal_variant_in_uint_unmanaged(VarFunc func)
3029 {
3030         VARIANT vt;
3031         vt.vt = VT_UI4;
3032         vt.ulVal = 100;
3033         return func (VT_UI4, vt);
3034 }
3035
3036 LIBTEST_API int STDCALL 
3037 mono_test_marshal_variant_in_long_unmanaged(VarFunc func)
3038 {
3039         VARIANT vt;
3040         vt.vt = VT_I8;
3041         vt.llVal = -100;
3042         return func (VT_I8, vt);
3043 }
3044
3045 LIBTEST_API int STDCALL 
3046 mono_test_marshal_variant_in_ulong_unmanaged(VarFunc func)
3047 {
3048         VARIANT vt;
3049         vt.vt = VT_UI8;
3050         vt.ullVal = 100;
3051         return func (VT_UI8, vt);
3052 }
3053
3054 LIBTEST_API int STDCALL 
3055 mono_test_marshal_variant_in_float_unmanaged(VarFunc func)
3056 {
3057         VARIANT vt;
3058         vt.vt = VT_R4;
3059         vt.fltVal = 3.14;
3060         return func (VT_R4, vt);
3061 }
3062
3063 LIBTEST_API int STDCALL 
3064 mono_test_marshal_variant_in_double_unmanaged(VarFunc func)
3065 {
3066         VARIANT vt;
3067         vt.vt = VT_R8;
3068         vt.dblVal = 3.14;
3069         return func (VT_R8, vt);
3070 }
3071
3072 LIBTEST_API int STDCALL 
3073 mono_test_marshal_variant_in_bstr_unmanaged(VarFunc func)
3074 {
3075         VARIANT vt;
3076         vt.vt = VT_BSTR;
3077         vt.bstrVal = marshal_bstr_alloc("PI");
3078         return func (VT_BSTR, vt);
3079 }
3080
3081 LIBTEST_API int STDCALL 
3082 mono_test_marshal_variant_in_bool_true_unmanaged(VarFunc func)
3083 {
3084         VARIANT vt;
3085         vt.vt = VT_BOOL;
3086         vt.boolVal = VARIANT_TRUE;
3087         return func (VT_BOOL, vt);
3088 }
3089
3090 LIBTEST_API int STDCALL 
3091 mono_test_marshal_variant_in_bool_false_unmanaged(VarFunc func)
3092 {
3093         VARIANT vt;
3094         vt.vt = VT_BOOL;
3095         vt.boolVal = VARIANT_FALSE;
3096         return func (VT_BOOL, vt);
3097 }
3098
3099 LIBTEST_API int STDCALL 
3100 mono_test_marshal_variant_out_sbyte_unmanaged(VarRefFunc func)
3101 {
3102         VARIANT vt;
3103         VariantInit (&vt);
3104         func (VT_I1, &vt);
3105         if (vt.vt == VT_I1 && vt.cVal == -100)
3106                 return 0;
3107         return 1;
3108 }
3109
3110 LIBTEST_API int STDCALL 
3111 mono_test_marshal_variant_out_byte_unmanaged(VarRefFunc func)
3112 {
3113         VARIANT vt;
3114         VariantInit (&vt);
3115         func (VT_UI1, &vt);
3116         if (vt.vt == VT_UI1 && vt.bVal == 100)
3117                 return 0;
3118         return 1;
3119 }
3120
3121 LIBTEST_API int STDCALL 
3122 mono_test_marshal_variant_out_short_unmanaged(VarRefFunc func)
3123 {
3124         VARIANT vt;
3125         VariantInit (&vt);
3126         func (VT_I2, &vt);
3127         if (vt.vt == VT_I2 && vt.iVal == -100)
3128                 return 0;
3129         return 1;
3130 }
3131
3132 LIBTEST_API int STDCALL 
3133 mono_test_marshal_variant_out_ushort_unmanaged(VarRefFunc func)
3134 {
3135         VARIANT vt;
3136         VariantInit (&vt);
3137         func (VT_UI2, &vt);
3138         if (vt.vt == VT_UI2 && vt.uiVal == 100)
3139                 return 0;
3140         return 1;
3141 }
3142
3143 LIBTEST_API int STDCALL 
3144 mono_test_marshal_variant_out_int_unmanaged(VarRefFunc func)
3145 {
3146         VARIANT vt;
3147         VariantInit (&vt);
3148         func (VT_I4, &vt);
3149         if (vt.vt == VT_I4 && vt.lVal == -100)
3150                 return 0;
3151         return 1;
3152 }
3153
3154 LIBTEST_API int STDCALL 
3155 mono_test_marshal_variant_out_uint_unmanaged(VarRefFunc func)
3156 {
3157         VARIANT vt;
3158         VariantInit (&vt);
3159         func (VT_UI4, &vt);
3160         if (vt.vt == VT_UI4 && vt.ulVal == 100)
3161                 return 0;
3162         return 1;
3163 }
3164
3165 LIBTEST_API int STDCALL 
3166 mono_test_marshal_variant_out_long_unmanaged(VarRefFunc func)
3167 {
3168         VARIANT vt;
3169         VariantInit (&vt);
3170         func (VT_I8, &vt);
3171         if (vt.vt == VT_I8 && vt.llVal == -100)
3172                 return 0;
3173         return 1;
3174 }
3175
3176 LIBTEST_API int STDCALL 
3177 mono_test_marshal_variant_out_ulong_unmanaged(VarRefFunc func)
3178 {
3179         VARIANT vt;
3180         VariantInit (&vt);
3181         func (VT_UI8, &vt);
3182         if (vt.vt == VT_UI8 && vt.ullVal == 100)
3183                 return 0;
3184         return 1;
3185 }
3186
3187 LIBTEST_API int STDCALL 
3188 mono_test_marshal_variant_out_float_unmanaged(VarRefFunc func)
3189 {
3190         VARIANT vt;
3191         VariantInit (&vt);
3192         func (VT_R4, &vt);
3193         if (vt.vt == VT_R4 && fabs (vt.fltVal - 3.14f) < 1e-10)
3194                 return 0;
3195         return 1;
3196 }
3197
3198 LIBTEST_API int STDCALL 
3199 mono_test_marshal_variant_out_double_unmanaged(VarRefFunc func)
3200 {
3201         VARIANT vt;
3202         VariantInit (&vt);
3203         func (VT_R8, &vt);
3204         if (vt.vt == VT_R8 && fabs (vt.dblVal - 3.14) < 1e-10)
3205                 return 0;
3206         return 1;
3207 }
3208
3209 LIBTEST_API int STDCALL 
3210 mono_test_marshal_variant_out_bstr_unmanaged(VarRefFunc func)
3211 {
3212         VARIANT vt;
3213         gchar* bstr_utf8;
3214         gint32 result = 0;
3215
3216
3217         VariantInit (&vt);
3218         func (VT_BSTR, &vt);
3219         bstr_utf8 = g_utf16_to_utf8 (vt.bstrVal, -1, NULL, NULL, NULL);
3220         result = strcmp("PI", bstr_utf8);
3221         g_free(bstr_utf8);
3222         if (vt.vt == VT_BSTR && !result)
3223                 return 0;
3224         return 1;
3225 }
3226
3227 LIBTEST_API int STDCALL 
3228 mono_test_marshal_variant_out_bool_true_unmanaged(VarRefFunc func)
3229 {
3230         VARIANT vt;
3231         VariantInit (&vt);
3232         func (VT_BOOL, &vt);
3233         if (vt.vt == VT_BOOL && vt.boolVal == VARIANT_TRUE)
3234                 return 0;
3235         return 1;
3236 }
3237
3238 LIBTEST_API int STDCALL 
3239 mono_test_marshal_variant_out_bool_false_unmanaged(VarRefFunc func)
3240 {
3241         VARIANT vt;
3242         VariantInit (&vt);
3243         func (VT_BOOL, &vt);
3244         if (vt.vt == VT_BOOL && vt.boolVal == VARIANT_TRUE)
3245                 return 0;
3246         return 1;
3247 }
3248
3249 typedef struct MonoComObject MonoComObject;
3250
3251 typedef struct
3252 {
3253         int (STDCALL *QueryInterface)(MonoComObject* pUnk, gpointer riid, gpointer* ppv);
3254         int (STDCALL *AddRef)(MonoComObject* pUnk);
3255         int (STDCALL *Release)(MonoComObject* pUnk);
3256         int (STDCALL *get_ITest)(MonoComObject* pUnk, MonoComObject* *ppUnk);
3257         int (STDCALL *SByteIn)(MonoComObject* pUnk, char a);
3258         int (STDCALL *ByteIn)(MonoComObject* pUnk, unsigned char a);
3259         int (STDCALL *ShortIn)(MonoComObject* pUnk, short a);
3260         int (STDCALL *UShortIn)(MonoComObject* pUnk, unsigned short a);
3261         int (STDCALL *IntIn)(MonoComObject* pUnk, int a);
3262         int (STDCALL *UIntIn)(MonoComObject* pUnk, unsigned int a);
3263         int (STDCALL *LongIn)(MonoComObject* pUnk, gint64 a);
3264         int (STDCALL *ULongIn)(MonoComObject* pUnk, guint64 a);
3265         int (STDCALL *FloatIn)(MonoComObject* pUnk, float a);
3266         int (STDCALL *DoubleIn)(MonoComObject* pUnk, double a);
3267         int (STDCALL *ITestIn)(MonoComObject* pUnk, MonoComObject* pUnk2);
3268         int (STDCALL *ITestOut)(MonoComObject* pUnk, MonoComObject* *ppUnk);
3269 } MonoIUnknown;
3270
3271 struct MonoComObject
3272 {
3273         MonoIUnknown* vtbl;
3274         int m_ref;
3275 };
3276
3277 static GUID IID_ITest = {0, 0, 0, {0,0,0,0,0,0,0,1}};
3278 static GUID IID_IMonoUnknown = {0, 0, 0, {0xc0,0,0,0,0,0,0,0x46}};
3279 static GUID IID_IMonoDispatch = {0x00020400, 0, 0, {0xc0,0,0,0,0,0,0,0x46}};
3280
3281 LIBTEST_API int STDCALL
3282 MonoQueryInterface(MonoComObject* pUnk, gpointer riid, gpointer* ppv)
3283 {
3284
3285         *ppv = NULL;
3286         if (!memcmp(riid, &IID_IMonoUnknown, sizeof(GUID))) {
3287                 *ppv = pUnk;
3288                 return S_OK;
3289         }
3290         else if (!memcmp(riid, &IID_ITest, sizeof(GUID))) {
3291                 *ppv = pUnk;
3292                 return S_OK;
3293         }
3294         else if (!memcmp(riid, &IID_IMonoDispatch, sizeof(GUID))) {
3295                 *ppv = pUnk;
3296                 return S_OK;
3297         }
3298         return 0x80004002; //E_NOINTERFACE;
3299 }
3300
3301 LIBTEST_API int STDCALL 
3302 MonoAddRef(MonoComObject* pUnk)
3303 {
3304         return ++(pUnk->m_ref);
3305 }
3306
3307 LIBTEST_API int STDCALL 
3308 MonoRelease(MonoComObject* pUnk)
3309 {
3310         return --(pUnk->m_ref);
3311 }
3312
3313 LIBTEST_API int STDCALL 
3314 SByteIn(MonoComObject* pUnk, char a)
3315 {
3316         return S_OK;
3317 }
3318
3319 LIBTEST_API int STDCALL 
3320 ByteIn(MonoComObject* pUnk, unsigned char a)
3321 {
3322         return S_OK;
3323 }
3324
3325 LIBTEST_API int STDCALL 
3326 ShortIn(MonoComObject* pUnk, short a)
3327 {
3328         return S_OK;
3329 }
3330
3331 LIBTEST_API int STDCALL 
3332 UShortIn(MonoComObject* pUnk, unsigned short a)
3333 {
3334         return S_OK;
3335 }
3336
3337 LIBTEST_API int STDCALL 
3338 IntIn(MonoComObject* pUnk, int a)
3339 {
3340         return S_OK;
3341 }
3342
3343 LIBTEST_API int STDCALL 
3344 UIntIn(MonoComObject* pUnk, unsigned int a)
3345 {
3346         return S_OK;
3347 }
3348
3349 LIBTEST_API int STDCALL 
3350 LongIn(MonoComObject* pUnk, gint64 a)
3351 {
3352         return S_OK;
3353 }
3354
3355 LIBTEST_API int STDCALL 
3356 ULongIn(MonoComObject* pUnk, guint64 a)
3357 {
3358         return S_OK;
3359 }
3360
3361 LIBTEST_API int STDCALL 
3362 FloatIn(MonoComObject* pUnk, float a)
3363 {
3364         return S_OK;
3365 }
3366
3367 LIBTEST_API int STDCALL 
3368 DoubleIn(MonoComObject* pUnk, double a)
3369 {
3370         return S_OK;
3371 }
3372
3373 LIBTEST_API int STDCALL 
3374 ITestIn(MonoComObject* pUnk, MonoComObject *pUnk2)
3375 {
3376         return S_OK;
3377 }
3378
3379 LIBTEST_API int STDCALL 
3380 ITestOut(MonoComObject* pUnk, MonoComObject* *ppUnk)
3381 {
3382         return S_OK;
3383 }
3384
3385 static void create_com_object (MonoComObject** pOut);
3386
3387 LIBTEST_API int STDCALL 
3388 get_ITest(MonoComObject* pUnk, MonoComObject* *ppUnk)
3389 {
3390         create_com_object (ppUnk);
3391         return S_OK;
3392 }
3393
3394 static void create_com_object (MonoComObject** pOut)
3395 {
3396         *pOut = marshal_new0 (MonoComObject, 1);
3397         (*pOut)->vtbl = marshal_new0 (MonoIUnknown, 1);
3398
3399         (*pOut)->m_ref = 1;
3400         (*pOut)->vtbl->QueryInterface = MonoQueryInterface;
3401         (*pOut)->vtbl->AddRef = MonoAddRef;
3402         (*pOut)->vtbl->Release = MonoRelease;
3403         (*pOut)->vtbl->SByteIn = SByteIn;
3404         (*pOut)->vtbl->ByteIn = ByteIn;
3405         (*pOut)->vtbl->ShortIn = ShortIn;
3406         (*pOut)->vtbl->UShortIn = UShortIn;
3407         (*pOut)->vtbl->IntIn = IntIn;
3408         (*pOut)->vtbl->UIntIn = UIntIn;
3409         (*pOut)->vtbl->LongIn = LongIn;
3410         (*pOut)->vtbl->ULongIn = ULongIn;
3411         (*pOut)->vtbl->FloatIn = FloatIn;
3412         (*pOut)->vtbl->DoubleIn = DoubleIn;
3413         (*pOut)->vtbl->ITestIn = ITestIn;
3414         (*pOut)->vtbl->ITestOut = ITestOut;
3415         (*pOut)->vtbl->get_ITest = get_ITest;
3416 }
3417
3418 static MonoComObject* same_object = NULL;
3419
3420 LIBTEST_API int STDCALL 
3421 mono_test_marshal_com_object_create(MonoComObject* *pUnk)
3422 {
3423         create_com_object (pUnk);
3424
3425         if (!same_object)
3426                 same_object = *pUnk;
3427
3428         return 0;
3429 }
3430
3431 LIBTEST_API int STDCALL 
3432 mono_test_marshal_com_object_same(MonoComObject* *pUnk)
3433 {
3434         *pUnk = same_object;
3435
3436         return 0;
3437 }
3438
3439 LIBTEST_API int STDCALL 
3440 mono_test_marshal_com_object_destroy(MonoComObject *pUnk)
3441 {
3442         int ref = --(pUnk->m_ref);
3443         g_free(pUnk->vtbl);
3444         g_free(pUnk);
3445
3446         return ref;
3447 }
3448
3449 LIBTEST_API int STDCALL 
3450 mono_test_marshal_com_object_ref_count(MonoComObject *pUnk)
3451 {
3452         return pUnk->m_ref;
3453 }
3454
3455 LIBTEST_API int STDCALL 
3456 mono_test_marshal_ccw_itest (MonoComObject *pUnk)
3457 {
3458         int hr = 0;
3459         MonoComObject* pTest;
3460
3461         if (!pUnk)
3462                 return 1;
3463
3464         hr = pUnk->vtbl->SByteIn (pUnk, -100);
3465         if (hr != 0)
3466                 return 2;
3467         hr = pUnk->vtbl->ByteIn (pUnk, 100);
3468         if (hr != 0)
3469                 return 3;
3470         hr = pUnk->vtbl->ShortIn (pUnk, -100);
3471         if (hr != 0)
3472                 return 4;
3473         hr = pUnk->vtbl->UShortIn (pUnk, 100);
3474         if (hr != 0)
3475                 return 5;
3476         hr = pUnk->vtbl->IntIn (pUnk, -100);
3477         if (hr != 0)
3478                 return 6;
3479         hr = pUnk->vtbl->UIntIn (pUnk, 100);
3480         if (hr != 0)
3481                 return 7;
3482         hr = pUnk->vtbl->LongIn (pUnk, -100);
3483         if (hr != 0)
3484                 return 8;
3485         hr = pUnk->vtbl->ULongIn (pUnk, 100);
3486         if (hr != 0)
3487                 return 9;
3488         hr = pUnk->vtbl->FloatIn (pUnk, 3.14f);
3489         if (hr != 0)
3490                 return 10;
3491         hr = pUnk->vtbl->DoubleIn (pUnk, 3.14);
3492         if (hr != 0)
3493                 return 11;
3494         hr = pUnk->vtbl->ITestIn (pUnk, pUnk);
3495         if (hr != 0)
3496                 return 12;
3497         hr = pUnk->vtbl->ITestOut (pUnk, &pTest);
3498         if (hr != 0)
3499                 return 13;
3500
3501         return 0;
3502 }
3503
3504 /*
3505  * mono_method_get_unmanaged_thunk tests
3506  */
3507
3508 #if defined(__GNUC__) && ((defined(__i386__) && (defined(__linux__) || defined (__APPLE__)) || defined (__FreeBSD__) || defined(__OpenBSD__)) || (defined(__ppc__) && defined(__APPLE__)))
3509 #define ALIGN(size) __attribute__ ((aligned(size)))
3510 #else
3511 #define ALIGN(size)
3512 #endif
3513
3514
3515 /* thunks.cs:TestStruct */
3516 typedef struct _TestStruct {
3517         int A;
3518         double B;
3519 } TestStruct;
3520
3521 /* Searches for mono symbols in all loaded modules */
3522 static gpointer
3523 lookup_mono_symbol (const char *symbol_name)
3524 {
3525         gpointer symbol;
3526         if (g_module_symbol (g_module_open (NULL, G_MODULE_BIND_LAZY), symbol_name, &symbol))
3527                 return symbol;
3528         else
3529                 return NULL;
3530 }
3531
3532 LIBTEST_API gpointer STDCALL
3533 mono_test_marshal_lookup_symbol (const char *symbol_name)
3534 {
3535         return lookup_mono_symbol (symbol_name);
3536 }
3537
3538 /**
3539  * test_method_thunk:
3540  *
3541  * @test_id: the test number
3542  * @test_method_handle: MonoMethod* of the C# test method
3543  * @create_object_method_handle: MonoMethod* of thunks.cs:Test.CreateObject
3544  */
3545 LIBTEST_API int STDCALL  
3546 test_method_thunk (int test_id, gpointer test_method_handle, gpointer create_object_method_handle)
3547 {
3548         gpointer (*mono_method_get_unmanaged_thunk)(gpointer)
3549                 = lookup_mono_symbol ("mono_method_get_unmanaged_thunk");
3550
3551         gpointer (*mono_string_new_wrapper)(const char *)
3552                 = lookup_mono_symbol ("mono_string_new_wrapper");
3553
3554         char* (*mono_string_to_utf8)(gpointer)
3555                 = lookup_mono_symbol ("mono_string_to_utf8");
3556
3557         gpointer (*mono_object_unbox)(gpointer)
3558                 = lookup_mono_symbol ("mono_object_unbox");
3559
3560         gpointer test_method, ex = NULL;
3561         gpointer (STDCALL *CreateObject)(gpointer*);
3562
3563         if (!mono_method_get_unmanaged_thunk)
3564                 return 1;
3565
3566         test_method =  mono_method_get_unmanaged_thunk (test_method_handle);
3567         if (!test_method)
3568                 return 2;
3569
3570         CreateObject = mono_method_get_unmanaged_thunk (create_object_method_handle);
3571         if (!CreateObject)
3572                 return 3;
3573
3574
3575         switch (test_id) {
3576
3577         case 0: {
3578                 /* thunks.cs:Test.Test0 */
3579                 void (STDCALL *F)(gpointer*) = test_method;
3580                 F (&ex);
3581                 break;
3582         }
3583
3584         case 1: {
3585                 /* thunks.cs:Test.Test1 */
3586                 int (STDCALL *F)(gpointer*) = test_method;
3587                 if (F (&ex) != 42)
3588                         return 4;
3589                 break;
3590         }
3591
3592         case 2: {
3593                 /* thunks.cs:Test.Test2 */
3594                 gpointer (STDCALL *F)(gpointer, gpointer*) = test_method;
3595                 gpointer str = mono_string_new_wrapper ("foo");
3596                 if (str != F (str, &ex))
3597                         return 4;
3598                 break;
3599         }
3600
3601         case 3: {
3602                 /* thunks.cs:Test.Test3 */
3603                 gpointer (STDCALL *F)(gpointer, gpointer, gpointer*);
3604                 gpointer obj;
3605                 gpointer str;
3606
3607                 F = test_method;
3608                 obj = CreateObject (&ex);
3609                 str = mono_string_new_wrapper ("bar");
3610
3611                 if (str != F (obj, str, &ex))
3612                         return 4;
3613                 break;
3614         }
3615
3616         case 4: {
3617                 /* thunks.cs:Test.Test4 */
3618                 int (STDCALL *F)(gpointer, gpointer, int, gpointer*);
3619                 gpointer obj;
3620                 gpointer str;
3621
3622                 F = test_method;
3623                 obj = CreateObject (&ex);
3624                 str = mono_string_new_wrapper ("bar");
3625
3626                 if (42 != F (obj, str, 42, &ex))
3627                         return 4;
3628
3629                 break;
3630         }
3631
3632         case 5: {
3633                 /* thunks.cs:Test.Test5 */
3634                 int (STDCALL *F)(gpointer, gpointer, int, gpointer*);
3635                 gpointer obj;
3636                 gpointer str;
3637
3638                 F = test_method;
3639                 obj = CreateObject (&ex);
3640                 str = mono_string_new_wrapper ("bar");
3641
3642                 F (obj, str, 42, &ex);
3643                 if (!ex)
3644                     return 4;
3645
3646                 break;
3647         }
3648
3649         case 6: {
3650                 /* thunks.cs:Test.Test6 */
3651                 int (STDCALL *F)(gpointer, guint8, gint16, gint32, gint64, float, double,
3652                                  gpointer, gpointer*);
3653                 gpointer obj;
3654                 gpointer str = mono_string_new_wrapper ("Test6");
3655                 int res;
3656
3657                 F = test_method;
3658                 obj = CreateObject (&ex);
3659
3660                 res = F (obj, 254, 32700, -245378, 6789600, 3.1415, 3.1415, str, &ex);
3661                 if (ex)
3662                         return 4;
3663
3664                 if (!res)
3665                         return 5;
3666
3667                 break;
3668         }
3669
3670         case 7: {
3671                 /* thunks.cs:Test.Test7 */
3672                 gint64 (STDCALL *F)(gpointer*) = test_method;
3673                 if (F (&ex) != G_MAXINT64)
3674                         return 4;
3675                 break;
3676         }
3677
3678         case 8: {
3679                 /* thunks.cs:Test.Test8 */
3680                 void (STDCALL *F)(guint8*, gint16*, gint32*, gint64*, float*, double*,
3681                                  gpointer*, gpointer*);
3682
3683                 guint8 a1;
3684                 gint16 a2;
3685                 gint32 a3;
3686                 gint64 a4;
3687                 float a5;
3688                 double a6;
3689                 gpointer a7;
3690
3691                 F = test_method;
3692
3693                 F (&a1, &a2, &a3, &a4, &a5, &a6, &a7, &ex);
3694                 if (ex)
3695                         return 4;
3696
3697                 if (!(a1 == 254 &&
3698                       a2 == 32700 &&
3699                       a3 == -245378 &&
3700                       a4 == 6789600 &&
3701                       (fabs (a5 - 3.1415) < 0.001) &&
3702                       (fabs (a6 - 3.1415) < 0.001) &&
3703                       strcmp (mono_string_to_utf8 (a7), "Test8") == 0))
3704                         return 5;
3705
3706                 break;
3707         }
3708
3709         case 9: {
3710                 /* thunks.cs:Test.Test9 */
3711                 void (STDCALL *F)(guint8*, gint16*, gint32*, gint64*, float*, double*,
3712                                  gpointer*, gpointer*);
3713
3714                 guint8 a1;
3715                 gint16 a2;
3716                 gint32 a3;
3717                 gint64 a4;
3718                 float a5;
3719                 double a6;
3720                 gpointer a7;
3721
3722                 F = test_method;
3723
3724                 F (&a1, &a2, &a3, &a4, &a5, &a6, &a7, &ex);
3725                 if (!ex)
3726                         return 4;
3727
3728                 break;
3729         }
3730
3731         case 10: {
3732                 /* thunks.cs:Test.Test10 */
3733                 void (STDCALL *F)(gpointer*, gpointer*);
3734
3735                 gpointer obj1, obj2;
3736
3737                 obj1 = obj2 = CreateObject (&ex);
3738                 if (ex)
3739                         return 4;
3740
3741                 F = test_method;
3742
3743                 F (&obj1, &ex);
3744                 if (ex)
3745                         return 5;
3746
3747                 if (obj1 == obj2)
3748                         return 6;
3749
3750                 break;
3751         }
3752
3753         case 100: {
3754                 /* thunks.cs:TestStruct.Test0 */
3755                 int (STDCALL *F)(gpointer*, gpointer*);
3756
3757                 gpointer obj;
3758                 TestStruct *a1;
3759                 int res;
3760
3761                 obj = CreateObject (&ex);
3762                 if (ex)
3763                         return 4;
3764
3765                 if (!obj)
3766                         return 5;
3767
3768                 a1 = mono_object_unbox (obj);
3769                 if (!a1)
3770                         return 6;
3771
3772                 a1->A = 42;
3773                 a1->B = 3.1415;
3774
3775                 F = test_method;
3776
3777                 res = F (obj, &ex);
3778                 if (ex)
3779                         return 7;
3780
3781                 if (!res)
3782                         return 8;
3783
3784                 /* check whether the call was really by value */
3785                 if (a1->A != 42 || a1->B != 3.1415)
3786                         return 9;
3787
3788                 break;
3789         }
3790
3791         case 101: {
3792                 /* thunks.cs:TestStruct.Test1 */
3793                 void (STDCALL *F)(gpointer, gpointer*);
3794
3795                 TestStruct *a1;
3796                 gpointer obj;
3797
3798                 obj = CreateObject (&ex);
3799                 if (ex)
3800                         return 4;
3801
3802                 if (!obj)
3803                         return 5;
3804
3805                 a1 = mono_object_unbox (obj);
3806                 if (!a1)
3807                         return 6;
3808
3809                 F = test_method;
3810
3811                 F (obj, &ex);
3812                 if (ex)
3813                         return 7;
3814
3815                 if (a1->A != 42)
3816                         return 8;
3817
3818                 if (!fabs (a1->B - 3.1415) < 0.001)
3819                         return 9;
3820
3821                 break;
3822         }
3823
3824         case 102: {
3825                 /* thunks.cs:TestStruct.Test2 */
3826                 gpointer (STDCALL *F)(gpointer*);
3827
3828                 TestStruct *a1;
3829                 gpointer obj;
3830
3831                 F = test_method;
3832
3833                 obj = F (&ex);
3834                 if (ex)
3835                         return 4;
3836
3837                 if (!obj)
3838                         return 5;
3839
3840                 a1 = mono_object_unbox (obj);
3841
3842                 if (a1->A != 42)
3843                         return 5;
3844
3845                 if (!fabs (a1->B - 3.1415) < 0.001)
3846                         return 6;
3847
3848                 break;
3849         }
3850
3851         case 103: {
3852                 /* thunks.cs:TestStruct.Test3 */
3853                 void (STDCALL *F)(gpointer, gpointer*);
3854
3855                 TestStruct *a1;
3856                 gpointer obj;
3857
3858                 obj = CreateObject (&ex);
3859                 if (ex)
3860                         return 4;
3861
3862                 if (!obj)
3863                         return 5;
3864                 
3865                 a1 = mono_object_unbox (obj);
3866
3867                 if (!a1)
3868                         return 6;
3869
3870                 a1->A = 42;
3871                 a1->B = 3.1415;
3872
3873                 F = test_method;
3874
3875                 F (obj, &ex);
3876                 if (ex)
3877                         return 4;
3878
3879                 if (a1->A != 1)
3880                         return 5;
3881
3882                 if (a1->B != 17)
3883                         return 6;
3884
3885                 break;
3886         }
3887
3888         default:
3889                 return 9;
3890
3891         }
3892
3893         return 0;
3894 }
3895
3896 typedef struct 
3897 {
3898         char a;
3899 } winx64_struct1;
3900
3901 LIBTEST_API int STDCALL  
3902 mono_test_Winx64_struct1_in (winx64_struct1 var)
3903 {
3904         if (var.a != 123)
3905                 return 1;
3906         return 0;
3907 }
3908
3909 typedef struct
3910 {
3911         char a;
3912         char b;
3913 } winx64_struct2;
3914
3915 LIBTEST_API int STDCALL  
3916 mono_test_Winx64_struct2_in (winx64_struct2 var)
3917 {
3918         if (var.a != 4)
3919                 return 1;
3920         if (var.b != 5)
3921                 return 2;
3922         return 0;
3923 }
3924
3925
3926 typedef struct
3927 {
3928         char a;
3929         char b;
3930         short c;
3931 } winx64_struct3;
3932
3933 LIBTEST_API int STDCALL  
3934 mono_test_Winx64_struct3_in (winx64_struct3 var)
3935 {
3936         if (var.a != 4)
3937                 return 1;
3938         if (var.b != 5)
3939                 return 2;
3940         if (var.c != 0x1234)
3941                 return 3;
3942         return 0;
3943 }
3944
3945 typedef struct
3946 {
3947         char a;
3948         char b;
3949         short c;
3950         unsigned int d;
3951 } winx64_struct4;
3952
3953 LIBTEST_API int STDCALL  
3954 mono_test_Winx64_struct4_in (winx64_struct4 var)
3955 {
3956         if (var.a != 4)
3957                 return 1;
3958         if (var.b != 5)
3959                 return 2;
3960         if (var.c != 0x1234)
3961                 return 3;
3962         if (var.d != 0x87654321)
3963                 return 4;
3964         return 0;
3965 }
3966
3967 typedef struct
3968 {
3969         char a;
3970         char b;
3971         char c;
3972 } winx64_struct5;
3973
3974 LIBTEST_API int STDCALL  
3975 mono_test_Winx64_struct5_in (winx64_struct5 var)
3976 {
3977         if (var.a != 4)
3978                 return 1;
3979         if (var.b != 5)
3980                 return 2;
3981         if (var.c != 6)
3982                 return 3;
3983         return 0;
3984 }
3985
3986 typedef struct
3987 {
3988         winx64_struct1 a;
3989         short b;
3990         char c;
3991 } winx64_struct6;
3992
3993 LIBTEST_API int STDCALL  
3994 mono_test_Winx64_struct6_in (winx64_struct6 var)
3995 {
3996         if (var.a.a != 4)
3997                 return 1;
3998         if (var.b != 5)
3999                 return 2;
4000         if (var.c != 6)
4001                 return 3;
4002         return 0;
4003 }
4004
4005 LIBTEST_API int STDCALL  
4006 mono_test_Winx64_structs_in1 (winx64_struct1 var1,
4007                          winx64_struct2 var2,
4008                          winx64_struct3 var3,
4009                          winx64_struct4 var4)
4010 {
4011         if (var1.a != 123)
4012                 return 1;
4013         
4014         if (var2.a != 4)
4015                 return 2;
4016         if (var2.b != 5)
4017                 return 3;
4018         
4019         if (var3.a != 4)
4020                 return 4;
4021         if (var3.b != 5)
4022                 return 2;
4023         if (var3.c != 0x1234)
4024                 return 5;
4025         
4026         if (var4.a != 4)
4027                 return 6;
4028         if (var4.b != 5)
4029                 return 7;
4030         if (var4.c != 0x1234)
4031                 return 8;
4032         if (var4.d != 0x87654321)
4033                 return 9;
4034         return 0;
4035 }
4036
4037 LIBTEST_API int STDCALL  
4038 mono_test_Winx64_structs_in2 (winx64_struct1 var1,
4039                          winx64_struct1 var2,
4040                          winx64_struct1 var3,
4041                          winx64_struct1 var4,
4042                          winx64_struct1 var5)
4043 {
4044         if (var1.a != 1)
4045                 return 1;
4046         if (var2.a != 2)
4047                 return 2;
4048         if (var3.a != 3)
4049                 return 3;
4050         if (var4.a != 4)
4051                 return 4;
4052         if (var5.a != 5)
4053                 return 5;
4054         
4055         return 0;
4056 }
4057
4058 LIBTEST_API int STDCALL  
4059 mono_test_Winx64_structs_in3 (winx64_struct1 var1,
4060                          winx64_struct5 var2,
4061                          winx64_struct1 var3,
4062                          winx64_struct5 var4,
4063                          winx64_struct1 var5,
4064                          winx64_struct5 var6)
4065 {
4066         if (var1.a != 1)
4067                 return 1;
4068         
4069         if (var2.a != 2)
4070                 return 2;
4071         if (var2.b != 3)
4072                 return 2;
4073         if (var2.c != 4)
4074                 return 4;
4075         
4076         if (var3.a != 5)
4077                 return 5;
4078         
4079         if (var4.a != 6)
4080                 return 6;
4081         if (var4.b != 7)
4082                 return 7;
4083         if (var4.c != 8)
4084                 return 8;
4085         
4086         if (var5.a != 9)
4087                 return 9;
4088
4089         if (var6.a != 10)
4090                 return 10;
4091         if (var6.b != 11)
4092                 return 11;
4093         if (var6.c != 12)
4094                 return 12;
4095         
4096         return 0;
4097 }
4098
4099 LIBTEST_API winx64_struct1 STDCALL  
4100 mono_test_Winx64_struct1_ret (void)
4101 {
4102         winx64_struct1 ret;
4103         ret.a = 123;
4104         return ret;
4105 }
4106
4107 LIBTEST_API winx64_struct2 STDCALL  
4108 mono_test_Winx64_struct2_ret (void)
4109 {
4110         winx64_struct2 ret;
4111         ret.a = 4;
4112         ret.b = 5;
4113         return ret;
4114 }
4115
4116 LIBTEST_API winx64_struct3 STDCALL  
4117 mono_test_Winx64_struct3_ret (void)
4118 {
4119         winx64_struct3 ret;
4120         ret.a = 4;
4121         ret.b = 5;
4122         ret.c = 0x1234;
4123         return ret;
4124 }
4125
4126 LIBTEST_API winx64_struct4 STDCALL  
4127 mono_test_Winx64_struct4_ret (void)
4128 {
4129         winx64_struct4 ret;
4130         ret.a = 4;
4131         ret.b = 5;
4132         ret.c = 0x1234;
4133         ret.d = 0x87654321;
4134         return ret;
4135 }
4136
4137 LIBTEST_API winx64_struct5 STDCALL  
4138 mono_test_Winx64_struct5_ret (void)
4139 {
4140         winx64_struct5 ret;
4141         ret.a = 4;
4142         ret.b = 5;
4143         ret.c = 6;
4144         return ret;
4145 }
4146
4147 LIBTEST_API winx64_struct1 STDCALL  
4148 mono_test_Winx64_struct1_ret_5_args (char a, char b, char c, char d, char e)
4149 {
4150         winx64_struct1 ret;
4151         ret.a = a + b + c + d + e;
4152         return ret;
4153 }
4154
4155 LIBTEST_API winx64_struct5 STDCALL
4156 mono_test_Winx64_struct5_ret6_args (char a, char b, char c, char d, char e)
4157 {
4158         winx64_struct5 ret;
4159         ret.a = a + b;
4160         ret.b = c + d;
4161         ret.c = e;
4162         return ret;
4163 }
4164
4165 typedef struct
4166 {
4167         float a;
4168         float b;
4169 } winx64_floatStruct;
4170
4171 LIBTEST_API int STDCALL  
4172 mono_test_Winx64_floatStruct (winx64_floatStruct a)
4173 {
4174         if (a.a > 5.6 || a.a < 5.4)
4175                 return 1;
4176
4177         if (a.b > 9.6 || a.b < 9.4)
4178                 return 2;
4179         
4180         return 0;
4181 }
4182
4183 typedef struct
4184 {
4185         double a;
4186 } winx64_doubleStruct;
4187
4188 LIBTEST_API int STDCALL  
4189 mono_test_Winx64_doubleStruct (winx64_doubleStruct a)
4190 {
4191         if (a.a > 5.6 || a.a < 5.4)
4192                 return 1;
4193         
4194         return 0;
4195 }
4196
4197 typedef int (STDCALL *managed_struct1_delegate) (winx64_struct1 a);
4198
4199 LIBTEST_API int STDCALL 
4200 mono_test_managed_Winx64_struct1_in(managed_struct1_delegate func)
4201 {
4202         winx64_struct1 val;
4203         val.a = 5;
4204         return func (val);
4205 }
4206
4207 typedef int (STDCALL *managed_struct5_delegate) (winx64_struct5 a);
4208
4209 LIBTEST_API int STDCALL 
4210 mono_test_managed_Winx64_struct5_in(managed_struct5_delegate func)
4211 {
4212         winx64_struct5 val;
4213         val.a = 5;
4214         val.b = 0x10;
4215         val.c = 0x99;
4216         return func (val);
4217 }
4218
4219 typedef int (STDCALL *managed_struct1_struct5_delegate) (winx64_struct1 a, winx64_struct5 b,
4220                                                          winx64_struct1 c, winx64_struct5 d,
4221                                                          winx64_struct1 e, winx64_struct5 f);
4222
4223 LIBTEST_API int STDCALL 
4224 mono_test_managed_Winx64_struct1_struct5_in(managed_struct1_struct5_delegate func)
4225 {
4226         winx64_struct1 a, c, e;
4227         winx64_struct5 b, d, f;
4228         a.a = 1;
4229         b.a = 2; b.b = 3; b.c = 4;
4230         c.a = 5;
4231         d.a = 6; d.b = 7; d.c = 8;
4232         e.a = 9;
4233         f.a = 10; f.b = 11; f.c = 12;
4234
4235         return func (a, b, c, d, e, f);
4236 }
4237
4238 typedef winx64_struct1 (STDCALL *managed_struct1_ret_delegate) (void);
4239
4240 LIBTEST_API int STDCALL 
4241 mono_test_Winx64_struct1_ret_managed (managed_struct1_ret_delegate func)
4242 {
4243         winx64_struct1 ret;
4244
4245         ret = func ();
4246
4247         if (ret.a != 0x45)
4248                 return 1;
4249         
4250         return 0;
4251 }
4252
4253 typedef winx64_struct5 (STDCALL *managed_struct5_ret_delegate) (void);
4254
4255 LIBTEST_API int STDCALL 
4256 mono_test_Winx64_struct5_ret_managed (managed_struct5_ret_delegate func)
4257 {
4258         winx64_struct5 ret;
4259
4260         ret = func ();
4261
4262         if (ret.a != 0x12)
4263                 return 1;
4264         if (ret.b != 0x34)
4265                 return 2;
4266         if (ret.c != 0x56)
4267                 return 3;
4268         
4269         return 0;
4270 }
4271
4272 LIBTEST_API int STDCALL 
4273 mono_test_marshal_bool_in (int arg, unsigned int expected, unsigned int bDefaultMarsh, unsigned int bBoolCustMarsh,
4274                            char bI1CustMarsh, unsigned char bU1CustMarsh, short bVBCustMarsh)
4275 {
4276         switch (arg) {
4277         case 1: 
4278                 if (bDefaultMarsh != expected)
4279                         return 1;
4280                 break;
4281         case 2: 
4282                 if (bBoolCustMarsh != expected)
4283                         return 2;
4284                 break;
4285         case 3: 
4286                 if (bI1CustMarsh != expected)
4287                         return 3;
4288                 break;
4289         case 4: 
4290                 if (bU1CustMarsh != expected)
4291                         return 4;
4292                 break;
4293         case 5: 
4294                 if (bVBCustMarsh != expected)
4295                         return 5;
4296                 break;
4297         default:
4298                 return 999;             
4299         }
4300         return 0;
4301 }
4302
4303 LIBTEST_API int STDCALL 
4304 mono_test_marshal_bool_out (int arg, unsigned int testVal, unsigned int* bDefaultMarsh, unsigned int* bBoolCustMarsh,
4305                            char* bI1CustMarsh, unsigned char* bU1CustMarsh, unsigned short* bVBCustMarsh)
4306 {
4307         switch (arg) {
4308         case 1: 
4309                 if (!bDefaultMarsh)
4310                         return 1;
4311                 *bDefaultMarsh = testVal;
4312                 break;  
4313         case 2: 
4314                 if (!bBoolCustMarsh)
4315                         return 2;
4316                 *bBoolCustMarsh = testVal;
4317                 break;  
4318         case 3: 
4319                 if (!bI1CustMarsh)
4320                         return 3;
4321                 *bI1CustMarsh = (char)testVal;
4322                 break;  
4323         case 4: 
4324                 if (!bU1CustMarsh)
4325                         return 4;
4326                 *bU1CustMarsh = (unsigned char)testVal;
4327                 break;  
4328         case 5: 
4329                 if (!bVBCustMarsh)
4330                         return 5;
4331                 *bVBCustMarsh = (unsigned short)testVal;
4332                 break;  
4333         default:
4334                 return 999;
4335         }
4336         return 0;
4337 }
4338
4339 LIBTEST_API int STDCALL 
4340 mono_test_marshal_bool_ref (int arg, unsigned int expected, unsigned int testVal, unsigned int* bDefaultMarsh,
4341                             unsigned int* bBoolCustMarsh, char* bI1CustMarsh, unsigned char* bU1CustMarsh, 
4342                             unsigned short* bVBCustMarsh)
4343 {
4344         switch (arg) {
4345         case 1: 
4346                 if (!bDefaultMarsh)
4347                         return 1;
4348                 if (*bDefaultMarsh != expected)
4349                         return 2;
4350                 *bDefaultMarsh = testVal;
4351                 break;
4352         case 2: 
4353                 if (!bBoolCustMarsh)
4354                         return 3;
4355                 if (*bBoolCustMarsh != expected)
4356                         return 4;
4357                 *bBoolCustMarsh = testVal;
4358                 break;
4359         case 3: 
4360                 if (!bI1CustMarsh)
4361                         return 5;
4362                 if (*bI1CustMarsh != expected)
4363                         return 6;
4364                 *bI1CustMarsh = (char)testVal;
4365                 break;
4366         case 4: 
4367                 if (!bU1CustMarsh)
4368                         return 7;
4369                 if (*bU1CustMarsh != expected)
4370                         return 8;
4371                 *bU1CustMarsh = (unsigned char)testVal;
4372                 break;
4373         case 5: 
4374                 if (!bVBCustMarsh)
4375                         return 9;
4376                 if (*bVBCustMarsh != expected)
4377                         return 10;
4378                 *bVBCustMarsh = (unsigned short)testVal;
4379                 break;
4380         default:
4381                 return 999;             
4382         }
4383         return 0;
4384 }
4385
4386
4387 typedef int (STDCALL *MarshalBoolInDelegate) (int arg, unsigned int expected, unsigned int bDefaultMarsh,
4388         unsigned int bBoolCustMarsh, char bI1CustMarsh, unsigned char bU1CustMarsh, unsigned short bVBCustMarsh);
4389
4390 LIBTEST_API int STDCALL 
4391 mono_test_managed_marshal_bool_in (int arg, unsigned int expected, unsigned int testVal, MarshalBoolInDelegate pfcn)
4392 {
4393         if (!pfcn)
4394                 return 0x9900;
4395
4396         switch (arg) {
4397         case 1:
4398                 return pfcn (arg, expected, testVal, 0, 0, 0, 0);
4399         case 2:
4400                 return pfcn (arg, expected, 0, testVal,  0, 0, 0);
4401         case 3:
4402                 return pfcn (arg, expected, 0, 0, testVal, 0, 0);
4403         case 4:
4404                 return pfcn (arg, expected, 0, 0, 0, testVal, 0);
4405         case 5:
4406                 return pfcn (arg, expected, 0, 0, 0, 0, testVal);
4407         default:
4408                 return 0x9800;
4409         }
4410
4411         return 0;
4412 }
4413
4414 typedef int (STDCALL *MarshalBoolOutDelegate) (int arg, unsigned int expected, unsigned int* bDefaultMarsh,
4415         unsigned int* bBoolCustMarsh, char* bI1CustMarsh, unsigned char* bU1CustMarsh, unsigned short* bVBCustMarsh);
4416
4417 LIBTEST_API int STDCALL 
4418 mono_test_managed_marshal_bool_out (int arg, unsigned int expected, unsigned int testVal, MarshalBoolOutDelegate pfcn)
4419 {
4420         int ret;
4421         unsigned int lDefaultMarsh, lBoolCustMarsh;
4422         char lI1CustMarsh = 0;
4423         unsigned char lU1CustMarsh = 0;
4424         unsigned short lVBCustMarsh = 0;
4425         lDefaultMarsh = lBoolCustMarsh = 0;
4426
4427         if (!pfcn)
4428                 return 0x9900;
4429
4430         switch (arg) {
4431         case 1: {
4432                 unsigned int ltVal = 0;
4433                 ret = pfcn (arg, testVal, &ltVal, &lBoolCustMarsh, &lI1CustMarsh, &lU1CustMarsh, &lVBCustMarsh);
4434                 if (ret)
4435                         return 0x0100 + ret;
4436                 if (expected != ltVal)
4437                         return 0x0200;
4438                 break;
4439         }
4440         case 2: {
4441                 unsigned int ltVal = 0;
4442                 ret = pfcn (arg, testVal, &lDefaultMarsh, &ltVal, &lI1CustMarsh, &lU1CustMarsh, &lVBCustMarsh);
4443                 if (ret)
4444                         return 0x0300 + ret;
4445                 if (expected != ltVal)
4446                         return 0x0400;
4447                 break;
4448         }
4449         case 3: {
4450                 char ltVal = 0;
4451                 ret = pfcn (arg, testVal, &lDefaultMarsh, &lBoolCustMarsh, &ltVal, &lU1CustMarsh, &lVBCustMarsh);
4452                 if (ret)
4453                         return 0x0500 + ret;
4454                 if (expected != ltVal)
4455                         return 0x0600;
4456                 break;
4457         }
4458         case 4: {
4459                 unsigned char ltVal = 0;
4460                 ret = pfcn (arg, testVal, &lDefaultMarsh, &lBoolCustMarsh, &lI1CustMarsh, &ltVal, &lVBCustMarsh);
4461                 if (ret)
4462                         return 0x0700 + ret;
4463                 if (expected != ltVal)
4464                         return 0x0800;
4465                 break;
4466         }
4467         case 5: {
4468                 unsigned short ltVal = 0;
4469                 ret = pfcn (arg, testVal, &lDefaultMarsh, &lBoolCustMarsh, &lI1CustMarsh, &lU1CustMarsh, &ltVal);
4470                 if (ret)
4471                         return 0x0900 + ret;
4472                 if (expected != ltVal)
4473                         return 0x1000;
4474                 break;
4475         }
4476         default:
4477                 return 0x9800;
4478         }
4479
4480         return 0;
4481 }
4482
4483 typedef int (STDCALL *MarshalBoolRefDelegate) (int arg, unsigned int expected, unsigned int testVal, unsigned int* bDefaultMarsh,
4484         unsigned int* bBoolCustMarsh, char* bI1CustMarsh, unsigned char* bU1CustMarsh, unsigned short* bVBCustMarsh);
4485
4486 LIBTEST_API int STDCALL 
4487 mono_test_managed_marshal_bool_ref (int arg, unsigned int expected, unsigned int testVal, unsigned int outExpected,
4488                                     unsigned int outTestVal, MarshalBoolRefDelegate pfcn)
4489 {
4490         int ret;
4491         unsigned int lDefaultMarsh, lBoolCustMarsh;
4492         char lI1CustMarsh = 0;
4493         unsigned char lU1CustMarsh = 0;
4494         unsigned short lVBCustMarsh = 0;
4495         lDefaultMarsh = lBoolCustMarsh = 0;
4496
4497         if (!pfcn)
4498                 return 0x9900;
4499
4500         switch (arg) {
4501         case 1:
4502         {
4503                 unsigned int ltestVal = testVal;
4504                 ret = pfcn (arg, expected, outTestVal, &ltestVal, &lBoolCustMarsh, &lI1CustMarsh, &lU1CustMarsh, &lVBCustMarsh);
4505                 if (ret)
4506                         return 0x0100 + ret;
4507                 if (outExpected != ltestVal)
4508                         return 0x0200;
4509                 break;
4510         }
4511         case 2:
4512         {
4513                 unsigned int ltestVal = testVal;
4514                 ret = pfcn (arg, expected, outTestVal, &lDefaultMarsh, &ltestVal, &lI1CustMarsh, &lU1CustMarsh, &lVBCustMarsh);
4515                 if (ret)
4516                         return 0x0300 + ret;
4517                 if (outExpected != ltestVal)
4518                         return 0x0400;
4519                 break;
4520         }
4521         case 3:
4522         {
4523                 char ltestVal = testVal;
4524                 ret = pfcn (arg, expected, outTestVal, &lDefaultMarsh, &lBoolCustMarsh, &ltestVal, &lU1CustMarsh, &lVBCustMarsh);
4525                 if (ret)
4526                         return 0x0500 + ret;
4527                 if (outExpected != ltestVal)
4528                         return 0x0600;
4529                 break;
4530         }
4531         case 4:
4532         {
4533                 unsigned char ltestVal = testVal;
4534                 ret = pfcn (arg, expected, outTestVal, &lDefaultMarsh, &lBoolCustMarsh, &lI1CustMarsh, &ltestVal, &lVBCustMarsh);
4535                 if (ret)
4536                         return 0x0700 + ret;
4537                 if (outExpected != ltestVal)
4538                         return 0x0800;
4539                 break;
4540         }
4541         case 5:
4542         {
4543                 unsigned short ltestVal = testVal;
4544                 ret = pfcn (arg, expected, outTestVal, &lDefaultMarsh, &lBoolCustMarsh, &lI1CustMarsh, &lU1CustMarsh, &ltestVal);
4545                 if (ret)
4546                         return 0x0900 + ret;
4547                 if (outExpected != ltestVal)
4548                         return 0x1000;
4549                 break;
4550         }
4551         default:
4552                 return 0x9800;
4553         }
4554
4555         return 0;
4556 }
4557
4558 #ifdef WIN32
4559
4560 LIBTEST_API int STDCALL 
4561 mono_test_marshal_safearray_out_1dim_vt_bstr_empty (SAFEARRAY** safearray)
4562 {
4563         /* Create an empty one-dimensional array of variants */
4564         SAFEARRAY *pSA;
4565         SAFEARRAYBOUND dimensions [1];
4566
4567         dimensions [0].lLbound = 0;
4568         dimensions [0].cElements = 0;
4569
4570         pSA= SafeArrayCreate (VT_VARIANT, 1, dimensions);
4571         *safearray = pSA;
4572         return S_OK;
4573 }
4574
4575 LIBTEST_API int STDCALL 
4576 mono_test_marshal_safearray_out_1dim_vt_bstr (SAFEARRAY** safearray)
4577 {
4578         /* Create a one-dimensional array of 10 variants filled with "0" to "9" */
4579         SAFEARRAY *pSA;
4580         SAFEARRAYBOUND dimensions [1];
4581         long i;
4582         gchar buffer [20];
4583         HRESULT hr = S_OK;
4584         long indices [1];
4585
4586         dimensions [0].lLbound = 0;
4587         dimensions [0].cElements = 10;
4588
4589         pSA= SafeArrayCreate (VT_VARIANT, 1, dimensions);
4590         for (i= dimensions [0].lLbound; i< (dimensions [0].cElements + dimensions [0].lLbound); i++) {
4591                 VARIANT vOut;
4592                 VariantInit (&vOut);
4593                 vOut.vt = VT_BSTR;
4594                 _ltoa (i,buffer,10);
4595                 vOut.bstrVal= marshal_bstr_alloc (buffer);
4596                 indices [0] = i;
4597                 if ((hr = SafeArrayPutElement (pSA, indices, &vOut)) != S_OK) {
4598                         VariantClear (&vOut);
4599                         SafeArrayDestroy (pSA);
4600                         return hr;
4601                 }
4602                 VariantClear (&vOut);
4603         }
4604         *safearray = pSA;
4605         return hr;
4606 }
4607
4608 LIBTEST_API int STDCALL 
4609 mono_test_marshal_safearray_out_2dim_vt_i4 (SAFEARRAY** safearray)
4610 {
4611         /* Create a two-dimensional array of 4x3 variants filled with 11, 12, 13, etc. */
4612         SAFEARRAY *pSA;
4613         SAFEARRAYBOUND dimensions [2];
4614         long i, j;
4615         HRESULT hr = S_OK;
4616         long indices [2];
4617
4618         dimensions [0].lLbound = 0;
4619         dimensions [0].cElements = 4;
4620         dimensions [1].lLbound = 0;
4621         dimensions [1].cElements = 3;
4622
4623         pSA= SafeArrayCreate(VT_VARIANT, 2, dimensions);
4624         for (i= dimensions [0].lLbound; i< (dimensions [0].cElements + dimensions [0].lLbound); i++) {
4625                 for (j= dimensions [1].lLbound; j< (dimensions [1].cElements + dimensions [1].lLbound); j++) {
4626                         VARIANT vOut;
4627                         VariantInit (&vOut);
4628                         vOut.vt = VT_I4;
4629                         vOut.lVal = (i+1)*10+(j+1);
4630                         indices [0] = i;
4631                         indices [1] = j;
4632                         if ((hr = SafeArrayPutElement (pSA, indices, &vOut)) != S_OK) {
4633                                 VariantClear (&vOut);
4634                                 SafeArrayDestroy (pSA);
4635                                 return hr;
4636                         }
4637                         VariantClear (&vOut);  // does a deep destroy of source VARIANT 
4638                 }
4639         }
4640         *safearray = pSA;
4641         return hr;
4642 }
4643
4644 LIBTEST_API int STDCALL 
4645 mono_test_marshal_safearray_out_4dim_vt_i4 (SAFEARRAY** safearray)
4646 {
4647         /* Create a four-dimensional array of 10x3x6x7 variants filled with their indices */
4648         /* Also use non zero lower bounds                                                 */
4649         SAFEARRAY *pSA;
4650         SAFEARRAYBOUND dimensions [4];
4651         long i;
4652         HRESULT hr = S_OK;
4653         VARIANT *pData;
4654
4655         dimensions [0].lLbound = 15;
4656         dimensions [0].cElements = 10;
4657         dimensions [1].lLbound = 20;
4658         dimensions [1].cElements = 3;
4659         dimensions [2].lLbound = 5;
4660         dimensions [2].cElements = 6;
4661         dimensions [3].lLbound = 12;
4662         dimensions [3].cElements = 7;
4663
4664         pSA= SafeArrayCreate (VT_VARIANT, 4, dimensions);
4665
4666         SafeArrayAccessData (pSA, (void **)&pData);
4667
4668         for (i= 0; i< 10*3*6*7; i++) {
4669                 VariantInit(&pData [i]);
4670                 pData [i].vt = VT_I4;
4671                 pData [i].lVal = i;
4672         }
4673         SafeArrayUnaccessData (pSA);
4674         *safearray = pSA;
4675         return hr;
4676 }
4677
4678 LIBTEST_API int STDCALL 
4679 mono_test_marshal_safearray_in_byval_1dim_empty (SAFEARRAY* safearray)
4680 {
4681         /* Check that array is one dimensional and empty */
4682
4683         UINT dim;
4684         long lbound, ubound;
4685         
4686         dim = SafeArrayGetDim (safearray);
4687         if (dim != 1)
4688                 return 1;
4689
4690         SafeArrayGetLBound (safearray, 1, &lbound);
4691         SafeArrayGetUBound (safearray, 1, &ubound);
4692
4693         if ((lbound > 0) || (ubound > 0))
4694                 return 1;
4695
4696         return 0;
4697 }
4698
4699 LIBTEST_API int STDCALL 
4700 mono_test_marshal_safearray_in_byval_1dim_vt_i4 (SAFEARRAY* safearray)
4701 {
4702         /* Check that array is one dimensional containing integers from 1 to 10 */
4703
4704         UINT dim;
4705         long lbound, ubound;
4706         VARIANT *pData; 
4707         long i;
4708         int result=0;
4709
4710         dim = SafeArrayGetDim (safearray);
4711         if (dim != 1)
4712                 return 1;
4713
4714         SafeArrayGetLBound (safearray, 1, &lbound);
4715         SafeArrayGetUBound (safearray, 1, &ubound);
4716
4717         if ((lbound != 0) || (ubound != 9))
4718                 return 1;
4719
4720         SafeArrayAccessData (safearray, (void **)&pData);
4721         for (i= lbound; i <= ubound; i++) {
4722                 if ((VariantChangeType (&pData [i], &pData [i], VARIANT_NOUSEROVERRIDE, VT_I4) != S_OK) || (pData [i].lVal != i + 1))
4723                         result = 1;
4724         }
4725         SafeArrayUnaccessData (safearray);
4726
4727         return result;
4728 }
4729
4730 LIBTEST_API int STDCALL 
4731 mono_test_marshal_safearray_in_byval_1dim_vt_mixed (SAFEARRAY* safearray)
4732 {
4733         /* Check that array is one dimensional containing integers mixed with strings from 0 to 12 */
4734
4735         UINT dim;
4736         long lbound, ubound;
4737         VARIANT *pData; 
4738         long i;
4739         long indices [1];
4740         VARIANT element;
4741         int result=0;
4742
4743         VariantInit (&element);
4744
4745         dim = SafeArrayGetDim (safearray);
4746         if (dim != 1)
4747                 return 1;
4748
4749         SafeArrayGetLBound (safearray, 1, &lbound);
4750         SafeArrayGetUBound (safearray, 1, &ubound);
4751                 
4752         if ((lbound != 0) || (ubound != 12))
4753                 return 1;
4754
4755         SafeArrayAccessData (safearray, (void **)&pData);
4756         for (i= lbound; i <= ubound; i++) {
4757                 if ((i%2 == 0) && (pData [i].vt != VT_I4))
4758                         result = 1;
4759                 if ((i%2 == 1) && (pData [i].vt != VT_BSTR))
4760                         result = 1;
4761                 if ((VariantChangeType (&pData [i], &pData [i], VARIANT_NOUSEROVERRIDE, VT_I4) != S_OK) || (pData [i].lVal != i))
4762                         result = 1;
4763         }
4764         SafeArrayUnaccessData (safearray);
4765
4766         /* Change the first element of the array to verify that [in] parameters are not marshalled back to the managed side */
4767
4768         indices [0] = 0;
4769         element.vt = VT_I4;
4770         element.lVal = 333;
4771         SafeArrayPutElement (safearray, indices, &element);
4772         VariantClear (&element);
4773
4774         return result;
4775 }
4776
4777 LIBTEST_API int STDCALL 
4778 mono_test_marshal_safearray_in_byval_2dim_vt_i4 (SAFEARRAY* safearray)
4779 {
4780         /* Check that array is one dimensional containing integers mixed with strings from 0 to 12 */
4781
4782         UINT dim;
4783         long lbound1, ubound1, lbound2, ubound2;
4784         long i, j, failed;
4785         long indices [2];
4786         VARIANT element;
4787
4788         VariantInit (&element);
4789
4790         dim = SafeArrayGetDim (safearray);
4791         if (dim != 2)
4792                 return 1;
4793
4794         SafeArrayGetLBound (safearray, 1, &lbound1);
4795         SafeArrayGetUBound (safearray, 1, &ubound1);
4796
4797         if ((lbound1 != 0) || (ubound1 != 1))
4798                 return 1;
4799
4800         SafeArrayGetLBound (safearray, 2, &lbound2);
4801         SafeArrayGetUBound (safearray, 2, &ubound2);
4802
4803         if ((lbound2 != 0) || (ubound2 != 3)) {
4804                 return 1;
4805         }
4806
4807         for (i= lbound1; i <= ubound1; i++) {
4808                 indices [0] = i;
4809                 for (j= lbound2; j <= ubound2; j++) {
4810                         indices [1] = j;
4811                         if (SafeArrayGetElement (safearray, indices, &element) != S_OK)
4812                                 return 1;
4813                         failed = ((element.vt != VT_I4) || (element.lVal != 10*(i+1)+(j+1)));
4814                         VariantClear (&element);
4815                         if (failed)
4816                                 return 1;
4817                 }
4818         }
4819
4820         /* Change the first element of the array to verify that [in] parameters are not marshalled back to the managed side */
4821
4822         indices [0] = 0;
4823         indices [1] = 0;
4824         element.vt = VT_I4;
4825         element.lVal = 333;
4826         SafeArrayPutElement (safearray, indices, &element);
4827         VariantClear (&element);
4828
4829         return 0;
4830 }
4831
4832 LIBTEST_API int STDCALL 
4833 mono_test_marshal_safearray_in_byval_3dim_vt_bstr (SAFEARRAY* safearray)
4834 {
4835         /* Check that array is one dimensional containing integers mixed with strings from 0 to 12 */
4836
4837         UINT dim;
4838         long lbound1, ubound1, lbound2, ubound2, lbound3, ubound3;
4839         long i, j, k, failed;
4840         long indices [3];
4841         VARIANT element;
4842
4843         VariantInit (&element);
4844
4845         dim = SafeArrayGetDim (safearray);
4846         if (dim != 3)
4847                 return 1;
4848
4849         SafeArrayGetLBound (safearray, 1, &lbound1);
4850         SafeArrayGetUBound (safearray, 1, &ubound1);
4851
4852         if ((lbound1 != 0) || (ubound1 != 1))
4853                 return 1;
4854
4855         SafeArrayGetLBound (safearray, 2, &lbound2);
4856         SafeArrayGetUBound (safearray, 2, &ubound2);
4857
4858         if ((lbound2 != 0) || (ubound2 != 1))
4859                 return 1;
4860
4861         SafeArrayGetLBound (safearray, 3, &lbound3);
4862         SafeArrayGetUBound (safearray, 3, &ubound3);
4863
4864         if ((lbound3 != 0) || (ubound3 != 2))
4865                 return 1;
4866
4867         for (i= lbound1; i <= ubound1; i++) {
4868                 indices [0] = i;
4869                 for (j= lbound2; j <= ubound2; j++) {
4870                         indices [1] = j;
4871                 for (k= lbound3; k <= ubound3; k++) {
4872                                 indices [2] = k;
4873                                 if (SafeArrayGetElement (safearray, indices, &element) != S_OK)
4874                                         return 1;
4875                                 failed = ((element.vt != VT_BSTR) 
4876                                         || (VariantChangeType (&element, &element, VARIANT_NOUSEROVERRIDE, VT_I4) != S_OK) 
4877                                         || (element.lVal != 100*(i+1)+10*(j+1)+(k+1)));
4878                                 VariantClear (&element);
4879                                 if (failed)
4880                                         return 1;
4881                         }
4882                 }
4883         }
4884
4885         /* Change the first element of the array to verify that [in] parameters are not marshalled back to the managed side */
4886
4887         indices [0] = 0;
4888         indices [1] = 0;
4889         indices [2] = 0;
4890         element.vt = VT_BSTR;
4891         element.bstrVal = SysAllocString(L"Should not be copied");
4892         SafeArrayPutElement (safearray, indices, &element);
4893         VariantClear (&element);
4894
4895         return 0;
4896 }
4897
4898 LIBTEST_API int STDCALL 
4899 mono_test_marshal_safearray_in_byref_3dim_vt_bstr (SAFEARRAY** safearray)
4900 {
4901         return mono_test_marshal_safearray_in_byval_3dim_vt_bstr (*safearray);
4902 }
4903
4904 LIBTEST_API int STDCALL 
4905 mono_test_marshal_safearray_in_out_byref_1dim_empty (SAFEARRAY** safearray)
4906 {
4907         /* Check that the input array is what is expected and change it so the caller can check */
4908         /* correct marshalling back to managed code                                             */
4909
4910         UINT dim;
4911         long lbound, ubound;
4912         SAFEARRAYBOUND dimensions [1];
4913         long i;
4914         wchar_t buffer [20];
4915         HRESULT hr = S_OK;
4916         long indices [1];
4917
4918         /* Check that in array is one dimensional and empty */
4919
4920         dim = SafeArrayGetDim (*safearray);
4921         if (dim != 1) {
4922                 return 1;
4923         }
4924
4925         SafeArrayGetLBound (*safearray, 1, &lbound);
4926         SafeArrayGetUBound (*safearray, 1, &ubound);
4927                 
4928         if ((lbound > 0) || (ubound > 0)) {
4929                 return 1;
4930         }
4931
4932         /* Re-dimension the array and return a one-dimensional array of 8 variants filled with "0" to "7" */
4933
4934         dimensions [0].lLbound = 0;
4935         dimensions [0].cElements = 8;
4936
4937         hr = SafeArrayRedim (*safearray, dimensions);
4938         if (hr != S_OK)
4939                 return 1;
4940
4941         for (i= dimensions [0].lLbound; i< (dimensions [0].lLbound + dimensions [0].cElements); i++) {
4942                 VARIANT vOut;
4943                 VariantInit (&vOut);
4944                 vOut.vt = VT_BSTR;
4945                 _ltow (i,buffer,10);
4946                 vOut.bstrVal = SysAllocString (buffer);
4947                 indices [0] = i;
4948                 if ((hr = SafeArrayPutElement (*safearray, indices, &vOut)) != S_OK) {
4949                         VariantClear (&vOut);
4950                         SafeArrayDestroy (*safearray);
4951                         return hr;
4952                 }
4953                 VariantClear (&vOut);
4954         }
4955         return hr;
4956 }
4957
4958 LIBTEST_API int STDCALL 
4959 mono_test_marshal_safearray_in_out_byref_3dim_vt_bstr (SAFEARRAY** safearray)
4960 {
4961         /* Check that the input array is what is expected and change it so the caller can check */
4962         /* correct marshalling back to managed code                                             */
4963
4964         UINT dim;
4965         long lbound1, ubound1, lbound2, ubound2, lbound3, ubound3;
4966         SAFEARRAYBOUND dimensions [1];
4967         long i, j, k, failed;
4968         wchar_t buffer [20];
4969         HRESULT hr = S_OK;
4970         long indices [3];
4971         VARIANT element;
4972
4973         VariantInit (&element);
4974
4975         /* Check that in array is three dimensional and contains the expected values */
4976
4977         dim = SafeArrayGetDim (*safearray);
4978         if (dim != 3)
4979                 return 1;
4980
4981         SafeArrayGetLBound (*safearray, 1, &lbound1);
4982         SafeArrayGetUBound (*safearray, 1, &ubound1);
4983
4984         if ((lbound1 != 0) || (ubound1 != 1))
4985                 return 1;
4986
4987         SafeArrayGetLBound (*safearray, 2, &lbound2);
4988         SafeArrayGetUBound (*safearray, 2, &ubound2);
4989
4990         if ((lbound2 != 0) || (ubound2 != 1))
4991                 return 1;
4992
4993         SafeArrayGetLBound (*safearray, 3, &lbound3);
4994         SafeArrayGetUBound (*safearray, 3, &ubound3);
4995
4996         if ((lbound3 != 0) || (ubound3 != 2))
4997                 return 1;
4998
4999         for (i= lbound1; i <= ubound1; i++) {
5000                 indices [0] = i;
5001                 for (j= lbound2; j <= ubound2; j++) {
5002                         indices [1] = j;
5003                         for (k= lbound3; k <= ubound3; k++) {
5004                                 indices [2] = k;
5005                                 if (SafeArrayGetElement (*safearray, indices, &element) != S_OK)
5006                                         return 1;
5007                                 failed = ((element.vt != VT_BSTR) 
5008                                         || (VariantChangeType (&element, &element, VARIANT_NOUSEROVERRIDE, VT_I4) != S_OK) 
5009                                         || (element.lVal != 100*(i+1)+10*(j+1)+(k+1)));
5010                                 VariantClear (&element);
5011                                 if (failed)
5012                                         return 1;
5013                         }
5014                 }
5015         }
5016
5017         hr = SafeArrayDestroy (*safearray);
5018         if (hr != S_OK)
5019                 return 1;
5020
5021         /* Return a new one-dimensional array of 8 variants filled with "0" to "7" */
5022
5023         dimensions [0].lLbound = 0;
5024         dimensions [0].cElements = 8;
5025
5026         *safearray = SafeArrayCreate (VT_VARIANT, 1, dimensions);
5027
5028         for (i= dimensions [0].lLbound; i< (dimensions [0].lLbound + dimensions [0].cElements); i++) {
5029                 VARIANT vOut;
5030                 VariantInit (&vOut);
5031                 vOut.vt = VT_BSTR;
5032                 _ltow (i,buffer,10);
5033                 vOut.bstrVal = SysAllocString (buffer);
5034                 indices [0] = i;
5035                 if ((hr = SafeArrayPutElement (*safearray, indices, &vOut)) != S_OK) {
5036                         VariantClear (&vOut);
5037                         SafeArrayDestroy (*safearray);
5038                         return hr;
5039                 }
5040                 VariantClear (&vOut);
5041         }
5042         return hr;
5043 }
5044
5045 LIBTEST_API int STDCALL 
5046 mono_test_marshal_safearray_in_out_byref_1dim_vt_i4 (SAFEARRAY** safearray)
5047 {
5048         /* Check that the input array is what is expected and change it so the caller can check */
5049         /* correct marshalling back to managed code                                             */
5050
5051         UINT dim;
5052         long lbound1, ubound1;
5053         long i, failed;
5054         HRESULT hr = S_OK;
5055         long indices [1];
5056         VARIANT element;
5057         
5058         VariantInit (&element);
5059
5060         /* Check that in array is one dimensional and contains the expected value */
5061
5062         dim = SafeArrayGetDim (*safearray);
5063         if (dim != 1)
5064                 return 1;
5065
5066         SafeArrayGetLBound (*safearray, 1, &lbound1);
5067         SafeArrayGetUBound (*safearray, 1, &ubound1);
5068
5069         ubound1 = 1;
5070         if ((lbound1 != 0) || (ubound1 != 1))
5071                 return 1;
5072         ubound1 = 0;
5073
5074         for (i= lbound1; i <= ubound1; i++) {
5075                 indices [0] = i;
5076                 if (SafeArrayGetElement (*safearray, indices, &element) != S_OK)
5077                         return 1;
5078                 failed = (element.vt != VT_I4) || (element.lVal != i+1);
5079                 VariantClear (&element);
5080                 if (failed)
5081                         return 1;
5082         }
5083
5084         /* Change one of the elements of the array to verify that [out] parameter is marshalled back to the managed side */
5085
5086         indices [0] = 0;
5087         element.vt = VT_I4;
5088         element.lVal = -1;
5089         SafeArrayPutElement (*safearray, indices, &element);
5090         VariantClear (&element);
5091
5092         return hr;
5093 }
5094
5095 LIBTEST_API int STDCALL 
5096 mono_test_marshal_safearray_in_out_byval_1dim_vt_i4 (SAFEARRAY* safearray)
5097 {
5098         /* Check that the input array is what is expected and change it so the caller can check */
5099         /* correct marshalling back to managed code                                             */
5100
5101         UINT dim;
5102         long lbound1, ubound1;
5103         SAFEARRAYBOUND dimensions [1];
5104         long i, failed;
5105         HRESULT hr = S_OK;
5106         long indices [1];
5107         VARIANT element;
5108
5109         VariantInit (&element);
5110
5111         /* Check that in array is one dimensional and contains the expected value */
5112
5113         dim = SafeArrayGetDim (safearray);
5114         if (dim != 1)
5115                 return 1;
5116
5117         SafeArrayGetLBound (safearray, 1, &lbound1);
5118         SafeArrayGetUBound (safearray, 1, &ubound1);
5119                 
5120         if ((lbound1 != 0) || (ubound1 != 0))
5121                 return 1;
5122
5123         for (i= lbound1; i <= ubound1; i++) {
5124                 indices [0] = i;
5125                 if (SafeArrayGetElement (safearray, indices, &element) != S_OK)
5126                         return 1;
5127                 failed = (element.vt != VT_I4) || (element.lVal != i+1);
5128                 VariantClear (&element);
5129                 if (failed)
5130                         return 1;
5131         }
5132
5133         /* Change the array to verify how [out] parameter is marshalled back to the managed side */
5134
5135         /* Redimension the array */
5136         dimensions [0].lLbound = lbound1;
5137         dimensions [0].cElements = 2;
5138         hr = SafeArrayRedim(safearray, dimensions);
5139
5140         indices [0] = 0;
5141         element.vt = VT_I4;
5142         element.lVal = 12345;
5143         SafeArrayPutElement (safearray, indices, &element);
5144         VariantClear (&element);
5145
5146         indices [0] = 1;
5147         element.vt = VT_I4;
5148         element.lVal = -12345;
5149         SafeArrayPutElement (safearray, indices, &element);
5150         VariantClear (&element);
5151
5152         return hr;
5153 }
5154
5155 LIBTEST_API int STDCALL 
5156 mono_test_marshal_safearray_in_out_byval_3dim_vt_bstr (SAFEARRAY* safearray)
5157 {
5158         /* Check that the input array is what is expected and change it so the caller can check */
5159         /* correct marshalling back to managed code                                             */
5160
5161         UINT dim;
5162         long lbound1, ubound1, lbound2, ubound2, lbound3, ubound3;
5163         long i, j, k, failed;
5164         HRESULT hr = S_OK;
5165         long indices [3];
5166         VARIANT element;
5167
5168         VariantInit (&element);
5169
5170         /* Check that in array is three dimensional and contains the expected values */
5171
5172         dim = SafeArrayGetDim (safearray);
5173         if (dim != 3)
5174                 return 1;
5175
5176         SafeArrayGetLBound (safearray, 1, &lbound1);
5177         SafeArrayGetUBound (safearray, 1, &ubound1);
5178
5179         if ((lbound1 != 0) || (ubound1 != 1))
5180                 return 1;
5181
5182         SafeArrayGetLBound (safearray, 2, &lbound2);
5183         SafeArrayGetUBound (safearray, 2, &ubound2);
5184
5185         if ((lbound2 != 0) || (ubound2 != 1))
5186                 return 1;
5187
5188         SafeArrayGetLBound (safearray, 3, &lbound3);
5189         SafeArrayGetUBound (safearray, 3, &ubound3);
5190
5191         if ((lbound3 != 0) || (ubound3 != 2))
5192                 return 1;
5193
5194         for (i= lbound1; i <= ubound1; i++) {
5195                 indices [0] = i;
5196                 for (j= lbound2; j <= ubound2; j++) {
5197                         indices [1] = j;
5198                         for (k= lbound3; k <= ubound3; k++) {
5199                                 indices [2] = k;
5200                                 if (SafeArrayGetElement (safearray, indices, &element) != S_OK)
5201                                         return 1;
5202                                 failed = ((element.vt != VT_BSTR) 
5203                                         || (VariantChangeType (&element, &element, VARIANT_NOUSEROVERRIDE, VT_I4) != S_OK) 
5204                                         || (element.lVal != 100*(i+1)+10*(j+1)+(k+1)));
5205                                 VariantClear (&element);
5206                                 if (failed)
5207                                         return 1;
5208                         }
5209                 }
5210         }
5211
5212         /* Change the elements of the array to verify that [out] parameter is marshalled back to the managed side */
5213
5214         indices [0] = 1;
5215         indices [1] = 1;
5216         indices [2] = 2;
5217         element.vt = VT_I4;
5218         element.lVal = 333;
5219         SafeArrayPutElement (safearray, indices, &element);
5220         VariantClear (&element);
5221
5222         indices [0] = 1;
5223         indices [1] = 1;
5224         indices [2] = 1;
5225         element.vt = VT_I4;
5226         element.lVal = 111;
5227         SafeArrayPutElement (safearray, indices, &element);
5228         VariantClear (&element);
5229
5230         indices [0] = 0;
5231         indices [1] = 1;
5232         indices [2] = 0;
5233         element.vt = VT_BSTR;
5234         element.bstrVal = marshal_bstr_alloc("ABCDEFG");
5235         SafeArrayPutElement (safearray, indices, &element);
5236         VariantClear (&element);
5237
5238         return hr;
5239 }
5240
5241 LIBTEST_API int STDCALL 
5242 mono_test_marshal_safearray_mixed(
5243                 SAFEARRAY  *safearray1,
5244                 SAFEARRAY **safearray2,
5245                 SAFEARRAY  *safearray3,
5246                 SAFEARRAY **safearray4
5247                 )
5248 {
5249         HRESULT hr = S_OK;
5250
5251         /* Initialize out parameters */
5252         *safearray2 = NULL;
5253
5254         /* array1: Check that in array is one dimensional and contains the expected value */
5255         hr = mono_test_marshal_safearray_in_out_byval_1dim_vt_i4 (safearray1);
5256
5257         /* array2: Fill in with some values to check on the managed side */
5258         if (hr == S_OK)
5259                 hr = mono_test_marshal_safearray_out_1dim_vt_bstr (safearray2);
5260
5261         /* array3: Check that in array is one dimensional and contains the expected value */
5262         if (hr == S_OK)
5263                 hr = mono_test_marshal_safearray_in_byval_1dim_vt_mixed(safearray3);
5264
5265         /* array4: Check input values and fill in with some values to check on the managed side */
5266         if (hr == S_OK)
5267                 hr = mono_test_marshal_safearray_in_out_byref_3dim_vt_bstr(safearray4);
5268
5269         return hr;
5270 }
5271
5272 #endif
5273
5274 static int call_managed_res;
5275
5276 static void
5277 call_managed (gpointer arg)
5278 {
5279         SimpleDelegate del = arg;
5280
5281         call_managed_res = del (42);
5282 }
5283
5284 LIBTEST_API int STDCALL 
5285 mono_test_marshal_thread_attach (SimpleDelegate del)
5286 {
5287 #ifdef WIN32
5288         return 43;
5289 #else
5290         int res;
5291         pthread_t t;
5292
5293         res = pthread_create (&t, NULL, (gpointer)call_managed, del);
5294         g_assert (res == 0);
5295         pthread_join (t, NULL);
5296
5297         return call_managed_res;
5298 #endif
5299 }
5300
5301 typedef int (STDCALL *Callback) (void);
5302
5303 static Callback callback;
5304
5305 LIBTEST_API void STDCALL 
5306 mono_test_marshal_set_callback (Callback cb)
5307 {
5308         callback = cb;
5309 }
5310
5311 LIBTEST_API int STDCALL 
5312 mono_test_marshal_call_callback (void)
5313 {
5314         return callback ();
5315 }
5316
5317 LIBTEST_API int STDCALL
5318 mono_test_marshal_lpstr (char *str)
5319 {
5320         return strcmp ("ABC", str);
5321 }
5322
5323 LIBTEST_API int STDCALL
5324 mono_test_marshal_lpwstr (gunichar2 *str)
5325 {
5326         char *s;
5327         int res;
5328
5329         s = g_utf16_to_utf8 (str, -1, NULL, NULL, NULL);
5330         res = strcmp ("ABC", s);
5331         g_free (s);
5332
5333         return res;
5334 }
5335
5336 LIBTEST_API char* STDCALL
5337 mono_test_marshal_return_lpstr (void)
5338 {
5339         char *res = marshal_alloc (4);
5340         strcpy (res, "XYZ");
5341         return res;
5342 }
5343
5344
5345 LIBTEST_API gunichar2* STDCALL
5346 mono_test_marshal_return_lpwstr (void)
5347 {
5348         gunichar2 *res = marshal_alloc (8);
5349         gunichar2* tmp = g_utf8_to_utf16 ("XYZ", -1, NULL, NULL, NULL);
5350
5351         memcpy (res, tmp, 8);
5352         g_free (tmp);
5353
5354         return res;
5355 }
5356
5357 typedef struct {
5358         double d;
5359 } SingleDoubleStruct;
5360
5361 LIBTEST_API SingleDoubleStruct STDCALL
5362 mono_test_marshal_return_single_double_struct (void)
5363 {
5364         SingleDoubleStruct res;
5365
5366         res.d = 3.0;
5367
5368         return res;
5369 }
5370
5371
5372 #ifndef TARGET_X86
5373
5374 LIBTEST_API int STDCALL
5375 mono_test_has_thiscall (void)
5376 {
5377         return 1;
5378 }
5379
5380 LIBTEST_API int
5381 _mono_test_native_thiscall1 (int arg)
5382 {
5383         return arg;
5384 }
5385
5386 LIBTEST_API int
5387 _mono_test_native_thiscall2 (int arg, int arg2)
5388 {
5389         return arg + (arg2^1);
5390 }
5391
5392 LIBTEST_API int
5393 _mono_test_native_thiscall3 (int arg, int arg2, int arg3)
5394 {
5395         return arg + (arg2^1) + (arg3^2);
5396 }
5397
5398 #elif defined(__GNUC__)
5399
5400 LIBTEST_API int STDCALL
5401 mono_test_has_thiscall (void)
5402 {
5403         return 1;
5404 }
5405
5406 #define def_asm_fn(name) \
5407         "\t.align 4\n" \
5408         "\t.globl _" #name "\n" \
5409         "_" #name ":\n" \
5410         "\t.globl __" #name "\n" \
5411         "__" #name ":\n"
5412
5413 asm(".text\n"
5414
5415 def_asm_fn(mono_test_native_thiscall1)
5416 "\tmovl %ecx,%eax\n"
5417 "\tret\n"
5418
5419 def_asm_fn(mono_test_native_thiscall2)
5420 "\tmovl %ecx,%eax\n"
5421 "\tmovl 4(%esp),%ecx\n"
5422 "\txorl $1,%ecx\n"
5423 "\taddl %ecx,%eax\n"
5424 "\tret $4\n"
5425
5426 def_asm_fn(mono_test_native_thiscall3)
5427 "\tmovl %ecx,%eax\n"
5428 "\tmovl 4(%esp),%ecx\n"
5429 "\txorl $1,%ecx\n"
5430 "\taddl %ecx,%eax\n"
5431 "\tmovl 8(%esp),%ecx\n"
5432 "\txorl $2,%ecx\n"
5433 "\taddl %ecx,%eax\n"
5434 "\tret $8\n"
5435
5436 );
5437
5438 #else
5439
5440 LIBTEST_API int STDCALL
5441 mono_test_has_thiscall (void)
5442 {
5443         return 0;
5444 }
5445
5446 #endif
5447