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