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