14 #define STDCALL __stdcall
20 extern __declspec(dllimport) __stdcall void CoTaskMemFree(void *ptr);
23 typedef int (STDCALL *SimpleDelegate) (int a);
25 static void marshal_free (void *ptr)
34 STDCALL unsigned short*
35 test_lpwstr_marshal (unsigned short* chars, long length)
40 res = malloc (2 * (length + 1));
42 // printf("test_lpwstr_marshal()\n");
44 while ( i < length ) {
45 // printf("X|%u|\n", chars[i]);
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;
68 mono_return_int (int a) {
69 // printf ("Got value %d\n", a);
79 mono_return_int_ss (struct ss a) {
80 // printf ("Got value %d\n", a.i);
85 mono_return_ss (struct ss a) {
86 // printf ("Got value %d\n", a.i);
97 mono_return_sc1 (struct sc1 a) {
98 // printf ("Got value %d\n", a.c[0]);
110 mono_return_sc3 (struct sc3 a) {
111 // printf ("Got values %d %d %d\n", a.c[0], a.c[1], a.c[2]);
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]);
141 mono_return_int_su (union su a) {
142 // printf ("Got value %d\n", a.i1);
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);
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);
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);
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;
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;
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;
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;
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;
187 mono_test_split_double_arguments (double a, double b, float c, double d, double e)
189 return a + b + c + d + e;
193 mono_test_puts_static (char *s)
195 // printf ("TEST %s\n", s);
199 typedef int (STDCALL *SimpleDelegate3) (int a, int b);
202 mono_invoke_delegate (SimpleDelegate3 delegate)
206 // printf ("start invoke %p\n", delegate);
208 res = delegate (2, 3);
210 // printf ("end invoke\n");
216 mono_test_marshal_char (short a1)
225 mono_test_marshal_char_array (gunichar2 *s)
227 const char m[] = "abcdef";
231 s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
240 mono_test_empty_pinvoke (int i)
246 mono_test_marshal_bool_byref (int a, int *b, int c)
256 mono_test_marshal_array (int *a1)
260 for (i = 0; i < 50; i++)
267 mono_test_marshal_inout_array (int *a1)
271 for (i = 0; i < 50; i++) {
273 a1 [i] = 50 - a1 [i];
280 mono_test_marshal_out_array (int *a1)
284 for (i = 0; i < 50; i++) {
292 mono_test_marshal_inout_nonblittable_array (gunichar2 *a1)
296 for (i = 0; i < 10; i++) {
317 mono_test_return_vtype (int i)
320 static gunichar2 test2 [] = { 'T', 'E', 'S', 'T', '2', 0 };
332 mono_test_delegate_struct (void)
334 // printf ("TEST\n");
337 typedef char* (STDCALL *ReturnStringDelegate) (const char *s);
340 mono_test_return_string (ReturnStringDelegate func)
344 // printf ("mono_test_return_string\n");
349 // printf ("got string: %s\n", res);
350 return g_strdup ("12345");
353 typedef int (STDCALL *RefVTypeDelegate) (int a, simplestruct *ss, int b);
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")) {
365 return func (a, ss, b);
371 typedef int (STDCALL *OutVTypeDelegate) (int a, simplestruct *ss, int b);
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;
381 if (ss->a && ss->b && ss->c && !strcmp (ss->d, "TEST3"))
389 SimpleDelegate func, func2;
392 STDCALL DelegateStruct
393 mono_test_marshal_delegate_struct (DelegateStruct ds)
397 res.a = ds.func (ds.a) + ds.func2 (ds.a);
399 res.func2 = ds.func2;
405 mono_test_marshal_struct (simplestruct ss)
407 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
408 !strcmp (ss.d, "TEST"))
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);
422 ss->d = g_strdup ("DEF");
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)
449 /* on HP some of the struct should be on the stack and not in registers */
451 mono_test_marshal_struct2_2 (int i, int j, int k, simplestruct2 ss)
453 if (i != 10 || j != 11 || k != 12)
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)
464 mono_test_marshal_lpstruct (simplestruct *ss)
466 if (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
467 !strcmp (ss->d, "TEST"))
474 mono_test_marshal_lpstruct_blittable (point *p)
476 if (p->x == 1.0 && p->y == 2.0)
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))
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))
498 typedef struct long_align_struct {
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)
518 if (i != 10 || j != 11 || k != 12 || l != 14)
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))
525 res = g_new0 (simplestruct2, 1);
526 memcpy (res, ss, sizeof (simplestruct2));
527 res->d = g_strdup ("TEST");
532 mono_test_marshal_byref_class (simplestruct2 **ssp)
534 simplestruct2 *ss = *ssp;
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))
542 res = g_new0 (simplestruct2, 1);
543 memcpy (res, ss, sizeof (simplestruct2));
544 res->d = g_strdup ("TEST-RES");
556 /* Yes, this is correct, we are only trying to determine the value of the stack here */
562 reliable_delegate (int a)
568 * Checks whether get_sp() works as expected. It doesn't work with gcc-2.95.3 on linux.
571 is_get_sp_reliable (void)
575 reliable_delegate(1);
577 reliable_delegate(1);
583 mono_test_marshal_delegate (SimpleDelegate delegate)
587 /* Check that the delegate wrapper is stdcall */
592 if (is_get_sp_reliable())
593 g_assert (sp1 == sp2);
598 STDCALL SimpleDelegate
599 mono_test_marshal_return_delegate (SimpleDelegate delegate)
605 return_plus_one (int i)
610 STDCALL SimpleDelegate
611 mono_test_marshal_return_delegate_2 ()
613 return return_plus_one;
616 typedef simplestruct (STDCALL *SimpleDelegate2) (simplestruct ss);
619 is_utf16_equals (gunichar2 *s1, const char *s2)
624 s = g_utf16_to_utf8 (s1, -1, NULL, NULL, NULL);
625 res = strcmp (s, s2);
632 mono_test_marshal_delegate2 (SimpleDelegate2 delegate)
634 simplestruct ss, res;
640 ss.d2 = g_utf8_to_utf16 ("TEST2", -1, NULL, NULL, NULL);
643 if (! (res.a && !res.b && res.c && !strcmp (res.d, "TEST-RES") && is_utf16_equals (res.d2, "TEST2-RES")))
649 typedef simplestruct* (STDCALL *SimpleDelegate4) (simplestruct *ss);
652 mono_test_marshal_delegate4 (SimpleDelegate4 delegate)
663 res = delegate (&ss);
667 /* Check return value */
668 if (! (!res->a && res->b && !res->c && !strcmp (res->d, "TEST")))
671 /* Check NULL argument and NULL result */
672 res = delegate (NULL);
679 typedef int (STDCALL *SimpleDelegate5) (simplestruct **ss);
682 mono_test_marshal_delegate5 (SimpleDelegate5 delegate)
695 res = delegate (&ptr);
699 if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
706 mono_test_marshal_delegate6 (SimpleDelegate5 delegate)
710 res = delegate (NULL);
715 typedef int (STDCALL *SimpleDelegate7) (simplestruct **ss);
718 mono_test_marshal_delegate7 (SimpleDelegate7 delegate)
723 /* Check that the input pointer is ignored */
724 ptr = (gpointer)0x12345678;
726 res = delegate (&ptr);
730 if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
736 typedef int (STDCALL *InOutByvalClassDelegate) (simplestruct *ss);
739 mono_test_marshal_inout_byval_class_delegate (InOutByvalClassDelegate delegate)
747 ss.d = g_strdup_printf ("%s", "FOO");
749 res = delegate (&ss);
753 if (!(ss.a && !ss.b && ss.c && !strcmp (ss.d, "RES")))
759 typedef int (STDCALL *SimpleDelegate8) (gunichar2 *s);
762 mono_test_marshal_delegate8 (SimpleDelegate8 delegate, gunichar2 *s)
767 typedef int (STDCALL *return_int_fnt) (int i);
768 typedef int (STDCALL *SimpleDelegate9) (return_int_fnt d);
771 mono_test_marshal_delegate9 (SimpleDelegate9 delegate, gpointer ftn)
773 return delegate (ftn);
783 mono_test_marshal_delegate10 (SimpleDelegate9 delegate)
785 return delegate (return_self);
788 typedef int (STDCALL *PrimitiveByrefDelegate) (int *i);
791 mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate delegate)
795 int res = delegate (&i);
805 typedef int (STDCALL *return_int_delegate) (int i);
807 typedef return_int_delegate (STDCALL *ReturnDelegateDelegate) (void);
810 mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d)
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)
828 mono_test_marshal_stringbuilder_default (char *s, int n)
830 const char m[] = "This is my message. Isn't it nice?";
838 mono_test_marshal_stringbuilder_unicode (gunichar2 *s, int n)
840 const char m[] = "This is my message. Isn't it nice?";
844 s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
863 mono_test_marshal_empty_string_array (char **array)
865 return (array == NULL) ? 0 : 1;
869 mono_test_marshal_string_array (char **array)
871 if (strcmp (array [0], "ABC"))
873 if (strcmp (array [1], "DEF"))
876 if (array [2] != NULL)
883 mono_test_marshal_byref_string_array (char ***array)
888 if (strcmp ((*array) [0], "Alpha"))
890 if (strcmp ((*array) [1], "Beta"))
892 if (strcmp ((*array) [2], "Gamma"))
899 mono_test_marshal_stringbuilder_array (char **array)
901 if (strcmp (array [0], "ABC"))
903 if (strcmp (array [1], "DEF"))
906 strcpy (array [0], "DEF");
907 strcpy (array [1], "ABC");
913 mono_test_marshal_unicode_string_array (gunichar2 **array, char **array2)
915 GError *error = NULL;
918 s = g_utf16_to_utf8 (array [0], -1, NULL, NULL, &error);
919 if (strcmp (s, "ABC")) {
926 s = g_utf16_to_utf8 (array [1], -1, NULL, NULL, &error);
927 if (strcmp (s, "DEF")) {
934 if (strcmp (array2 [0], "ABC"))
937 if (strcmp (array2 [1], "DEF"))
943 /* this does not work on Redhat gcc 2.96 */
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)
953 if (a == 1 && b == 2)
963 STDCALL ByValStrStruct *
964 mono_test_byvalstr_gen (void)
968 ret = malloc(sizeof(ByValStrStruct));
969 memset(ret, 'a', sizeof(ByValStrStruct)-1);
970 ret->a[sizeof(ByValStrStruct)-1] = 0;
976 mono_test_byvalstr_check (ByValStrStruct* data, char* correctString)
980 ret = strcmp(data->a, correctString);
981 // printf ("T1: %s\n", data->a);
982 // printf ("T2: %s\n", correctString);
989 NameManglingAnsi (char *data)
991 return data [0] + data [1] + data [2];
995 NameManglingAnsiA (char *data)
997 g_assert_not_reached ();
1001 NameManglingAnsiW (char *data)
1003 g_assert_not_reached ();
1007 NameManglingAnsi2A (char *data)
1009 return data [0] + data [1] + data [2];
1013 NameManglingAnsi2W (char *data)
1015 g_assert_not_reached ();
1019 NameManglingUnicode (char *data)
1021 g_assert_not_reached ();
1025 NameManglingUnicodeW (gunichar2 *data)
1027 return data [0] + data [1] + data [2];
1031 NameManglingUnicode2 (gunichar2 *data)
1033 return data [0] + data [1] + data [2];
1037 NameManglingAutoW (char *data)
1040 return (data [0] + data [1] + data [2]) == 131 ? 0 : 1;
1042 g_assert_not_reached ();
1047 NameManglingAuto (char *data)
1050 return (data [0] + data [1] + data [2]) == 198 ? 0 : 1;
1052 g_assert_not_reached ();
1056 typedef int (STDCALL *intcharFunc)(const char*);
1059 callFunction (intcharFunc f)
1070 class_marshal_test0 (SimpleObj *obj1)
1072 // printf ("class_marshal_test0 %s %d\n", obj1->str, obj1->i);
1074 if (strcmp(obj1->str, "T1"))
1083 class_marshal_test4 (SimpleObj *obj1)
1092 class_marshal_test1 (SimpleObj **obj1)
1094 SimpleObj *res = malloc (sizeof (SimpleObj));
1096 res->str = g_strdup ("ABC");
1103 class_marshal_test2 (SimpleObj **obj1)
1105 // printf ("class_marshal_test2 %s %d\n", (*obj1)->str, (*obj1)->i);
1107 if (strcmp((*obj1)->str, "ABC"))
1109 if ((*obj1)->i != 5)
1116 string_marshal_test0 (char *str)
1118 if (strcmp (str, "TEST0"))
1125 string_marshal_test1 (const char **str)
1131 string_marshal_test2 (char **str)
1133 // printf ("string_marshal_test2 %s\n", *str);
1135 if (strcmp (*str, "TEST1"))
1142 string_marshal_test3 (char *str)
1155 STDCALL BlittableClass*
1156 TestBlittableClass (BlittableClass *vl)
1158 BlittableClass *res;
1160 // printf ("TestBlittableClass %d %d\n", vl->a, vl->b);
1166 res = g_new0 (BlittableClass, 1);
1167 memcpy (res, vl, sizeof (BlittableClass));
1169 res = g_new0 (BlittableClass, 1);
1177 typedef struct OSVERSIONINFO_STRUCT
1181 } OSVERSIONINFO_STRUCT;
1184 MyGetVersionEx (OSVERSIONINFO_STRUCT *osvi)
1187 // printf ("GOT %d %d\n", osvi->a, osvi->b);
1192 return osvi->a + osvi->b;
1196 BugGetVersionEx (int a, int b, int c, int d, int e, int f, int g, int h, OSVERSIONINFO_STRUCT *osvi)
1199 // printf ("GOT %d %d\n", osvi->a, osvi->b);
1204 return osvi->a + osvi->b;
1208 mono_test_marshal_point (point pt)
1210 // printf("point %g %g\n", pt.x, pt.y);
1211 if (pt.x == 1.25 && pt.y == 3.5)
1223 mono_test_marshal_mixed_point (mixed_point pt)
1225 // printf("mixed point %d %g\n", pt.x, pt.y);
1226 if (pt.x == 5 && pt.y == 6.75)
1233 mono_test_marshal_mixed_point_2 (mixed_point *pt)
1235 if (pt->x != 5 || pt->y != 6.75)
1245 marshal_test_ref_bool(int i, char *b1, short *b2, int *b3)
1248 if (*b1 != 0 && *b1 != 1)
1250 if (*b2 != 0 && *b2 != -1) /* variant_bool */
1252 if (*b3 != 0 && *b3 != 1)
1254 if (i == ((*b1 << 2) | (-*b2 << 1) | *b3))
1266 short b2; /* variant_bool */
1271 marshal_test_bool_struct(struct BoolStruct *s)
1274 if (s->b1 != 0 && s->b1 != 1)
1276 if (s->b2 != 0 && s->b2 != -1)
1278 if (s->b3 != 0 && s->b3 != 1)
1280 if (s->i == ((s->b1 << 2) | (-s->b2 << 1) | s->b3))
1289 mono_test_last_error (int err)
1299 mono_test_asany (void *ptr, int what)
1303 return (*(int*)ptr == 5) ? 0 : 1;
1305 return strcmp (ptr, "ABC") == 0 ? 0 : 1;
1307 simplestruct2 ss = *(simplestruct2*)ptr;
1309 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
1310 !strcmp (ss.d, "TEST") &&
1311 ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
1317 GError *error = NULL;
1320 s = g_utf16_to_utf8 (ptr, -1, NULL, NULL, &error);
1321 if (!strcmp (s, "ABC")) {
1331 g_assert_not_reached ();
1346 mono_test_marshal_asany_inout (void* ptr)
1348 AsAnyStruct* asAny = ptr;
1349 int res = asAny->i + asAny->j + asAny->k;
1360 * AMD64 marshalling tests.
1363 typedef struct amd64_struct1 {
1370 STDCALL amd64_struct1
1371 mono_test_marshal_amd64_pass_return_struct1 (amd64_struct1 s)
1381 typedef struct amd64_struct2 {
1386 STDCALL amd64_struct2
1387 mono_test_marshal_amd64_pass_return_struct2 (amd64_struct2 s)
1395 typedef struct amd64_struct3 {
1399 STDCALL amd64_struct3
1400 mono_test_marshal_amd64_pass_return_struct3 (amd64_struct3 s)
1407 typedef struct amd64_struct4 {
1411 STDCALL amd64_struct4
1412 mono_test_marshal_amd64_pass_return_struct4 (amd64_struct4 s)
1421 * IA64 marshalling tests.
1423 typedef struct test_struct5 {
1427 STDCALL test_struct5
1428 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, test_struct5 s, double d3, double d4)
1436 typedef struct test_struct6 {
1440 STDCALL test_struct6
1441 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, test_struct6 s, double d3, double d4)
1449 static guint32 custom_res [2];
1452 mono_test_marshal_pass_return_custom (int i, guint32 *ptr, int j)
1454 /* ptr will be freed by CleanupNative, so make a copy */
1455 custom_res [0] = 0; /* not allocated by AllocHGlobal */
1456 custom_res [1] = ptr [1];
1462 mono_test_marshal_pass_out_custom (int i, guint32 **ptr, int j)
1465 custom_res [1] = i + j + 10;
1473 mono_test_marshal_pass_inout_custom (int i, guint32 *ptr, int j)
1476 ptr [1] = i + ptr [1] + j;
1482 mono_test_marshal_pass_out_byval_custom (int i, guint32 *ptr, int j)
1484 return ptr == NULL ? 0 : 1;
1488 mono_test_marshal_pass_byref_custom (int i, guint32 **ptr, int j)
1496 mono_test_marshal_pass_return_custom2 (int i, guint32 *ptr, int j)
1498 g_assert_not_reached ();
1504 mono_test_marshal_pass_return_custom_null (int i, guint32 *ptr, int j)
1506 g_assert (ptr == NULL);
1511 typedef void *(STDCALL *PassReturnPtrDelegate) (void *ptr);
1514 mono_test_marshal_pass_return_custom_in_delegate (PassReturnPtrDelegate del)
1528 /* FIXME: Freed with FreeHGlobal */
1537 mono_test_marshal_pass_return_custom_null_in_delegate (PassReturnPtrDelegate del)
1539 void *ptr = del (NULL);
1541 return (ptr == NULL) ? 15 : 0;
1544 typedef int (STDCALL *ReturnEnumDelegate) (int e);
1547 mono_test_marshal_return_enum_delegate (ReturnEnumDelegate func)
1557 typedef BlittableStruct (STDCALL *SimpleDelegate10) (BlittableStruct ss);
1560 mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 delegate)
1562 BlittableStruct ss, res;
1569 res = delegate (ss);
1570 if (! ((res.a == -1) && (res.b == -2) && (res.c == -3) && (res.d == -55)))
1577 mono_test_stdcall_name_mangling (int a, int b, int c)
1583 * PASSING AND RETURNING SMALL STRUCTURES FROM DELEGATES TESTS
1590 typedef SmallStruct1 (STDCALL *SmallStructDelegate1) (SmallStruct1 ss);
1593 mono_test_marshal_small_struct_delegate1 (SmallStructDelegate1 delegate)
1595 SmallStruct1 ss, res;
1599 res = delegate (ss);
1600 if (! (res.i == -1))
1610 typedef SmallStruct2 (STDCALL *SmallStructDelegate2) (SmallStruct2 ss);
1613 mono_test_marshal_small_struct_delegate2 (SmallStructDelegate2 delegate)
1615 SmallStruct2 ss, res;
1620 res = delegate (ss);
1621 if (! ((res.i == -2) && (res.j == -3)))
1632 typedef SmallStruct3 (STDCALL *SmallStructDelegate3) (SmallStruct3 ss);
1635 mono_test_marshal_small_struct_delegate3 (SmallStructDelegate3 delegate)
1637 SmallStruct3 ss, res;
1642 res = delegate (ss);
1643 if (! ((res.i == -1) && (res.j == -2)))
1653 typedef SmallStruct4 (STDCALL *SmallStructDelegate4) (SmallStruct4 ss);
1656 mono_test_marshal_small_struct_delegate4 (SmallStructDelegate4 delegate)
1658 SmallStruct4 ss, res;
1662 res = delegate (ss);
1663 if (! (res.i == -1))
1673 typedef SmallStruct5 (STDCALL *SmallStructDelegate5) (SmallStruct5 ss);
1676 mono_test_marshal_small_struct_delegate5 (SmallStructDelegate5 delegate)
1678 SmallStruct5 ss, res;
1682 res = delegate (ss);
1683 if (! (res.i == -5))
1693 typedef SmallStruct6 (STDCALL *SmallStructDelegate6) (SmallStruct6 ss);
1696 mono_test_marshal_small_struct_delegate6 (SmallStructDelegate6 delegate)
1698 SmallStruct6 ss, res;
1703 res = delegate (ss);
1704 if (! ((res.i == -1) && (res.j == -2)))
1715 typedef SmallStruct7 (STDCALL *SmallStructDelegate7) (SmallStruct7 ss);
1718 mono_test_marshal_small_struct_delegate7 (SmallStructDelegate7 delegate)
1720 SmallStruct7 ss, res;
1725 res = delegate (ss);
1726 if (! ((res.i == -1) && (res.j == -2)))
1736 typedef SmallStruct8 (STDCALL *SmallStructDelegate8) (SmallStruct8 ss);
1739 mono_test_marshal_small_struct_delegate8 (SmallStructDelegate8 delegate)
1741 SmallStruct8 ss, res;
1745 res = delegate (ss);
1746 if (! ((res.i == -1.0)))
1756 typedef SmallStruct9 (STDCALL *SmallStructDelegate9) (SmallStruct9 ss);
1759 mono_test_marshal_small_struct_delegate9 (SmallStructDelegate9 delegate)
1761 SmallStruct9 ss, res;
1765 res = delegate (ss);
1766 if (! ((res.i == -1.0)))
1776 typedef SmallStruct10 (STDCALL *SmallStructDelegate10) (SmallStruct10 ss);
1779 mono_test_marshal_small_struct_delegate10 (SmallStructDelegate10 delegate)
1781 SmallStruct10 ss, res;
1786 res = delegate (ss);
1787 if (! ((res.i == -1.0) && (res.j == -2.0)))
1798 typedef SmallStruct11 (STDCALL *SmallStructDelegate11) (SmallStruct11 ss);
1801 mono_test_marshal_small_struct_delegate11 (SmallStructDelegate11 delegate)
1803 SmallStruct11 ss, res;
1808 res = delegate (ss);
1809 if (! ((res.i == -1.0) && (res.j == -2)))
1815 typedef int (STDCALL *ArrayDelegate) (int i, char *j, void *arr);
1818 mono_test_marshal_array_delegate (void *arr, int len, ArrayDelegate del)
1820 return del (len, NULL, arr);
1824 mono_test_marshal_out_array_delegate (int *arr, int len, ArrayDelegate del)
1826 del (len, NULL, arr);
1828 if ((arr [0] != 1) || (arr [1] != 2))
1835 mono_test_marshal_out_string_array_delegate (char **arr, int len, ArrayDelegate del)
1837 del (len, NULL, arr);
1839 if (!strcmp (arr [0], "ABC") && !strcmp (arr [1], "DEF"))
1845 typedef int (*CdeclDelegate) (int i, int j);
1848 mono_test_marshal_cdecl_delegate (CdeclDelegate del)
1852 for (i = 0; i < 1000; ++i)
1858 typedef char** (*ReturnStringArrayDelegate) (int i);
1861 mono_test_marshal_return_string_array_delegate (ReturnStringArrayDelegate d)
1869 if (strcmp (arr [0], "ABC") || strcmp (arr [1], "DEF"))
1880 add_delegate (int i, int j)
1886 mono_test_marshal_return_fnptr (void)
1888 return &add_delegate;
1898 mono_test_marshal_bstr_in(BSTR bstr)
1900 if (!wcscmp(bstr, L"mono_test_marshal_bstr_in"))
1906 mono_test_marshal_bstr_out(BSTR* bstr)
1908 *bstr = SysAllocString(L"mono_test_marshal_bstr_out");
1913 mono_test_marshal_bstr_in_null(BSTR bstr)
1921 mono_test_marshal_bstr_out_null(BSTR* bstr)
1928 mono_test_marshal_variant_in_sbyte(VARIANT variant)
1930 if (variant.vt == VT_I1 && variant.cVal == 100)
1936 mono_test_marshal_variant_in_byte(VARIANT variant)
1938 if (variant.vt == VT_UI1 && variant.bVal == 100)
1944 mono_test_marshal_variant_in_short(VARIANT variant)
1946 if (variant.vt == VT_I2 && variant.iVal == 314)
1952 mono_test_marshal_variant_in_ushort(VARIANT variant)
1954 if (variant.vt == VT_UI2 && variant.uiVal == 314)
1960 mono_test_marshal_variant_in_int(VARIANT variant)
1962 if (variant.vt == VT_I4 && variant.lVal == 314)
1968 mono_test_marshal_variant_in_uint(VARIANT variant)
1970 if (variant.vt == VT_UI4 && variant.ulVal == 314)
1976 mono_test_marshal_variant_in_long(VARIANT variant)
1978 if (variant.vt == VT_I8 && variant.llVal == 314)
1984 mono_test_marshal_variant_in_ulong(VARIANT variant)
1986 if (variant.vt == VT_UI8 && variant.ullVal == 314)
1992 mono_test_marshal_variant_in_float(VARIANT variant)
1994 if (variant.vt == VT_R4 && (variant.fltVal - 3.14)/3.14 < .001)
2000 mono_test_marshal_variant_in_double(VARIANT variant)
2002 if (variant.vt == VT_R8 && (variant.dblVal - 3.14)/3.14 < .001)
2008 mono_test_marshal_variant_in_bstr(VARIANT variant)
2010 if (variant.vt == VT_BSTR && !wcscmp(variant.bstrVal, L"PI"))
2016 mono_test_marshal_variant_out_sbyte(VARIANT* variant)
2018 variant->vt = VT_I1;
2019 variant->cVal = 100;
2025 mono_test_marshal_variant_out_byte(VARIANT* variant)
2027 variant->vt = VT_UI1;
2028 variant->bVal = 100;
2034 mono_test_marshal_variant_out_short(VARIANT* variant)
2036 variant->vt = VT_I2;
2037 variant->iVal = 314;
2043 mono_test_marshal_variant_out_ushort(VARIANT* variant)
2045 variant->vt = VT_UI2;
2046 variant->uiVal = 314;
2052 mono_test_marshal_variant_out_int(VARIANT* variant)
2054 variant->vt = VT_I4;
2055 variant->lVal = 314;
2061 mono_test_marshal_variant_out_uint(VARIANT* variant)
2063 variant->vt = VT_UI4;
2064 variant->ulVal = 314;
2070 mono_test_marshal_variant_out_long(VARIANT* variant)
2072 variant->vt = VT_I8;
2073 variant->llVal = 314;
2079 mono_test_marshal_variant_out_ulong(VARIANT* variant)
2081 variant->vt = VT_UI8;
2082 variant->ullVal = 314;
2088 mono_test_marshal_variant_out_float(VARIANT* variant)
2090 variant->vt = VT_R4;
2091 variant->fltVal = 3.14;
2097 mono_test_marshal_variant_out_double(VARIANT* variant)
2099 variant->vt = VT_R8;
2100 variant->dblVal = 3.14;
2106 mono_test_marshal_variant_out_bstr(VARIANT* variant)
2108 variant->vt = VT_BSTR;
2109 variant->bstrVal = SysAllocString(L"PI");
2115 #define COM_STDCALL __stdcall
2117 #define COM_STDCALL __attribute__((stdcall))
2120 typedef struct MonoComObject MonoComObject;
2124 int (COM_STDCALL *QueryInterface)(MonoComObject* pUnk, gpointer riid, gpointer* ppv);
2125 int (COM_STDCALL *AddRef)(MonoComObject* pUnk);
2126 int (COM_STDCALL *Release)(MonoComObject* pUnk);
2127 int (COM_STDCALL *Add)(MonoComObject* pUnk, int a, int b, int* c);
2128 int (COM_STDCALL *Subtract)(MonoComObject* pUnk, int a, int b, int* c);
2129 int (COM_STDCALL *Same)(MonoComObject* pUnk, MonoComObject* *pOut);
2130 int (COM_STDCALL *Different)(MonoComObject* pUnk, MonoComObject* *pOut);
2133 struct MonoComObject
2139 DEFINE_GUID(IID_IMath, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
2140 DEFINE_GUID(IID_IMonoUnknown, 0, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0, 0x46);
2141 DEFINE_GUID(IID_IMonoDispatch, 0x00020400, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0, 0x46);
2143 int COM_STDCALL MonoQueryInterface(MonoComObject* pUnk, gpointer riid, gpointer* ppv)
2146 if (!memcmp(riid, &IID_IMonoUnknown, sizeof(GUID))) {
2150 else if (!memcmp(riid, &IID_IMath, sizeof(GUID))) {
2154 else if (!memcmp(riid, &IID_IMonoDispatch, sizeof(GUID))) {
2158 return E_NOINTERFACE;
2161 int COM_STDCALL MonoAddRef(MonoComObject* pUnk)
2163 return ++(pUnk->m_ref);
2166 int COM_STDCALL MonoRelease(MonoComObject* pUnk)
2168 return --(pUnk->m_ref);
2171 int COM_STDCALL Add(MonoComObject* pUnk, int a, int b, int* c)
2177 int COM_STDCALL Subtract(MonoComObject* pUnk, int a, int b, int* c)
2183 static void create_com_object (MonoComObject** pOut);
2184 static MonoComObject* same_com_object = NULL;
2186 int COM_STDCALL Same(MonoComObject* pUnk, MonoComObject** pOut)
2188 if (!same_com_object)
2189 create_com_object (&same_com_object);
2190 *pOut = same_com_object;
2194 int COM_STDCALL Different(MonoComObject* pUnk, MonoComObject** pOut)
2196 create_com_object (pOut);
2200 static void create_com_object (MonoComObject** pOut)
2202 *pOut = g_new0 (MonoComObject, 1);
2203 (*pOut)->vtbl = g_new0 (MonoIUnknown, 1);
2206 (*pOut)->vtbl->QueryInterface = MonoQueryInterface;
2207 (*pOut)->vtbl->AddRef = MonoAddRef;
2208 (*pOut)->vtbl->Release = MonoRelease;
2209 (*pOut)->vtbl->Add = Add;
2210 (*pOut)->vtbl->Subtract = Subtract;
2211 (*pOut)->vtbl->Same = Same;
2212 (*pOut)->vtbl->Different = Different;
2216 mono_test_marshal_com_object_create(MonoComObject* *pUnk)
2218 create_com_object (pUnk);
2224 mono_test_marshal_com_object_destroy(MonoComObject *pUnk)
2226 int ref = --(pUnk->m_ref);
2234 mono_test_marshal_com_object_ref_count(MonoComObject *pUnk)