2007-03-28 Chris Toshok <toshok@ximian.com>
[mono-project.git] / mono / tests / libtest.c
blobfec1f3b4f1fbdaaafcb8528f26c3fc7483180348
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <glib.h>
5 #include <errno.h>
6 #include <time.h>
8 #ifdef WIN32
9 #include <windows.h>
10 #include "initguid.h"
11 #endif
13 #ifdef WIN32
14 #define STDCALL __stdcall
15 #else
16 #define STDCALL
17 #endif
19 #ifdef WIN32
20 extern __declspec(dllimport) __stdcall void CoTaskMemFree(void *ptr);
21 #endif
23 typedef int (STDCALL *SimpleDelegate) (int a);
25 static void marshal_free (void *ptr)
27 #ifdef WIN32
28 CoTaskMemFree (ptr);
29 #else
30 g_free (ptr);
31 #endif
34 STDCALL unsigned short*
35 test_lpwstr_marshal (unsigned short* chars, long length)
37 int i = 0;
38 unsigned short *res;
40 res = malloc (2 * (length + 1));
42 // printf("test_lpwstr_marshal()\n");
44 while ( i < length ) {
45 // printf("X|%u|\n", chars[i]);
46 res [i] = chars[i];
47 i++;
50 res [i] = 0;
52 return res;
55 typedef struct {
56 int b;
57 int a;
58 int c;
59 } union_test_1_type;
61 STDCALL int
62 mono_union_test_1 (union_test_1_type u1) {
63 // printf ("Got values %d %d %d\n", u1.b, u1.a, u1.c);
64 return u1.a + u1.b + u1.c;
67 STDCALL int
68 mono_return_int (int a) {
69 // printf ("Got value %d\n", a);
70 return a;
73 struct ss
75 int i;
78 STDCALL int
79 mono_return_int_ss (struct ss a) {
80 // printf ("Got value %d\n", a.i);
81 return a.i;
84 STDCALL struct ss
85 mono_return_ss (struct ss a) {
86 // printf ("Got value %d\n", a.i);
87 a.i++;
88 return a;
91 struct sc1
93 char c[1];
96 STDCALL struct sc1
97 mono_return_sc1 (struct sc1 a) {
98 // printf ("Got value %d\n", a.c[0]);
99 a.c[0]++;
100 return a;
104 struct sc3
106 char c[3];
109 STDCALL struct sc3
110 mono_return_sc3 (struct sc3 a) {
111 // printf ("Got values %d %d %d\n", a.c[0], a.c[1], a.c[2]);
112 a.c[0]++;
113 a.c[1] += 2;
114 a.c[2] += 3;
115 return a;
118 struct sc5
120 char c[5];
123 STDCALL struct sc5
124 mono_return_sc5 (struct sc5 a) {
125 // printf ("Got values %d %d %d %d %d\n", a.c[0], a.c[1], a.c[2], a.c[3], a.c[4]);
126 a.c[0]++;
127 a.c[1] += 2;
128 a.c[2] += 3;
129 a.c[3] += 4;
130 a.c[4] += 5;
131 return a;
134 union su
136 int i1;
137 int i2;
140 STDCALL int
141 mono_return_int_su (union su a) {
142 // printf ("Got value %d\n", a.i1);
143 return a.i1;
146 STDCALL int
147 mono_test_many_int_arguments (int a, int b, int c, int d, int e,
148 int f, int g, int h, int i, int j);
149 STDCALL short
150 mono_test_many_short_arguments (short a, short b, short c, short d, short e,
151 short f, short g, short h, short i, short j);
152 STDCALL char
153 mono_test_many_char_arguments (char a, char b, char c, char d, char e,
154 char f, char g, char h, char i, char j);
156 STDCALL int
157 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)
159 return a + b + c + d + e + f + g + h + i + j;
162 STDCALL short
163 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)
165 return a + b + c + d + e + f + g + h + i + j;
168 STDCALL char
169 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)
171 return a + b + c + d + e + f + g + h + i + j;
174 STDCALL float
175 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)
177 return a + b + c + d + e + f + g + h + i + j;
180 STDCALL double
181 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)
183 return a + b + c + d + e + f + g + h + i + j;
186 STDCALL double
187 mono_test_split_double_arguments (double a, double b, float c, double d, double e)
189 return a + b + c + d + e;
192 STDCALL int
193 mono_test_puts_static (char *s)
195 // printf ("TEST %s\n", s);
196 return 1;
199 typedef int (STDCALL *SimpleDelegate3) (int a, int b);
201 STDCALL int
202 mono_invoke_delegate (SimpleDelegate3 delegate)
204 int res;
206 // printf ("start invoke %p\n", delegate);
208 res = delegate (2, 3);
210 // printf ("end invoke\n");
212 return res;
215 STDCALL int
216 mono_test_marshal_char (short a1)
218 if (a1 == 'a')
219 return 0;
221 return 1;
224 STDCALL void
225 mono_test_marshal_char_array (gunichar2 *s)
227 const char m[] = "abcdef";
228 gunichar2* s2;
229 glong len;
231 s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
233 len = (len * 2) + 2;
234 memcpy (s, s2, len);
236 g_free (s2);
239 STDCALL int
240 mono_test_empty_pinvoke (int i)
242 return i;
245 STDCALL int
246 mono_test_marshal_bool_byref (int a, int *b, int c)
248 int res = *b;
250 *b = 1;
252 return res;
255 STDCALL int
256 mono_test_marshal_array (int *a1)
258 int i, sum = 0;
260 for (i = 0; i < 50; i++)
261 sum += a1 [i];
263 return sum;
266 STDCALL int
267 mono_test_marshal_inout_array (int *a1)
269 int i, sum = 0;
271 for (i = 0; i < 50; i++) {
272 sum += a1 [i];
273 a1 [i] = 50 - a1 [i];
276 return sum;
279 STDCALL int
280 mono_test_marshal_out_array (int *a1)
282 int i;
284 for (i = 0; i < 50; i++) {
285 a1 [i] = i;
288 return 0;
291 STDCALL int
292 mono_test_marshal_inout_nonblittable_array (gunichar2 *a1)
294 int i, sum = 0;
296 for (i = 0; i < 10; i++) {
297 a1 [i] = 'F';
300 return sum;
303 typedef struct {
304 int a;
305 int b;
306 int c;
307 const char *d;
308 gunichar2 *d2;
309 } simplestruct;
311 typedef struct {
312 double x;
313 double y;
314 } point;
316 STDCALL simplestruct
317 mono_test_return_vtype (int i)
319 simplestruct res;
320 static gunichar2 test2 [] = { 'T', 'E', 'S', 'T', '2', 0 };
322 res.a = 0;
323 res.b = 1;
324 res.c = 0;
325 res.d = "TEST";
326 res.d2 = test2;
328 return res;
331 STDCALL void
332 mono_test_delegate_struct (void)
334 // printf ("TEST\n");
337 typedef char* (STDCALL *ReturnStringDelegate) (const char *s);
339 STDCALL char *
340 mono_test_return_string (ReturnStringDelegate func)
342 char *res;
344 // printf ("mono_test_return_string\n");
346 res = func ("TEST");
347 marshal_free (res);
349 // printf ("got string: %s\n", res);
350 return g_strdup ("12345");
353 typedef int (STDCALL *RefVTypeDelegate) (int a, simplestruct *ss, int b);
355 STDCALL int
356 mono_test_ref_vtype (int a, simplestruct *ss, int b, RefVTypeDelegate func)
358 if (a == 1 && b == 2 && ss->a == 0 && ss->b == 1 && ss->c == 0 &&
359 !strcmp (ss->d, "TEST1")) {
360 ss->a = 1;
361 ss->b = 0;
362 ss->c = 1;
363 ss->d = "TEST2";
365 return func (a, ss, b);
368 return 1;
371 typedef int (STDCALL *OutVTypeDelegate) (int a, simplestruct *ss, int b);
373 STDCALL int
374 mono_test_marshal_out_struct (int a, simplestruct *ss, int b, OutVTypeDelegate func)
376 /* Check that the input pointer is ignored */
377 ss->d = (gpointer)0x12345678;
379 func (a, ss, b);
381 if (ss->a && ss->b && ss->c && !strcmp (ss->d, "TEST3"))
382 return 0;
383 else
384 return 1;
387 typedef struct {
388 int a;
389 SimpleDelegate func, func2;
390 } DelegateStruct;
392 STDCALL DelegateStruct
393 mono_test_marshal_delegate_struct (DelegateStruct ds)
395 DelegateStruct res;
397 res.a = ds.func (ds.a) + ds.func2 (ds.a);
398 res.func = ds.func;
399 res.func2 = ds.func2;
401 return res;
404 STDCALL int
405 mono_test_marshal_struct (simplestruct ss)
407 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
408 !strcmp (ss.d, "TEST"))
409 return 0;
411 return 1;
414 STDCALL int
415 mono_test_marshal_byref_struct (simplestruct *ss, int a, int b, int c, char *d)
417 gboolean res = (ss->a == a && ss->b == b && ss->c == c && strcmp (ss->d, d) == 0);
419 ss->a = !ss->a;
420 ss->b = !ss->b;
421 ss->c = !ss->c;
422 ss->d = g_strdup ("DEF");
424 return res ? 0 : 1;
427 typedef struct {
428 int a;
429 int b;
430 int c;
431 char *d;
432 unsigned char e;
433 double f;
434 unsigned char g;
435 guint64 h;
436 } simplestruct2;
438 STDCALL int
439 mono_test_marshal_struct2 (simplestruct2 ss)
441 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
442 !strcmp (ss.d, "TEST") &&
443 ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
444 return 0;
446 return 1;
449 /* on HP some of the struct should be on the stack and not in registers */
450 STDCALL int
451 mono_test_marshal_struct2_2 (int i, int j, int k, simplestruct2 ss)
453 if (i != 10 || j != 11 || k != 12)
454 return 1;
455 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
456 !strcmp (ss.d, "TEST") &&
457 ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
458 return 0;
460 return 1;
463 STDCALL int
464 mono_test_marshal_lpstruct (simplestruct *ss)
466 if (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
467 !strcmp (ss->d, "TEST"))
468 return 0;
470 return 1;
473 STDCALL int
474 mono_test_marshal_lpstruct_blittable (point *p)
476 if (p->x == 1.0 && p->y == 2.0)
477 return 0;
478 else
479 return 1;
482 STDCALL int
483 mono_test_marshal_struct_array (simplestruct2 *ss)
485 if (! (ss[0].a == 0 && ss[0].b == 1 && ss[0].c == 0 &&
486 !strcmp (ss[0].d, "TEST") &&
487 ss[0].e == 99 && ss[0].f == 1.5 && ss[0].g == 42 && ss[0].h == (guint64)123))
488 return 1;
490 if (! (ss[1].a == 0 && ss[1].b == 0 && ss[1].c == 0 &&
491 !strcmp (ss[1].d, "TEST2") &&
492 ss[1].e == 100 && ss[1].f == 2.5 && ss[1].g == 43 && ss[1].h == (guint64)124))
493 return 1;
495 return 0;
498 typedef struct long_align_struct {
499 gint32 a;
500 gint64 b;
501 gint64 c;
502 } long_align_struct;
504 STDCALL int
505 mono_test_marshal_long_align_struct_array (long_align_struct *ss)
507 return ss[0].a + ss[0].b + ss[0].c + ss[1].a + ss[1].b + ss[1].c;
510 STDCALL simplestruct2 *
511 mono_test_marshal_class (int i, int j, int k, simplestruct2 *ss, int l)
513 simplestruct2 *res;
515 if (!ss)
516 return NULL;
518 if (i != 10 || j != 11 || k != 12 || l != 14)
519 return NULL;
520 if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
521 !strcmp (ss->d, "TEST") &&
522 ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
523 return NULL;
525 res = g_new0 (simplestruct2, 1);
526 memcpy (res, ss, sizeof (simplestruct2));
527 res->d = g_strdup ("TEST");
528 return res;
531 STDCALL int
532 mono_test_marshal_byref_class (simplestruct2 **ssp)
534 simplestruct2 *ss = *ssp;
535 simplestruct2 *res;
537 if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
538 !strcmp (ss->d, "TEST") &&
539 ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
540 return 1;
542 res = g_new0 (simplestruct2, 1);
543 memcpy (res, ss, sizeof (simplestruct2));
544 res->d = g_strdup ("TEST-RES");
546 *ssp = res;
547 return 0;
550 static void *
551 get_sp (void)
553 int i;
554 void *p;
556 /* Yes, this is correct, we are only trying to determine the value of the stack here */
557 p = &i;
558 return p;
561 STDCALL int
562 reliable_delegate (int a)
564 return a;
568 * Checks whether get_sp() works as expected. It doesn't work with gcc-2.95.3 on linux.
570 static gboolean
571 is_get_sp_reliable (void)
573 void *sp1, *sp2;
575 reliable_delegate(1);
576 sp1 = get_sp();
577 reliable_delegate(1);
578 sp2 = get_sp();
579 return sp1 == sp2;
582 STDCALL int
583 mono_test_marshal_delegate (SimpleDelegate delegate)
585 void *sp1, *sp2;
587 /* Check that the delegate wrapper is stdcall */
588 delegate (2);
589 sp1 = get_sp ();
590 delegate (2);
591 sp2 = get_sp ();
592 if (is_get_sp_reliable())
593 g_assert (sp1 == sp2);
595 return delegate (2);
598 STDCALL SimpleDelegate
599 mono_test_marshal_return_delegate (SimpleDelegate delegate)
601 return delegate;
604 static STDCALL int
605 return_plus_one (int i)
607 return i + 1;
610 STDCALL SimpleDelegate
611 mono_test_marshal_return_delegate_2 ()
613 return return_plus_one;
616 typedef simplestruct (STDCALL *SimpleDelegate2) (simplestruct ss);
618 static gboolean
619 is_utf16_equals (gunichar2 *s1, const char *s2)
621 char *s;
622 int res;
624 s = g_utf16_to_utf8 (s1, -1, NULL, NULL, NULL);
625 res = strcmp (s, s2);
626 g_free (s);
628 return res == 0;
631 STDCALL int
632 mono_test_marshal_delegate2 (SimpleDelegate2 delegate)
634 simplestruct ss, res;
636 ss.a = 0;
637 ss.b = 1;
638 ss.c = 0;
639 ss.d = "TEST";
640 ss.d2 = g_utf8_to_utf16 ("TEST2", -1, NULL, NULL, NULL);
642 res = delegate (ss);
643 if (! (res.a && !res.b && res.c && !strcmp (res.d, "TEST-RES") && is_utf16_equals (res.d2, "TEST2-RES")))
644 return 1;
646 return 0;
649 typedef simplestruct* (STDCALL *SimpleDelegate4) (simplestruct *ss);
651 STDCALL int
652 mono_test_marshal_delegate4 (SimpleDelegate4 delegate)
654 simplestruct ss;
655 simplestruct *res;
657 ss.a = 0;
658 ss.b = 1;
659 ss.c = 0;
660 ss.d = "TEST";
662 /* Check argument */
663 res = delegate (&ss);
664 if (!res)
665 return 1;
667 /* Check return value */
668 if (! (!res->a && res->b && !res->c && !strcmp (res->d, "TEST")))
669 return 2;
671 /* Check NULL argument and NULL result */
672 res = delegate (NULL);
673 if (res)
674 return 3;
676 return 0;
679 typedef int (STDCALL *SimpleDelegate5) (simplestruct **ss);
681 STDCALL int
682 mono_test_marshal_delegate5 (SimpleDelegate5 delegate)
684 simplestruct ss;
685 int res;
686 simplestruct *ptr;
688 ss.a = 0;
689 ss.b = 1;
690 ss.c = 0;
691 ss.d = "TEST";
693 ptr = &ss;
695 res = delegate (&ptr);
696 if (res != 0)
697 return 1;
699 if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
700 return 2;
702 return 0;
705 STDCALL int
706 mono_test_marshal_delegate6 (SimpleDelegate5 delegate)
708 int res;
710 res = delegate (NULL);
712 return 0;
715 typedef int (STDCALL *SimpleDelegate7) (simplestruct **ss);
717 STDCALL int
718 mono_test_marshal_delegate7 (SimpleDelegate7 delegate)
720 int res;
721 simplestruct *ptr;
723 /* Check that the input pointer is ignored */
724 ptr = (gpointer)0x12345678;
726 res = delegate (&ptr);
727 if (res != 0)
728 return 1;
730 if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
731 return 2;
733 return 0;
736 typedef int (STDCALL *InOutByvalClassDelegate) (simplestruct *ss);
738 STDCALL int
739 mono_test_marshal_inout_byval_class_delegate (InOutByvalClassDelegate delegate)
741 int res;
742 simplestruct ss;
744 ss.a = FALSE;
745 ss.b = TRUE;
746 ss.c = FALSE;
747 ss.d = g_strdup_printf ("%s", "FOO");
749 res = delegate (&ss);
750 if (res != 0)
751 return 1;
753 if (!(ss.a && !ss.b && ss.c && !strcmp (ss.d, "RES")))
754 return 2;
756 return 0;
759 typedef int (STDCALL *SimpleDelegate8) (gunichar2 *s);
761 STDCALL int
762 mono_test_marshal_delegate8 (SimpleDelegate8 delegate, gunichar2 *s)
764 return delegate (s);
767 typedef int (STDCALL *return_int_fnt) (int i);
768 typedef int (STDCALL *SimpleDelegate9) (return_int_fnt d);
770 STDCALL int
771 mono_test_marshal_delegate9 (SimpleDelegate9 delegate, gpointer ftn)
773 return delegate (ftn);
776 STDCALL static int
777 return_self (int i)
779 return i;
782 STDCALL int
783 mono_test_marshal_delegate10 (SimpleDelegate9 delegate)
785 return delegate (return_self);
788 typedef int (STDCALL *PrimitiveByrefDelegate) (int *i);
790 STDCALL int
791 mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate delegate)
793 int i = 1;
795 int res = delegate (&i);
796 if (res != 0)
797 return res;
799 if (i != 2)
800 return 2;
802 return 0;
805 typedef int (STDCALL *return_int_delegate) (int i);
807 typedef return_int_delegate (STDCALL *ReturnDelegateDelegate) (void);
809 STDCALL int
810 mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d)
812 return (d ()) (55);
815 STDCALL int
816 mono_test_marshal_stringbuilder (char *s, int n)
818 const char m[] = "This is my message. Isn't it nice?";
820 if (strcmp (s, "ABCD") != 0)
821 return 1;
822 strncpy(s, m, n);
823 s [n] = '\0';
824 return 0;
827 STDCALL int
828 mono_test_marshal_stringbuilder_default (char *s, int n)
830 const char m[] = "This is my message. Isn't it nice?";
832 strncpy(s, m, n);
833 s [n] = '\0';
834 return 0;
837 STDCALL int
838 mono_test_marshal_stringbuilder_unicode (gunichar2 *s, int n)
840 const char m[] = "This is my message. Isn't it nice?";
841 gunichar2* s2;
842 glong len;
844 s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
846 len = (len * 2) + 2;
847 if (len > (n * 2))
848 len = n * 2;
849 memcpy (s, s2, len);
851 g_free (s2);
853 return 0;
856 typedef struct {
857 #ifndef __GNUC__
858 char a;
859 #endif
860 } EmptyStruct;
862 STDCALL int
863 mono_test_marshal_empty_string_array (char **array)
865 return (array == NULL) ? 0 : 1;
868 STDCALL int
869 mono_test_marshal_string_array (char **array)
871 if (strcmp (array [0], "ABC"))
872 return 1;
873 if (strcmp (array [1], "DEF"))
874 return 2;
876 if (array [2] != NULL)
877 return 3;
879 return 0;
882 STDCALL int
883 mono_test_marshal_byref_string_array (char ***array)
885 if (*array == NULL)
886 return 0;
888 if (strcmp ((*array) [0], "Alpha"))
889 return 2;
890 if (strcmp ((*array) [1], "Beta"))
891 return 2;
892 if (strcmp ((*array) [2], "Gamma"))
893 return 2;
895 return 1;
898 STDCALL int
899 mono_test_marshal_stringbuilder_array (char **array)
901 if (strcmp (array [0], "ABC"))
902 return 1;
903 if (strcmp (array [1], "DEF"))
904 return 2;
906 strcpy (array [0], "DEF");
907 strcpy (array [1], "ABC");
909 return 0;
912 STDCALL int
913 mono_test_marshal_unicode_string_array (gunichar2 **array, char **array2)
915 GError *error = NULL;
916 char *s;
918 s = g_utf16_to_utf8 (array [0], -1, NULL, NULL, &error);
919 if (strcmp (s, "ABC")) {
920 g_free (s);
921 return 1;
923 else
924 g_free (s);
926 s = g_utf16_to_utf8 (array [1], -1, NULL, NULL, &error);
927 if (strcmp (s, "DEF")) {
928 g_free (s);
929 return 2;
931 else
932 g_free (s);
934 if (strcmp (array2 [0], "ABC"))
935 return 3;
937 if (strcmp (array2 [1], "DEF"))
938 return 4;
940 return 0;
943 /* this does not work on Redhat gcc 2.96 */
944 STDCALL int
945 mono_test_empty_struct (int a, EmptyStruct es, int b)
947 // printf ("mono_test_empty_struct %d %d\n", a, b);
949 // Intel icc on ia64 passes 'es' in 2 registers
950 #if defined(__ia64) && defined(__INTEL_COMPILER)
951 return 0;
952 #else
953 if (a == 1 && b == 2)
954 return 0;
955 return 1;
956 #endif
959 typedef struct {
960 char a[100];
961 } ByValStrStruct;
963 STDCALL ByValStrStruct *
964 mono_test_byvalstr_gen (void)
966 ByValStrStruct *ret;
968 ret = malloc(sizeof(ByValStrStruct));
969 memset(ret, 'a', sizeof(ByValStrStruct)-1);
970 ret->a[sizeof(ByValStrStruct)-1] = 0;
972 return ret;
975 STDCALL int
976 mono_test_byvalstr_check (ByValStrStruct* data, char* correctString)
978 int ret;
980 ret = strcmp(data->a, correctString);
981 // printf ("T1: %s\n", data->a);
982 // printf ("T2: %s\n", correctString);
984 marshal_free (data);
985 return (ret != 0);
988 typedef struct {
989 guint16 a[4];
990 int flag;
991 } ByValStrStruct_Unicode;
993 STDCALL int
994 mono_test_byvalstr_check_unicode (ByValStrStruct_Unicode *ref, int test)
996 if (ref->flag != 0x1234abcd){
997 printf ("overwritten data");
998 return 1;
1001 if (test == 1 || test == 3){
1002 if (ref->a [0] != '1' ||
1003 ref->a [1] != '2' ||
1004 ref->a [2] != '3')
1005 return 1;
1006 return 0;
1008 if (test == 2){
1009 if (ref->a [0] != '1' ||
1010 ref->a [1] != '2')
1011 return 1;
1012 return 0;
1014 return 10;
1017 STDCALL int
1018 NameManglingAnsi (char *data)
1020 return data [0] + data [1] + data [2];
1023 STDCALL int
1024 NameManglingAnsiA (char *data)
1026 g_assert_not_reached ();
1029 STDCALL int
1030 NameManglingAnsiW (char *data)
1032 g_assert_not_reached ();
1035 STDCALL int
1036 NameManglingAnsi2A (char *data)
1038 return data [0] + data [1] + data [2];
1041 STDCALL int
1042 NameManglingAnsi2W (char *data)
1044 g_assert_not_reached ();
1047 STDCALL int
1048 NameManglingUnicode (char *data)
1050 g_assert_not_reached ();
1053 STDCALL int
1054 NameManglingUnicodeW (gunichar2 *data)
1056 return data [0] + data [1] + data [2];
1059 STDCALL int
1060 NameManglingUnicode2 (gunichar2 *data)
1062 return data [0] + data [1] + data [2];
1065 STDCALL int
1066 NameManglingAutoW (char *data)
1068 #ifdef WIN32
1069 return (data [0] + data [1] + data [2]) == 131 ? 0 : 1;
1070 #else
1071 g_assert_not_reached ();
1072 #endif
1075 STDCALL int
1076 NameManglingAuto (char *data)
1078 #ifndef WIN32
1079 return (data [0] + data [1] + data [2]) == 198 ? 0 : 1;
1080 #else
1081 g_assert_not_reached ();
1082 #endif
1085 typedef int (STDCALL *intcharFunc)(const char*);
1087 STDCALL void
1088 callFunction (intcharFunc f)
1090 f ("ABC");
1093 typedef struct {
1094 const char* str;
1095 int i;
1096 } SimpleObj;
1098 STDCALL int
1099 class_marshal_test0 (SimpleObj *obj1)
1101 // printf ("class_marshal_test0 %s %d\n", obj1->str, obj1->i);
1103 if (strcmp(obj1->str, "T1"))
1104 return -1;
1105 if (obj1->i != 4)
1106 return -2;
1108 return 0;
1111 STDCALL int
1112 class_marshal_test4 (SimpleObj *obj1)
1114 if (obj1)
1115 return -1;
1117 return 0;
1120 STDCALL void
1121 class_marshal_test1 (SimpleObj **obj1)
1123 SimpleObj *res = malloc (sizeof (SimpleObj));
1125 res->str = g_strdup ("ABC");
1126 res->i = 5;
1128 *obj1 = res;
1131 STDCALL int
1132 class_marshal_test2 (SimpleObj **obj1)
1134 // printf ("class_marshal_test2 %s %d\n", (*obj1)->str, (*obj1)->i);
1136 if (strcmp((*obj1)->str, "ABC"))
1137 return -1;
1138 if ((*obj1)->i != 5)
1139 return -2;
1141 return 0;
1144 STDCALL int
1145 string_marshal_test0 (char *str)
1147 if (strcmp (str, "TEST0"))
1148 return -1;
1150 return 0;
1153 STDCALL void
1154 string_marshal_test1 (const char **str)
1156 *str = "TEST1";
1159 STDCALL int
1160 string_marshal_test2 (char **str)
1162 // printf ("string_marshal_test2 %s\n", *str);
1164 if (strcmp (*str, "TEST1"))
1165 return -1;
1167 return 0;
1170 STDCALL int
1171 string_marshal_test3 (char *str)
1173 if (str)
1174 return -1;
1176 return 0;
1179 typedef struct {
1180 int a;
1181 int b;
1182 } BlittableClass;
1184 STDCALL BlittableClass*
1185 TestBlittableClass (BlittableClass *vl)
1187 BlittableClass *res;
1189 // printf ("TestBlittableClass %d %d\n", vl->a, vl->b);
1191 if (vl) {
1192 vl->a++;
1193 vl->b++;
1195 res = g_new0 (BlittableClass, 1);
1196 memcpy (res, vl, sizeof (BlittableClass));
1197 } else {
1198 res = g_new0 (BlittableClass, 1);
1199 res->a = 42;
1200 res->b = 43;
1203 return res;
1206 typedef struct OSVERSIONINFO_STRUCT
1208 int a;
1209 int b;
1210 } OSVERSIONINFO_STRUCT;
1212 STDCALL int
1213 MyGetVersionEx (OSVERSIONINFO_STRUCT *osvi)
1216 // printf ("GOT %d %d\n", osvi->a, osvi->b);
1218 osvi->a += 1;
1219 osvi->b += 1;
1221 return osvi->a + osvi->b;
1224 STDCALL int
1225 BugGetVersionEx (int a, int b, int c, int d, int e, int f, int g, int h, OSVERSIONINFO_STRUCT *osvi)
1228 // printf ("GOT %d %d\n", osvi->a, osvi->b);
1230 osvi->a += 1;
1231 osvi->b += 1;
1233 return osvi->a + osvi->b;
1236 STDCALL int
1237 mono_test_marshal_point (point pt)
1239 // printf("point %g %g\n", pt.x, pt.y);
1240 if (pt.x == 1.25 && pt.y == 3.5)
1241 return 0;
1243 return 1;
1246 typedef struct {
1247 int x;
1248 double y;
1249 } mixed_point;
1251 STDCALL int
1252 mono_test_marshal_mixed_point (mixed_point pt)
1254 // printf("mixed point %d %g\n", pt.x, pt.y);
1255 if (pt.x == 5 && pt.y == 6.75)
1256 return 0;
1258 return 1;
1261 STDCALL int
1262 mono_test_marshal_mixed_point_2 (mixed_point *pt)
1264 if (pt->x != 5 || pt->y != 6.75)
1265 return 1;
1267 pt->x = 10;
1268 pt->y = 12.35;
1270 return 0;
1273 STDCALL int
1274 marshal_test_ref_bool(int i, char *b1, short *b2, int *b3)
1276 int res = 1;
1277 if (*b1 != 0 && *b1 != 1)
1278 return 1;
1279 if (*b2 != 0 && *b2 != -1) /* variant_bool */
1280 return 1;
1281 if (*b3 != 0 && *b3 != 1)
1282 return 1;
1283 if (i == ((*b1 << 2) | (-*b2 << 1) | *b3))
1284 res = 0;
1285 *b1 = !*b1;
1286 *b2 = ~*b2;
1287 *b3 = !*b3;
1288 return res;
1291 struct BoolStruct
1293 int i;
1294 char b1;
1295 short b2; /* variant_bool */
1296 int b3;
1299 STDCALL int
1300 marshal_test_bool_struct(struct BoolStruct *s)
1302 int res = 1;
1303 if (s->b1 != 0 && s->b1 != 1)
1304 return 1;
1305 if (s->b2 != 0 && s->b2 != -1)
1306 return 1;
1307 if (s->b3 != 0 && s->b3 != 1)
1308 return 1;
1309 if (s->i == ((s->b1 << 2) | (-s->b2 << 1) | s->b3))
1310 res = 0;
1311 s->b1 = !s->b1;
1312 s->b2 = ~s->b2;
1313 s->b3 = !s->b3;
1314 return res;
1317 STDCALL void
1318 mono_test_last_error (int err)
1320 #ifdef WIN32
1321 SetLastError (err);
1322 #else
1323 errno = err;
1324 #endif
1327 STDCALL int
1328 mono_test_asany (void *ptr, int what)
1330 switch (what) {
1331 case 1:
1332 return (*(int*)ptr == 5) ? 0 : 1;
1333 case 2:
1334 return strcmp (ptr, "ABC") == 0 ? 0 : 1;
1335 case 3: {
1336 simplestruct2 ss = *(simplestruct2*)ptr;
1338 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
1339 !strcmp (ss.d, "TEST") &&
1340 ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
1341 return 0;
1342 else
1343 return 1;
1345 case 4: {
1346 GError *error = NULL;
1347 char *s;
1349 s = g_utf16_to_utf8 (ptr, -1, NULL, NULL, &error);
1350 if (!strcmp (s, "ABC")) {
1351 g_free (s);
1352 return 0;
1354 else {
1355 g_free (s);
1356 return 1;
1359 default:
1360 g_assert_not_reached ();
1363 return 1;
1366 typedef struct
1368 int i;
1369 int j;
1370 int k;
1371 char *s;
1372 } AsAnyStruct;
1374 STDCALL int
1375 mono_test_marshal_asany_inout (void* ptr)
1377 AsAnyStruct* asAny = ptr;
1378 int res = asAny->i + asAny->j + asAny->k;
1380 asAny->i = 10;
1381 asAny->j = 20;
1382 asAny->k = 30;
1383 asAny->s = 0;
1385 return res;
1389 * AMD64 marshalling tests.
1392 typedef struct amd64_struct1 {
1393 int i;
1394 int j;
1395 int k;
1396 int l;
1397 } amd64_struct1;
1399 STDCALL amd64_struct1
1400 mono_test_marshal_amd64_pass_return_struct1 (amd64_struct1 s)
1402 s.i ++;
1403 s.j ++;
1404 s.k ++;
1405 s.l ++;
1407 return s;
1410 typedef struct amd64_struct2 {
1411 int i;
1412 int j;
1413 } amd64_struct2;
1415 STDCALL amd64_struct2
1416 mono_test_marshal_amd64_pass_return_struct2 (amd64_struct2 s)
1418 s.i ++;
1419 s.j ++;
1421 return s;
1424 typedef struct amd64_struct3 {
1425 int i;
1426 } amd64_struct3;
1428 STDCALL amd64_struct3
1429 mono_test_marshal_amd64_pass_return_struct3 (amd64_struct3 s)
1431 s.i ++;
1433 return s;
1436 typedef struct amd64_struct4 {
1437 double d1, d2;
1438 } amd64_struct4;
1440 STDCALL amd64_struct4
1441 mono_test_marshal_amd64_pass_return_struct4 (amd64_struct4 s)
1443 s.d1 ++;
1444 s.d2 ++;
1446 return s;
1450 * IA64 marshalling tests.
1452 typedef struct test_struct5 {
1453 float d1, d2;
1454 } test_struct5;
1456 STDCALL test_struct5
1457 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, test_struct5 s, double d3, double d4)
1459 s.d1 += d1 + d2;
1460 s.d2 += d3 + d4;
1462 return s;
1465 typedef struct test_struct6 {
1466 double d1, d2;
1467 } test_struct6;
1469 STDCALL test_struct6
1470 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, test_struct6 s, double d3, double d4)
1472 s.d1 += d1 + d2;
1473 s.d2 += d3 + d4;
1475 return s;
1478 static guint32 custom_res [2];
1480 STDCALL void*
1481 mono_test_marshal_pass_return_custom (int i, guint32 *ptr, int j)
1483 /* ptr will be freed by CleanupNative, so make a copy */
1484 custom_res [0] = 0; /* not allocated by AllocHGlobal */
1485 custom_res [1] = ptr [1];
1487 return &custom_res;
1490 STDCALL int
1491 mono_test_marshal_pass_out_custom (int i, guint32 **ptr, int j)
1493 custom_res [0] = 0;
1494 custom_res [1] = i + j + 10;
1496 *ptr = custom_res;
1498 return 0;
1501 STDCALL int
1502 mono_test_marshal_pass_inout_custom (int i, guint32 *ptr, int j)
1504 ptr [0] = 0;
1505 ptr [1] = i + ptr [1] + j;
1507 return 0;
1510 STDCALL int
1511 mono_test_marshal_pass_out_byval_custom (int i, guint32 *ptr, int j)
1513 return ptr == NULL ? 0 : 1;
1516 STDCALL int
1517 mono_test_marshal_pass_byref_custom (int i, guint32 **ptr, int j)
1519 (*ptr)[1] += i + j;
1521 return 0;
1524 STDCALL void*
1525 mono_test_marshal_pass_return_custom2 (int i, guint32 *ptr, int j)
1527 g_assert_not_reached ();
1529 return NULL;
1532 STDCALL void*
1533 mono_test_marshal_pass_return_custom_null (int i, guint32 *ptr, int j)
1535 g_assert (ptr == NULL);
1537 return NULL;
1540 typedef void *(STDCALL *PassReturnPtrDelegate) (void *ptr);
1542 STDCALL int
1543 mono_test_marshal_pass_return_custom_in_delegate (PassReturnPtrDelegate del)
1545 guint32 buf [2];
1546 guint32 res;
1547 guint32 *ptr;
1549 buf [0] = 0;
1550 buf [1] = 10;
1552 ptr = del (&buf);
1554 res = ptr [1];
1556 #ifdef WIN32
1557 /* FIXME: Freed with FreeHGlobal */
1558 #else
1559 g_free (ptr);
1560 #endif
1562 return res;
1565 STDCALL int
1566 mono_test_marshal_pass_return_custom_null_in_delegate (PassReturnPtrDelegate del)
1568 void *ptr = del (NULL);
1570 return (ptr == NULL) ? 15 : 0;
1573 typedef int (STDCALL *ReturnEnumDelegate) (int e);
1575 STDCALL int
1576 mono_test_marshal_return_enum_delegate (ReturnEnumDelegate func)
1578 return func (1);
1581 typedef struct {
1582 int a, b, c;
1583 gint64 d;
1584 } BlittableStruct;
1586 typedef BlittableStruct (STDCALL *SimpleDelegate10) (BlittableStruct ss);
1588 STDCALL int
1589 mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 delegate)
1591 BlittableStruct ss, res;
1593 ss.a = 1;
1594 ss.b = 2;
1595 ss.c = 3;
1596 ss.d = 55;
1598 res = delegate (ss);
1599 if (! ((res.a == -1) && (res.b == -2) && (res.c == -3) && (res.d == -55)))
1600 return 1;
1602 return 0;
1605 STDCALL int
1606 mono_test_stdcall_name_mangling (int a, int b, int c)
1608 return a + b + c;
1612 * PASSING AND RETURNING SMALL STRUCTURES FROM DELEGATES TESTS
1615 typedef struct {
1616 int i;
1617 } SmallStruct1;
1619 typedef SmallStruct1 (STDCALL *SmallStructDelegate1) (SmallStruct1 ss);
1621 STDCALL int
1622 mono_test_marshal_small_struct_delegate1 (SmallStructDelegate1 delegate)
1624 SmallStruct1 ss, res;
1626 ss.i = 1;
1628 res = delegate (ss);
1629 if (! (res.i == -1))
1630 return 1;
1632 return 0;
1635 typedef struct {
1636 gint16 i, j;
1637 } SmallStruct2;
1639 typedef SmallStruct2 (STDCALL *SmallStructDelegate2) (SmallStruct2 ss);
1641 STDCALL int
1642 mono_test_marshal_small_struct_delegate2 (SmallStructDelegate2 delegate)
1644 SmallStruct2 ss, res;
1646 ss.i = 2;
1647 ss.j = 3;
1649 res = delegate (ss);
1650 if (! ((res.i == -2) && (res.j == -3)))
1651 return 1;
1653 return 0;
1656 typedef struct {
1657 gint16 i;
1658 gint8 j;
1659 } SmallStruct3;
1661 typedef SmallStruct3 (STDCALL *SmallStructDelegate3) (SmallStruct3 ss);
1663 STDCALL int
1664 mono_test_marshal_small_struct_delegate3 (SmallStructDelegate3 delegate)
1666 SmallStruct3 ss, res;
1668 ss.i = 1;
1669 ss.j = 2;
1671 res = delegate (ss);
1672 if (! ((res.i == -1) && (res.j == -2)))
1673 return 1;
1675 return 0;
1678 typedef struct {
1679 gint16 i;
1680 } SmallStruct4;
1682 typedef SmallStruct4 (STDCALL *SmallStructDelegate4) (SmallStruct4 ss);
1684 STDCALL int
1685 mono_test_marshal_small_struct_delegate4 (SmallStructDelegate4 delegate)
1687 SmallStruct4 ss, res;
1689 ss.i = 1;
1691 res = delegate (ss);
1692 if (! (res.i == -1))
1693 return 1;
1695 return 0;
1698 typedef struct {
1699 gint64 i;
1700 } SmallStruct5;
1702 typedef SmallStruct5 (STDCALL *SmallStructDelegate5) (SmallStruct5 ss);
1704 STDCALL int
1705 mono_test_marshal_small_struct_delegate5 (SmallStructDelegate5 delegate)
1707 SmallStruct5 ss, res;
1709 ss.i = 5;
1711 res = delegate (ss);
1712 if (! (res.i == -5))
1713 return 1;
1715 return 0;
1718 typedef struct {
1719 int i, j;
1720 } SmallStruct6;
1722 typedef SmallStruct6 (STDCALL *SmallStructDelegate6) (SmallStruct6 ss);
1724 STDCALL int
1725 mono_test_marshal_small_struct_delegate6 (SmallStructDelegate6 delegate)
1727 SmallStruct6 ss, res;
1729 ss.i = 1;
1730 ss.j = 2;
1732 res = delegate (ss);
1733 if (! ((res.i == -1) && (res.j == -2)))
1734 return 1;
1736 return 0;
1739 typedef struct {
1740 int i;
1741 gint16 j;
1742 } SmallStruct7;
1744 typedef SmallStruct7 (STDCALL *SmallStructDelegate7) (SmallStruct7 ss);
1746 STDCALL int
1747 mono_test_marshal_small_struct_delegate7 (SmallStructDelegate7 delegate)
1749 SmallStruct7 ss, res;
1751 ss.i = 1;
1752 ss.j = 2;
1754 res = delegate (ss);
1755 if (! ((res.i == -1) && (res.j == -2)))
1756 return 1;
1758 return 0;
1761 typedef struct {
1762 float i;
1763 } SmallStruct8;
1765 typedef SmallStruct8 (STDCALL *SmallStructDelegate8) (SmallStruct8 ss);
1767 STDCALL int
1768 mono_test_marshal_small_struct_delegate8 (SmallStructDelegate8 delegate)
1770 SmallStruct8 ss, res;
1772 ss.i = 1.0;
1774 res = delegate (ss);
1775 if (! ((res.i == -1.0)))
1776 return 1;
1778 return 0;
1781 typedef struct {
1782 double i;
1783 } SmallStruct9;
1785 typedef SmallStruct9 (STDCALL *SmallStructDelegate9) (SmallStruct9 ss);
1787 STDCALL int
1788 mono_test_marshal_small_struct_delegate9 (SmallStructDelegate9 delegate)
1790 SmallStruct9 ss, res;
1792 ss.i = 1.0;
1794 res = delegate (ss);
1795 if (! ((res.i == -1.0)))
1796 return 1;
1798 return 0;
1801 typedef struct {
1802 float i, j;
1803 } SmallStruct10;
1805 typedef SmallStruct10 (STDCALL *SmallStructDelegate10) (SmallStruct10 ss);
1807 STDCALL int
1808 mono_test_marshal_small_struct_delegate10 (SmallStructDelegate10 delegate)
1810 SmallStruct10 ss, res;
1812 ss.i = 1.0;
1813 ss.j = 2.0;
1815 res = delegate (ss);
1816 if (! ((res.i == -1.0) && (res.j == -2.0)))
1817 return 1;
1819 return 0;
1822 typedef struct {
1823 float i;
1824 int j;
1825 } SmallStruct11;
1827 typedef SmallStruct11 (STDCALL *SmallStructDelegate11) (SmallStruct11 ss);
1829 STDCALL int
1830 mono_test_marshal_small_struct_delegate11 (SmallStructDelegate11 delegate)
1832 SmallStruct11 ss, res;
1834 ss.i = 1.0;
1835 ss.j = 2;
1837 res = delegate (ss);
1838 if (! ((res.i == -1.0) && (res.j == -2)))
1839 return 1;
1841 return 0;
1844 typedef int (STDCALL *ArrayDelegate) (int i, char *j, void *arr);
1846 STDCALL int
1847 mono_test_marshal_array_delegate (void *arr, int len, ArrayDelegate del)
1849 return del (len, NULL, arr);
1852 STDCALL int
1853 mono_test_marshal_out_array_delegate (int *arr, int len, ArrayDelegate del)
1855 del (len, NULL, arr);
1857 if ((arr [0] != 1) || (arr [1] != 2))
1858 return 1;
1859 else
1860 return 0;
1863 STDCALL int
1864 mono_test_marshal_out_string_array_delegate (char **arr, int len, ArrayDelegate del)
1866 del (len, NULL, arr);
1868 if (!strcmp (arr [0], "ABC") && !strcmp (arr [1], "DEF"))
1869 return 0;
1870 else
1871 return 1;
1874 typedef int (*CdeclDelegate) (int i, int j);
1876 STDCALL int
1877 mono_test_marshal_cdecl_delegate (CdeclDelegate del)
1879 int i;
1881 for (i = 0; i < 1000; ++i)
1882 del (1, 2);
1884 return 0;
1887 typedef char** (*ReturnStringArrayDelegate) (int i);
1889 STDCALL int
1890 mono_test_marshal_return_string_array_delegate (ReturnStringArrayDelegate d)
1892 char **arr = d (2);
1893 int res;
1895 if (arr == NULL)
1896 return 3;
1898 if (strcmp (arr [0], "ABC") || strcmp (arr [1], "DEF"))
1899 res = 1;
1900 else
1901 res = 0;
1903 marshal_free (arr);
1905 return res;
1908 STDCALL int
1909 add_delegate (int i, int j)
1911 return i + j;
1914 STDCALL gpointer
1915 mono_test_marshal_return_fnptr (void)
1917 return &add_delegate;
1920 STDCALL int mono_xr (int code)
1922 printf ("codigo %x\n", code);
1923 return code + 1234;
1926 typedef struct {
1927 int a;
1928 void *handle1;
1929 void *handle2;
1930 int b;
1931 } HandleStructs;
1933 STDCALL int
1934 mono_safe_handle_struct_ref (HandleStructs *x)
1936 printf ("Dingus Ref! \n");
1937 printf ("Values: %d %d %d %d\n", x->a, x->b, x->handle1, x->handle2);
1938 if (x->a != 1234)
1939 return 1;
1940 if (x->b != 8743)
1941 return 2;
1943 if (x->handle1 != (void*) 0x7080feed)
1944 return 3;
1946 if (x->handle2 != (void*) 0x1234abcd)
1947 return 4;
1949 return 0xf00d;
1952 STDCALL int
1953 mono_safe_handle_struct (HandleStructs x)
1955 printf ("Dingus Standard! \n");
1956 printf ("Values: %d %d %d %d\n", x.a, x.b, x.handle1, x.handle2);
1957 if (x.a != 1234)
1958 return 1;
1959 if (x.b != 8743)
1960 return 2;
1962 if (x.handle1 != (void*) 0x7080feed)
1963 return 3;
1965 if (x.handle2 != (void*) 0x1234abcd)
1966 return 4;
1968 return 0xf00f;
1971 typedef struct {
1972 void *a;
1973 } TrivialHandle;
1975 STDCALL int
1976 mono_safe_handle_struct_simple (TrivialHandle x)
1978 printf ("The value is %d\n", x.a);
1979 return ((int)x.a) * 2;
1982 STDCALL int
1983 mono_safe_handle_return ()
1985 return 0x1000f00d;
1988 STDCALL void
1989 mono_safe_handle_ref (void **handle)
1991 if (*handle != 0){
1992 *handle = (void *) 0xbad;
1993 return;
1996 *handle = (void *) 0x800d;
1999 * COM INTEROP TESTS
2002 #ifdef WIN32
2004 STDCALL int
2005 mono_test_marshal_bstr_in(BSTR bstr)
2007 if (!wcscmp(bstr, L"mono_test_marshal_bstr_in"))
2008 return 0;
2009 return 1;
2012 STDCALL int
2013 mono_test_marshal_bstr_out(BSTR* bstr)
2015 *bstr = SysAllocString(L"mono_test_marshal_bstr_out");
2016 return 0;
2019 STDCALL int
2020 mono_test_marshal_bstr_in_null(BSTR bstr)
2022 if (!bstr)
2023 return 0;
2024 return 1;
2027 STDCALL int
2028 mono_test_marshal_bstr_out_null(BSTR* bstr)
2030 *bstr = NULL;
2031 return 0;
2034 STDCALL int
2035 mono_test_marshal_variant_in_sbyte(VARIANT variant)
2037 if (variant.vt == VT_I1 && variant.cVal == 100)
2038 return 0;
2039 return 1;
2042 STDCALL int
2043 mono_test_marshal_variant_in_byte(VARIANT variant)
2045 if (variant.vt == VT_UI1 && variant.bVal == 100)
2046 return 0;
2047 return 1;
2050 STDCALL int
2051 mono_test_marshal_variant_in_short(VARIANT variant)
2053 if (variant.vt == VT_I2 && variant.iVal == 314)
2054 return 0;
2055 return 1;
2058 STDCALL int
2059 mono_test_marshal_variant_in_ushort(VARIANT variant)
2061 if (variant.vt == VT_UI2 && variant.uiVal == 314)
2062 return 0;
2063 return 1;
2066 STDCALL int
2067 mono_test_marshal_variant_in_int(VARIANT variant)
2069 if (variant.vt == VT_I4 && variant.lVal == 314)
2070 return 0;
2071 return 1;
2074 STDCALL int
2075 mono_test_marshal_variant_in_uint(VARIANT variant)
2077 if (variant.vt == VT_UI4 && variant.ulVal == 314)
2078 return 0;
2079 return 1;
2082 STDCALL int
2083 mono_test_marshal_variant_in_long(VARIANT variant)
2085 if (variant.vt == VT_I8 && variant.llVal == 314)
2086 return 0;
2087 return 1;
2090 STDCALL int
2091 mono_test_marshal_variant_in_ulong(VARIANT variant)
2093 if (variant.vt == VT_UI8 && variant.ullVal == 314)
2094 return 0;
2095 return 1;
2098 STDCALL int
2099 mono_test_marshal_variant_in_float(VARIANT variant)
2101 if (variant.vt == VT_R4 && (variant.fltVal - 3.14)/3.14 < .001)
2102 return 0;
2103 return 1;
2106 STDCALL int
2107 mono_test_marshal_variant_in_double(VARIANT variant)
2109 if (variant.vt == VT_R8 && (variant.dblVal - 3.14)/3.14 < .001)
2110 return 0;
2111 return 1;
2114 STDCALL int
2115 mono_test_marshal_variant_in_bstr(VARIANT variant)
2117 if (variant.vt == VT_BSTR && !wcscmp(variant.bstrVal, L"PI"))
2118 return 0;
2119 return 1;
2122 STDCALL int
2123 mono_test_marshal_variant_in_bool_true (VARIANT variant)
2125 if (variant.vt == VT_BOOL && variant.boolVal == VARIANT_TRUE)
2126 return 0;
2127 return 1;
2130 STDCALL int
2131 mono_test_marshal_variant_in_bool_false (VARIANT variant)
2133 if (variant.vt == VT_BOOL && variant.boolVal == VARIANT_FALSE)
2134 return 0;
2135 return 1;
2138 STDCALL int
2139 mono_test_marshal_variant_out_sbyte(VARIANT* variant)
2141 variant->vt = VT_I1;
2142 variant->cVal = 100;
2144 return 0;
2147 STDCALL int
2148 mono_test_marshal_variant_out_byte(VARIANT* variant)
2150 variant->vt = VT_UI1;
2151 variant->bVal = 100;
2153 return 0;
2156 STDCALL int
2157 mono_test_marshal_variant_out_short(VARIANT* variant)
2159 variant->vt = VT_I2;
2160 variant->iVal = 314;
2162 return 0;
2165 STDCALL int
2166 mono_test_marshal_variant_out_ushort(VARIANT* variant)
2168 variant->vt = VT_UI2;
2169 variant->uiVal = 314;
2171 return 0;
2174 STDCALL int
2175 mono_test_marshal_variant_out_int(VARIANT* variant)
2177 variant->vt = VT_I4;
2178 variant->lVal = 314;
2180 return 0;
2183 STDCALL int
2184 mono_test_marshal_variant_out_uint(VARIANT* variant)
2186 variant->vt = VT_UI4;
2187 variant->ulVal = 314;
2189 return 0;
2192 STDCALL int
2193 mono_test_marshal_variant_out_long(VARIANT* variant)
2195 variant->vt = VT_I8;
2196 variant->llVal = 314;
2198 return 0;
2201 STDCALL int
2202 mono_test_marshal_variant_out_ulong(VARIANT* variant)
2204 variant->vt = VT_UI8;
2205 variant->ullVal = 314;
2207 return 0;
2210 STDCALL int
2211 mono_test_marshal_variant_out_float(VARIANT* variant)
2213 variant->vt = VT_R4;
2214 variant->fltVal = 3.14;
2216 return 0;
2219 STDCALL int
2220 mono_test_marshal_variant_out_double(VARIANT* variant)
2222 variant->vt = VT_R8;
2223 variant->dblVal = 3.14;
2225 return 0;
2228 STDCALL int
2229 mono_test_marshal_variant_out_bstr(VARIANT* variant)
2231 variant->vt = VT_BSTR;
2232 variant->bstrVal = SysAllocString(L"PI");
2234 return 0;
2237 STDCALL int
2238 mono_test_marshal_variant_out_bool_true (VARIANT* variant)
2240 variant->vt = VT_BOOL;
2241 variant->boolVal = VARIANT_TRUE;
2243 return 0;
2246 STDCALL int
2247 mono_test_marshal_variant_out_bool_false (VARIANT* variant)
2249 variant->vt = VT_BOOL;
2250 variant->boolVal = VARIANT_FALSE;
2252 return 0;
2255 typedef int (STDCALL *VarFunc) (int vt, VARIANT variant);
2256 typedef int (STDCALL *VarRefFunc) (int vt, VARIANT* variant);
2258 STDCALL int
2259 mono_test_marshal_variant_in_sbyte_unmanaged(VarFunc func)
2261 VARIANT vt;
2262 vt.vt = VT_I1;
2263 vt.cVal = -100;
2264 return func (VT_I1, vt);
2267 STDCALL int
2268 mono_test_marshal_variant_in_byte_unmanaged(VarFunc func)
2270 VARIANT vt;
2271 vt.vt = VT_UI1;
2272 vt.bVal = 100;
2273 return func (VT_UI1, vt);
2276 STDCALL int
2277 mono_test_marshal_variant_in_short_unmanaged(VarFunc func)
2279 VARIANT vt;
2280 vt.vt = VT_I2;
2281 vt.iVal = -100;
2282 return func (VT_I2, vt);
2285 STDCALL int
2286 mono_test_marshal_variant_in_ushort_unmanaged(VarFunc func)
2288 VARIANT vt;
2289 vt.vt = VT_UI2;
2290 vt.uiVal = 100;
2291 return func (VT_UI2, vt);
2294 STDCALL int
2295 mono_test_marshal_variant_in_int_unmanaged(VarFunc func)
2297 VARIANT vt;
2298 vt.vt = VT_I4;
2299 vt.lVal = -100;
2300 return func (VT_I4, vt);
2303 STDCALL int
2304 mono_test_marshal_variant_in_uint_unmanaged(VarFunc func)
2306 VARIANT vt;
2307 vt.vt = VT_UI4;
2308 vt.ulVal = 100;
2309 return func (VT_UI4, vt);
2312 STDCALL int
2313 mono_test_marshal_variant_in_long_unmanaged(VarFunc func)
2315 VARIANT vt;
2316 vt.vt = VT_I8;
2317 vt.llVal = -100;
2318 return func (VT_I8, vt);
2321 STDCALL int
2322 mono_test_marshal_variant_in_ulong_unmanaged(VarFunc func)
2324 VARIANT vt;
2325 vt.vt = VT_UI8;
2326 vt.ullVal = 100;
2327 return func (VT_UI8, vt);
2330 STDCALL int
2331 mono_test_marshal_variant_in_float_unmanaged(VarFunc func)
2333 VARIANT vt;
2334 vt.vt = VT_R4;
2335 vt.fltVal = 3.14;
2336 return func (VT_R4, vt);
2339 STDCALL int
2340 mono_test_marshal_variant_in_double_unmanaged(VarFunc func)
2342 VARIANT vt;
2343 vt.vt = VT_R8;
2344 vt.dblVal = 3.14;
2345 return func (VT_R8, vt);
2348 STDCALL int
2349 mono_test_marshal_variant_in_bstr_unmanaged(VarFunc func)
2351 VARIANT vt;
2352 vt.vt = VT_BSTR;
2353 vt.bstrVal = SysAllocString(L"PI");
2354 return func (VT_BSTR, vt);
2357 STDCALL int
2358 mono_test_marshal_variant_in_bool_true_unmanaged(VarFunc func)
2360 VARIANT vt;
2361 vt.vt = VT_BOOL;
2362 vt.boolVal = VARIANT_TRUE;
2363 return func (VT_BOOL, vt);
2366 STDCALL int
2367 mono_test_marshal_variant_in_bool_false_unmanaged(VarFunc func)
2369 VARIANT vt;
2370 vt.vt = VT_BOOL;
2371 vt.boolVal = VARIANT_FALSE;
2372 return func (VT_BOOL, vt);
2375 STDCALL int
2376 mono_test_marshal_variant_out_sbyte_unmanaged(VarRefFunc func)
2378 VARIANT vt;
2379 VariantInit (&vt);
2380 func (VT_I1, &vt);
2381 if (vt.vt == VT_I1 && vt.cVal == -100)
2382 return 0;
2383 return 1;
2386 STDCALL int
2387 mono_test_marshal_variant_out_byte_unmanaged(VarRefFunc func)
2389 VARIANT vt;
2390 VariantInit (&vt);
2391 func (VT_UI1, &vt);
2392 if (vt.vt == VT_UI1 && vt.bVal == 100)
2393 return 0;
2394 return 1;
2397 STDCALL int
2398 mono_test_marshal_variant_out_short_unmanaged(VarRefFunc func)
2400 VARIANT vt;
2401 VariantInit (&vt);
2402 func (VT_I2, &vt);
2403 if (vt.vt == VT_I2 && vt.iVal == -100)
2404 return 0;
2405 return 1;
2408 STDCALL int
2409 mono_test_marshal_variant_out_ushort_unmanaged(VarRefFunc func)
2411 VARIANT vt;
2412 VariantInit (&vt);
2413 func (VT_UI2, &vt);
2414 if (vt.vt == VT_UI2 && vt.uiVal == 100)
2415 return 0;
2416 return 1;
2419 STDCALL int
2420 mono_test_marshal_variant_out_int_unmanaged(VarRefFunc func)
2422 VARIANT vt;
2423 VariantInit (&vt);
2424 func (VT_I4, &vt);
2425 if (vt.vt == VT_I4 && vt.lVal == -100)
2426 return 0;
2427 return 1;
2430 STDCALL int
2431 mono_test_marshal_variant_out_uint_unmanaged(VarRefFunc func)
2433 VARIANT vt;
2434 VariantInit (&vt);
2435 func (VT_UI4, &vt);
2436 if (vt.vt == VT_UI4 && vt.ulVal == 100)
2437 return 0;
2438 return 1;
2441 STDCALL int
2442 mono_test_marshal_variant_out_long_unmanaged(VarRefFunc func)
2444 VARIANT vt;
2445 VariantInit (&vt);
2446 func (VT_I8, &vt);
2447 if (vt.vt == VT_I8 && vt.llVal == -100)
2448 return 0;
2449 return 1;
2452 STDCALL int
2453 mono_test_marshal_variant_out_ulong_unmanaged(VarRefFunc func)
2455 VARIANT vt;
2456 VariantInit (&vt);
2457 func (VT_UI8, &vt);
2458 if (vt.vt == VT_UI8 && vt.ullVal == 100)
2459 return 0;
2460 return 1;
2463 STDCALL int
2464 mono_test_marshal_variant_out_float_unmanaged(VarRefFunc func)
2466 VARIANT vt;
2467 VariantInit (&vt);
2468 func (VT_R4, &vt);
2469 if (vt.vt == VT_R4 && fabs (vt.fltVal - 3.14f) < 1e-10)
2470 return 0;
2471 return 1;
2474 STDCALL int
2475 mono_test_marshal_variant_out_double_unmanaged(VarRefFunc func)
2477 VARIANT vt;
2478 VariantInit (&vt);
2479 func (VT_R8, &vt);
2480 if (vt.vt == VT_R8 && fabs (vt.dblVal - 3.14) < 1e-10)
2481 return 0;
2482 return 1;
2485 STDCALL int
2486 mono_test_marshal_variant_out_bstr_unmanaged(VarRefFunc func)
2488 VARIANT vt;
2489 VariantInit (&vt);
2490 func (VT_BSTR, &vt);
2491 if (vt.vt == VT_BSTR && !wcscmp(vt.bstrVal, L"PI"))
2492 return 0;
2493 return 1;
2496 STDCALL int
2497 mono_test_marshal_variant_out_bool_true_unmanaged(VarRefFunc func)
2499 VARIANT vt;
2500 VariantInit (&vt);
2501 func (VT_BOOL, &vt);
2502 if (vt.vt == VT_BOOL && vt.boolVal == VARIANT_TRUE)
2503 return 0;
2504 return 1;
2507 STDCALL int
2508 mono_test_marshal_variant_out_bool_false_unmanaged(VarRefFunc func)
2510 VARIANT vt;
2511 VariantInit (&vt);
2512 func (VT_BOOL, &vt);
2513 if (vt.vt == VT_BOOL && vt.boolVal == VARIANT_TRUE)
2514 return 0;
2515 return 1;
2518 typedef struct MonoComObject MonoComObject;
2520 typedef struct
2522 int (STDCALL *QueryInterface)(MonoComObject* pUnk, gpointer riid, gpointer* ppv);
2523 int (STDCALL *AddRef)(MonoComObject* pUnk);
2524 int (STDCALL *Release)(MonoComObject* pUnk);
2525 int (STDCALL *get_ITest)(MonoComObject* pUnk, MonoComObject* *ppUnk);
2526 int (STDCALL *SByteIn)(MonoComObject* pUnk, char a);
2527 int (STDCALL *ByteIn)(MonoComObject* pUnk, unsigned char a);
2528 int (STDCALL *ShortIn)(MonoComObject* pUnk, short a);
2529 int (STDCALL *UShortIn)(MonoComObject* pUnk, unsigned short a);
2530 int (STDCALL *IntIn)(MonoComObject* pUnk, int a);
2531 int (STDCALL *UIntIn)(MonoComObject* pUnk, unsigned int a);
2532 int (STDCALL *LongIn)(MonoComObject* pUnk, LONGLONG a);
2533 int (STDCALL *ULongIn)(MonoComObject* pUnk, ULONGLONG a);
2534 int (STDCALL *FloatIn)(MonoComObject* pUnk, float a);
2535 int (STDCALL *DoubleIn)(MonoComObject* pUnk, double a);
2536 int (STDCALL *ITestIn)(MonoComObject* pUnk, MonoComObject* pUnk2);
2537 int (STDCALL *ITestOut)(MonoComObject* pUnk, MonoComObject* *ppUnk);
2538 } MonoIUnknown;
2540 struct MonoComObject
2542 MonoIUnknown* vtbl;
2543 int m_ref;
2546 DEFINE_GUID(IID_ITest, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
2547 DEFINE_GUID(IID_IMonoUnknown, 0, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0, 0x46);
2548 DEFINE_GUID(IID_IMonoDispatch, 0x00020400, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0, 0x46);
2550 int STDCALL MonoQueryInterface(MonoComObject* pUnk, gpointer riid, gpointer* ppv)
2552 *ppv = NULL;
2553 if (!memcmp(riid, &IID_IMonoUnknown, sizeof(GUID))) {
2554 *ppv = pUnk;
2555 return S_OK;
2557 else if (!memcmp(riid, &IID_ITest, sizeof(GUID))) {
2558 *ppv = pUnk;
2559 return S_OK;
2561 else if (!memcmp(riid, &IID_IMonoDispatch, sizeof(GUID))) {
2562 *ppv = pUnk;
2563 return S_OK;
2565 return E_NOINTERFACE;
2568 int STDCALL MonoAddRef(MonoComObject* pUnk)
2570 return ++(pUnk->m_ref);
2573 int STDCALL MonoRelease(MonoComObject* pUnk)
2575 return --(pUnk->m_ref);
2578 int STDCALL SByteIn(MonoComObject* pUnk, char a)
2580 return S_OK;
2583 int STDCALL ByteIn(MonoComObject* pUnk, unsigned char a)
2585 return S_OK;
2588 int STDCALL ShortIn(MonoComObject* pUnk, short a)
2590 return S_OK;
2593 int STDCALL UShortIn(MonoComObject* pUnk, unsigned short a)
2595 return S_OK;
2598 int STDCALL IntIn(MonoComObject* pUnk, int a)
2600 return S_OK;
2603 int STDCALL UIntIn(MonoComObject* pUnk, unsigned int a)
2605 return S_OK;
2608 int STDCALL LongIn(MonoComObject* pUnk, LONGLONG a)
2610 return S_OK;
2613 int STDCALL ULongIn(MonoComObject* pUnk, ULONGLONG a)
2615 return S_OK;
2618 int STDCALL FloatIn(MonoComObject* pUnk, float a)
2620 return S_OK;
2623 int STDCALL DoubleIn(MonoComObject* pUnk, double a)
2625 return S_OK;
2628 int STDCALL ITestIn(MonoComObject* pUnk, MonoComObject *pUnk2)
2630 return S_OK;
2633 int STDCALL ITestOut(MonoComObject* pUnk, MonoComObject* *ppUnk)
2635 return S_OK;
2638 int STDCALL get_ITest(MonoComObject* pUnk, MonoComObject* *ppUnk)
2640 return S_OK;
2643 static void create_com_object (MonoComObject** pOut)
2645 *pOut = g_new0 (MonoComObject, 1);
2646 (*pOut)->vtbl = g_new0 (MonoIUnknown, 1);
2648 (*pOut)->m_ref = 1;
2649 (*pOut)->vtbl->QueryInterface = MonoQueryInterface;
2650 (*pOut)->vtbl->AddRef = MonoAddRef;
2651 (*pOut)->vtbl->Release = MonoRelease;
2652 (*pOut)->vtbl->SByteIn = SByteIn;
2653 (*pOut)->vtbl->ByteIn = ByteIn;
2654 (*pOut)->vtbl->ShortIn = ShortIn;
2655 (*pOut)->vtbl->UShortIn = UShortIn;
2656 (*pOut)->vtbl->IntIn = IntIn;
2657 (*pOut)->vtbl->UIntIn = UIntIn;
2658 (*pOut)->vtbl->LongIn = LongIn;
2659 (*pOut)->vtbl->ULongIn = ULongIn;
2660 (*pOut)->vtbl->FloatIn = FloatIn;
2661 (*pOut)->vtbl->DoubleIn = DoubleIn;
2662 (*pOut)->vtbl->ITestIn = ITestIn;
2663 (*pOut)->vtbl->ITestOut = ITestOut;
2664 (*pOut)->vtbl->get_ITest = get_ITest;
2667 static MonoComObject* same_object = NULL;
2669 STDCALL int
2670 mono_test_marshal_com_object_create(MonoComObject* *pUnk)
2672 create_com_object (pUnk);
2674 if (!same_object)
2675 same_object = *pUnk;
2677 return 0;
2680 STDCALL int
2681 mono_test_marshal_com_object_same(MonoComObject* *pUnk)
2683 *pUnk = same_object;
2685 return 0;
2688 STDCALL int
2689 mono_test_marshal_com_object_destroy(MonoComObject *pUnk)
2691 int ref = --(pUnk->m_ref);
2692 g_free(pUnk->vtbl);
2693 g_free(pUnk);
2695 return ref;
2698 STDCALL int
2699 mono_test_marshal_com_object_ref_count(MonoComObject *pUnk)
2701 return pUnk->m_ref;
2704 STDCALL int
2705 mono_test_marshal_ccw_itest (MonoComObject *pUnk)
2707 int hr = 0;
2708 MonoComObject* pTest;
2710 if (!pUnk)
2711 return 1;
2713 hr = pUnk->vtbl->SByteIn (pUnk, -100);
2714 if (hr != 0)
2715 return 2;
2716 hr = pUnk->vtbl->ByteIn (pUnk, 100);
2717 if (hr != 0)
2718 return 3;
2719 hr = pUnk->vtbl->ShortIn (pUnk, -100);
2720 if (hr != 0)
2721 return 4;
2722 hr = pUnk->vtbl->UShortIn (pUnk, 100);
2723 if (hr != 0)
2724 return 5;
2725 hr = pUnk->vtbl->IntIn (pUnk, -100);
2726 if (hr != 0)
2727 return 6;
2728 hr = pUnk->vtbl->UIntIn (pUnk, 100);
2729 if (hr != 0)
2730 return 7;
2731 hr = pUnk->vtbl->LongIn (pUnk, -100);
2732 if (hr != 0)
2733 return 8;
2734 hr = pUnk->vtbl->ULongIn (pUnk, 100);
2735 if (hr != 0)
2736 return 9;
2737 hr = pUnk->vtbl->FloatIn (pUnk, 3.14f);
2738 if (hr != 0)
2739 return 10;
2740 hr = pUnk->vtbl->DoubleIn (pUnk, 3.14);
2741 if (hr != 0)
2742 return 11;
2743 hr = pUnk->vtbl->ITestIn (pUnk, pUnk);
2744 if (hr != 0)
2745 return 12;
2746 hr = pUnk->vtbl->ITestOut (pUnk, &pTest);
2747 if (hr != 0)
2748 return 13;
2750 return 0;
2754 #endif //NOT_YET