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);
991 } ByValStrStruct_Unicode;
994 mono_test_byvalstr_check_unicode (ByValStrStruct_Unicode *ref, int test)
996 if (ref->flag != 0x1234abcd){
997 printf ("overwritten data");
1001 if (test == 1 || test == 3){
1002 if (ref->a [0] != '1' ||
1004 ref->a [2] != '2' ||
1006 ref->a [4] != '3' ||
1012 if (ref->a [0] != '1' ||
1014 ref->a [2] != '2' ||
1023 NameManglingAnsi (char *data)
1025 return data [0] + data [1] + data [2];
1029 NameManglingAnsiA (char *data)
1031 g_assert_not_reached ();
1035 NameManglingAnsiW (char *data)
1037 g_assert_not_reached ();
1041 NameManglingAnsi2A (char *data)
1043 return data [0] + data [1] + data [2];
1047 NameManglingAnsi2W (char *data)
1049 g_assert_not_reached ();
1053 NameManglingUnicode (char *data)
1055 g_assert_not_reached ();
1059 NameManglingUnicodeW (gunichar2 *data)
1061 return data [0] + data [1] + data [2];
1065 NameManglingUnicode2 (gunichar2 *data)
1067 return data [0] + data [1] + data [2];
1071 NameManglingAutoW (char *data)
1074 return (data [0] + data [1] + data [2]) == 131 ? 0 : 1;
1076 g_assert_not_reached ();
1081 NameManglingAuto (char *data)
1084 return (data [0] + data [1] + data [2]) == 198 ? 0 : 1;
1086 g_assert_not_reached ();
1090 typedef int (STDCALL *intcharFunc)(const char*);
1093 callFunction (intcharFunc f)
1104 class_marshal_test0 (SimpleObj *obj1)
1106 // printf ("class_marshal_test0 %s %d\n", obj1->str, obj1->i);
1108 if (strcmp(obj1->str, "T1"))
1117 class_marshal_test4 (SimpleObj *obj1)
1126 class_marshal_test1 (SimpleObj **obj1)
1128 SimpleObj *res = malloc (sizeof (SimpleObj));
1130 res->str = g_strdup ("ABC");
1137 class_marshal_test2 (SimpleObj **obj1)
1139 // printf ("class_marshal_test2 %s %d\n", (*obj1)->str, (*obj1)->i);
1141 if (strcmp((*obj1)->str, "ABC"))
1143 if ((*obj1)->i != 5)
1150 string_marshal_test0 (char *str)
1152 if (strcmp (str, "TEST0"))
1159 string_marshal_test1 (const char **str)
1165 string_marshal_test2 (char **str)
1167 // printf ("string_marshal_test2 %s\n", *str);
1169 if (strcmp (*str, "TEST1"))
1176 string_marshal_test3 (char *str)
1189 STDCALL BlittableClass*
1190 TestBlittableClass (BlittableClass *vl)
1192 BlittableClass *res;
1194 // printf ("TestBlittableClass %d %d\n", vl->a, vl->b);
1200 res = g_new0 (BlittableClass, 1);
1201 memcpy (res, vl, sizeof (BlittableClass));
1203 res = g_new0 (BlittableClass, 1);
1211 typedef struct OSVERSIONINFO_STRUCT
1215 } OSVERSIONINFO_STRUCT;
1218 MyGetVersionEx (OSVERSIONINFO_STRUCT *osvi)
1221 // printf ("GOT %d %d\n", osvi->a, osvi->b);
1226 return osvi->a + osvi->b;
1230 BugGetVersionEx (int a, int b, int c, int d, int e, int f, int g, int h, OSVERSIONINFO_STRUCT *osvi)
1233 // printf ("GOT %d %d\n", osvi->a, osvi->b);
1238 return osvi->a + osvi->b;
1242 mono_test_marshal_point (point pt)
1244 // printf("point %g %g\n", pt.x, pt.y);
1245 if (pt.x == 1.25 && pt.y == 3.5)
1257 mono_test_marshal_mixed_point (mixed_point pt)
1259 // printf("mixed point %d %g\n", pt.x, pt.y);
1260 if (pt.x == 5 && pt.y == 6.75)
1267 mono_test_marshal_mixed_point_2 (mixed_point *pt)
1269 if (pt->x != 5 || pt->y != 6.75)
1279 marshal_test_ref_bool(int i, char *b1, short *b2, int *b3)
1282 if (*b1 != 0 && *b1 != 1)
1284 if (*b2 != 0 && *b2 != -1) /* variant_bool */
1286 if (*b3 != 0 && *b3 != 1)
1288 if (i == ((*b1 << 2) | (-*b2 << 1) | *b3))
1300 short b2; /* variant_bool */
1305 marshal_test_bool_struct(struct BoolStruct *s)
1308 if (s->b1 != 0 && s->b1 != 1)
1310 if (s->b2 != 0 && s->b2 != -1)
1312 if (s->b3 != 0 && s->b3 != 1)
1314 if (s->i == ((s->b1 << 2) | (-s->b2 << 1) | s->b3))
1323 mono_test_last_error (int err)
1333 mono_test_asany (void *ptr, int what)
1337 return (*(int*)ptr == 5) ? 0 : 1;
1339 return strcmp (ptr, "ABC") == 0 ? 0 : 1;
1341 simplestruct2 ss = *(simplestruct2*)ptr;
1343 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
1344 !strcmp (ss.d, "TEST") &&
1345 ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
1351 GError *error = NULL;
1354 s = g_utf16_to_utf8 (ptr, -1, NULL, NULL, &error);
1355 if (!strcmp (s, "ABC")) {
1365 g_assert_not_reached ();
1380 mono_test_marshal_asany_inout (void* ptr)
1382 AsAnyStruct* asAny = ptr;
1383 int res = asAny->i + asAny->j + asAny->k;
1394 * AMD64 marshalling tests.
1397 typedef struct amd64_struct1 {
1404 STDCALL amd64_struct1
1405 mono_test_marshal_amd64_pass_return_struct1 (amd64_struct1 s)
1415 typedef struct amd64_struct2 {
1420 STDCALL amd64_struct2
1421 mono_test_marshal_amd64_pass_return_struct2 (amd64_struct2 s)
1429 typedef struct amd64_struct3 {
1433 STDCALL amd64_struct3
1434 mono_test_marshal_amd64_pass_return_struct3 (amd64_struct3 s)
1441 typedef struct amd64_struct4 {
1445 STDCALL amd64_struct4
1446 mono_test_marshal_amd64_pass_return_struct4 (amd64_struct4 s)
1455 * IA64 marshalling tests.
1457 typedef struct test_struct5 {
1461 STDCALL test_struct5
1462 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, test_struct5 s, double d3, double d4)
1470 typedef struct test_struct6 {
1474 STDCALL test_struct6
1475 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, test_struct6 s, double d3, double d4)
1483 static guint32 custom_res [2];
1486 mono_test_marshal_pass_return_custom (int i, guint32 *ptr, int j)
1488 /* ptr will be freed by CleanupNative, so make a copy */
1489 custom_res [0] = 0; /* not allocated by AllocHGlobal */
1490 custom_res [1] = ptr [1];
1496 mono_test_marshal_pass_out_custom (int i, guint32 **ptr, int j)
1499 custom_res [1] = i + j + 10;
1507 mono_test_marshal_pass_inout_custom (int i, guint32 *ptr, int j)
1510 ptr [1] = i + ptr [1] + j;
1516 mono_test_marshal_pass_out_byval_custom (int i, guint32 *ptr, int j)
1518 return ptr == NULL ? 0 : 1;
1522 mono_test_marshal_pass_byref_custom (int i, guint32 **ptr, int j)
1530 mono_test_marshal_pass_return_custom2 (int i, guint32 *ptr, int j)
1532 g_assert_not_reached ();
1538 mono_test_marshal_pass_return_custom_null (int i, guint32 *ptr, int j)
1540 g_assert (ptr == NULL);
1545 typedef void *(STDCALL *PassReturnPtrDelegate) (void *ptr);
1548 mono_test_marshal_pass_return_custom_in_delegate (PassReturnPtrDelegate del)
1562 /* FIXME: Freed with FreeHGlobal */
1571 mono_test_marshal_pass_return_custom_null_in_delegate (PassReturnPtrDelegate del)
1573 void *ptr = del (NULL);
1575 return (ptr == NULL) ? 15 : 0;
1578 typedef int (STDCALL *ReturnEnumDelegate) (int e);
1581 mono_test_marshal_return_enum_delegate (ReturnEnumDelegate func)
1591 typedef BlittableStruct (STDCALL *SimpleDelegate10) (BlittableStruct ss);
1594 mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 delegate)
1596 BlittableStruct ss, res;
1603 res = delegate (ss);
1604 if (! ((res.a == -1) && (res.b == -2) && (res.c == -3) && (res.d == -55)))
1611 mono_test_stdcall_name_mangling (int a, int b, int c)
1617 * PASSING AND RETURNING SMALL STRUCTURES FROM DELEGATES TESTS
1624 typedef SmallStruct1 (STDCALL *SmallStructDelegate1) (SmallStruct1 ss);
1627 mono_test_marshal_small_struct_delegate1 (SmallStructDelegate1 delegate)
1629 SmallStruct1 ss, res;
1633 res = delegate (ss);
1634 if (! (res.i == -1))
1644 typedef SmallStruct2 (STDCALL *SmallStructDelegate2) (SmallStruct2 ss);
1647 mono_test_marshal_small_struct_delegate2 (SmallStructDelegate2 delegate)
1649 SmallStruct2 ss, res;
1654 res = delegate (ss);
1655 if (! ((res.i == -2) && (res.j == -3)))
1666 typedef SmallStruct3 (STDCALL *SmallStructDelegate3) (SmallStruct3 ss);
1669 mono_test_marshal_small_struct_delegate3 (SmallStructDelegate3 delegate)
1671 SmallStruct3 ss, res;
1676 res = delegate (ss);
1677 if (! ((res.i == -1) && (res.j == -2)))
1687 typedef SmallStruct4 (STDCALL *SmallStructDelegate4) (SmallStruct4 ss);
1690 mono_test_marshal_small_struct_delegate4 (SmallStructDelegate4 delegate)
1692 SmallStruct4 ss, res;
1696 res = delegate (ss);
1697 if (! (res.i == -1))
1707 typedef SmallStruct5 (STDCALL *SmallStructDelegate5) (SmallStruct5 ss);
1710 mono_test_marshal_small_struct_delegate5 (SmallStructDelegate5 delegate)
1712 SmallStruct5 ss, res;
1716 res = delegate (ss);
1717 if (! (res.i == -5))
1727 typedef SmallStruct6 (STDCALL *SmallStructDelegate6) (SmallStruct6 ss);
1730 mono_test_marshal_small_struct_delegate6 (SmallStructDelegate6 delegate)
1732 SmallStruct6 ss, res;
1737 res = delegate (ss);
1738 if (! ((res.i == -1) && (res.j == -2)))
1749 typedef SmallStruct7 (STDCALL *SmallStructDelegate7) (SmallStruct7 ss);
1752 mono_test_marshal_small_struct_delegate7 (SmallStructDelegate7 delegate)
1754 SmallStruct7 ss, res;
1759 res = delegate (ss);
1760 if (! ((res.i == -1) && (res.j == -2)))
1770 typedef SmallStruct8 (STDCALL *SmallStructDelegate8) (SmallStruct8 ss);
1773 mono_test_marshal_small_struct_delegate8 (SmallStructDelegate8 delegate)
1775 SmallStruct8 ss, res;
1779 res = delegate (ss);
1780 if (! ((res.i == -1.0)))
1790 typedef SmallStruct9 (STDCALL *SmallStructDelegate9) (SmallStruct9 ss);
1793 mono_test_marshal_small_struct_delegate9 (SmallStructDelegate9 delegate)
1795 SmallStruct9 ss, res;
1799 res = delegate (ss);
1800 if (! ((res.i == -1.0)))
1810 typedef SmallStruct10 (STDCALL *SmallStructDelegate10) (SmallStruct10 ss);
1813 mono_test_marshal_small_struct_delegate10 (SmallStructDelegate10 delegate)
1815 SmallStruct10 ss, res;
1820 res = delegate (ss);
1821 if (! ((res.i == -1.0) && (res.j == -2.0)))
1832 typedef SmallStruct11 (STDCALL *SmallStructDelegate11) (SmallStruct11 ss);
1835 mono_test_marshal_small_struct_delegate11 (SmallStructDelegate11 delegate)
1837 SmallStruct11 ss, res;
1842 res = delegate (ss);
1843 if (! ((res.i == -1.0) && (res.j == -2)))
1849 typedef int (STDCALL *ArrayDelegate) (int i, char *j, void *arr);
1852 mono_test_marshal_array_delegate (void *arr, int len, ArrayDelegate del)
1854 return del (len, NULL, arr);
1858 mono_test_marshal_out_array_delegate (int *arr, int len, ArrayDelegate del)
1860 del (len, NULL, arr);
1862 if ((arr [0] != 1) || (arr [1] != 2))
1869 mono_test_marshal_out_string_array_delegate (char **arr, int len, ArrayDelegate del)
1871 del (len, NULL, arr);
1873 if (!strcmp (arr [0], "ABC") && !strcmp (arr [1], "DEF"))
1879 typedef int (*CdeclDelegate) (int i, int j);
1882 mono_test_marshal_cdecl_delegate (CdeclDelegate del)
1886 for (i = 0; i < 1000; ++i)
1892 typedef char** (*ReturnStringArrayDelegate) (int i);
1895 mono_test_marshal_return_string_array_delegate (ReturnStringArrayDelegate d)
1903 if (strcmp (arr [0], "ABC") || strcmp (arr [1], "DEF"))
1914 add_delegate (int i, int j)
1920 mono_test_marshal_return_fnptr (void)
1922 return &add_delegate;
1925 int mono_xr (int code)
1927 printf ("codigo %x\n", code);
1939 mono_safe_handle_struct_ref (HandleStructs *x)
1941 printf ("Dingus Ref! \n");
1942 printf ("Values: %d %d %d %d\n", x->a, x->b, x->handle1, x->handle2);
1948 if (x->handle1 != (void*) 0x7080feed)
1951 if (x->handle2 != (void*) 0x1234abcd)
1958 mono_safe_handle_struct (HandleStructs x)
1960 printf ("Dingus Standard! \n");
1961 printf ("Values: %d %d %d %d\n", x.a, x.b, x.handle1, x.handle2);
1967 if (x.handle1 != (void*) 0x7080feed)
1970 if (x.handle2 != (void*) 0x1234abcd)
1981 mono_safe_handle_struct_simple (TrivialHandle x)
1983 printf ("The value is %d\n", x.a);
1984 return ((int)x.a) * 2;
1988 mono_safe_handle_return ()
1994 mono_safe_handle_ref (void **handle)
1997 *handle = (void *) 0xbad;
2001 *handle = (void *) 0x800d;
2010 mono_test_marshal_bstr_in(BSTR bstr)
2012 if (!wcscmp(bstr, L"mono_test_marshal_bstr_in"))
2018 mono_test_marshal_bstr_out(BSTR* bstr)
2020 *bstr = SysAllocString(L"mono_test_marshal_bstr_out");
2025 mono_test_marshal_bstr_in_null(BSTR bstr)
2033 mono_test_marshal_bstr_out_null(BSTR* bstr)
2040 mono_test_marshal_variant_in_sbyte(VARIANT variant)
2042 if (variant.vt == VT_I1 && variant.cVal == 100)
2048 mono_test_marshal_variant_in_byte(VARIANT variant)
2050 if (variant.vt == VT_UI1 && variant.bVal == 100)
2056 mono_test_marshal_variant_in_short(VARIANT variant)
2058 if (variant.vt == VT_I2 && variant.iVal == 314)
2064 mono_test_marshal_variant_in_ushort(VARIANT variant)
2066 if (variant.vt == VT_UI2 && variant.uiVal == 314)
2072 mono_test_marshal_variant_in_int(VARIANT variant)
2074 if (variant.vt == VT_I4 && variant.lVal == 314)
2080 mono_test_marshal_variant_in_uint(VARIANT variant)
2082 if (variant.vt == VT_UI4 && variant.ulVal == 314)
2088 mono_test_marshal_variant_in_long(VARIANT variant)
2090 if (variant.vt == VT_I8 && variant.llVal == 314)
2096 mono_test_marshal_variant_in_ulong(VARIANT variant)
2098 if (variant.vt == VT_UI8 && variant.ullVal == 314)
2104 mono_test_marshal_variant_in_float(VARIANT variant)
2106 if (variant.vt == VT_R4 && (variant.fltVal - 3.14)/3.14 < .001)
2112 mono_test_marshal_variant_in_double(VARIANT variant)
2114 if (variant.vt == VT_R8 && (variant.dblVal - 3.14)/3.14 < .001)
2120 mono_test_marshal_variant_in_bstr(VARIANT variant)
2122 if (variant.vt == VT_BSTR && !wcscmp(variant.bstrVal, L"PI"))
2128 mono_test_marshal_variant_in_bool_true (VARIANT variant)
2130 if (variant.vt == VT_BOOL && variant.boolVal == VARIANT_TRUE)
2136 mono_test_marshal_variant_in_bool_false (VARIANT variant)
2138 if (variant.vt == VT_BOOL && variant.boolVal == VARIANT_FALSE)
2144 mono_test_marshal_variant_out_sbyte(VARIANT* variant)
2146 variant->vt = VT_I1;
2147 variant->cVal = 100;
2153 mono_test_marshal_variant_out_byte(VARIANT* variant)
2155 variant->vt = VT_UI1;
2156 variant->bVal = 100;
2162 mono_test_marshal_variant_out_short(VARIANT* variant)
2164 variant->vt = VT_I2;
2165 variant->iVal = 314;
2171 mono_test_marshal_variant_out_ushort(VARIANT* variant)
2173 variant->vt = VT_UI2;
2174 variant->uiVal = 314;
2180 mono_test_marshal_variant_out_int(VARIANT* variant)
2182 variant->vt = VT_I4;
2183 variant->lVal = 314;
2189 mono_test_marshal_variant_out_uint(VARIANT* variant)
2191 variant->vt = VT_UI4;
2192 variant->ulVal = 314;
2198 mono_test_marshal_variant_out_long(VARIANT* variant)
2200 variant->vt = VT_I8;
2201 variant->llVal = 314;
2207 mono_test_marshal_variant_out_ulong(VARIANT* variant)
2209 variant->vt = VT_UI8;
2210 variant->ullVal = 314;
2216 mono_test_marshal_variant_out_float(VARIANT* variant)
2218 variant->vt = VT_R4;
2219 variant->fltVal = 3.14;
2225 mono_test_marshal_variant_out_double(VARIANT* variant)
2227 variant->vt = VT_R8;
2228 variant->dblVal = 3.14;
2234 mono_test_marshal_variant_out_bstr(VARIANT* variant)
2236 variant->vt = VT_BSTR;
2237 variant->bstrVal = SysAllocString(L"PI");
2243 mono_test_marshal_variant_out_bool_true (VARIANT* variant)
2245 variant->vt = VT_BOOL;
2246 variant->boolVal = VARIANT_TRUE;
2252 mono_test_marshal_variant_out_bool_false (VARIANT* variant)
2254 variant->vt = VT_BOOL;
2255 variant->boolVal = VARIANT_FALSE;
2260 typedef int (STDCALL *VarFunc) (int vt, VARIANT variant);
2261 typedef int (STDCALL *VarRefFunc) (int vt, VARIANT* variant);
2264 mono_test_marshal_variant_in_sbyte_unmanaged(VarFunc func)
2269 return func (VT_I1, vt);
2273 mono_test_marshal_variant_in_byte_unmanaged(VarFunc func)
2278 return func (VT_UI1, vt);
2282 mono_test_marshal_variant_in_short_unmanaged(VarFunc func)
2287 return func (VT_I2, vt);
2291 mono_test_marshal_variant_in_ushort_unmanaged(VarFunc func)
2296 return func (VT_UI2, vt);
2300 mono_test_marshal_variant_in_int_unmanaged(VarFunc func)
2305 return func (VT_I4, vt);
2309 mono_test_marshal_variant_in_uint_unmanaged(VarFunc func)
2314 return func (VT_UI4, vt);
2318 mono_test_marshal_variant_in_long_unmanaged(VarFunc func)
2323 return func (VT_I8, vt);
2327 mono_test_marshal_variant_in_ulong_unmanaged(VarFunc func)
2332 return func (VT_UI8, vt);
2336 mono_test_marshal_variant_in_float_unmanaged(VarFunc func)
2341 return func (VT_R4, vt);
2345 mono_test_marshal_variant_in_double_unmanaged(VarFunc func)
2350 return func (VT_R8, vt);
2354 mono_test_marshal_variant_in_bstr_unmanaged(VarFunc func)
2358 vt.bstrVal = SysAllocString(L"PI");
2359 return func (VT_BSTR, vt);
2363 mono_test_marshal_variant_in_bool_true_unmanaged(VarFunc func)
2367 vt.boolVal = VARIANT_TRUE;
2368 return func (VT_BOOL, vt);
2372 mono_test_marshal_variant_in_bool_false_unmanaged(VarFunc func)
2376 vt.boolVal = VARIANT_FALSE;
2377 return func (VT_BOOL, vt);
2381 mono_test_marshal_variant_out_sbyte_unmanaged(VarRefFunc func)
2386 if (vt.vt == VT_I1 && vt.cVal == -100)
2392 mono_test_marshal_variant_out_byte_unmanaged(VarRefFunc func)
2397 if (vt.vt == VT_UI1 && vt.bVal == 100)
2403 mono_test_marshal_variant_out_short_unmanaged(VarRefFunc func)
2408 if (vt.vt == VT_I2 && vt.iVal == -100)
2414 mono_test_marshal_variant_out_ushort_unmanaged(VarRefFunc func)
2419 if (vt.vt == VT_UI2 && vt.uiVal == 100)
2425 mono_test_marshal_variant_out_int_unmanaged(VarRefFunc func)
2430 if (vt.vt == VT_I4 && vt.lVal == -100)
2436 mono_test_marshal_variant_out_uint_unmanaged(VarRefFunc func)
2441 if (vt.vt == VT_UI4 && vt.ulVal == 100)
2447 mono_test_marshal_variant_out_long_unmanaged(VarRefFunc func)
2452 if (vt.vt == VT_I8 && vt.llVal == -100)
2458 mono_test_marshal_variant_out_ulong_unmanaged(VarRefFunc func)
2463 if (vt.vt == VT_UI8 && vt.ullVal == 100)
2469 mono_test_marshal_variant_out_float_unmanaged(VarRefFunc func)
2474 if (vt.vt == VT_R4 && fabs (vt.fltVal - 3.14f) < 1e-10)
2480 mono_test_marshal_variant_out_double_unmanaged(VarRefFunc func)
2485 if (vt.vt == VT_R8 && fabs (vt.dblVal - 3.14) < 1e-10)
2491 mono_test_marshal_variant_out_bstr_unmanaged(VarRefFunc func)
2495 func (VT_BSTR, &vt);
2496 if (vt.vt == VT_BSTR && !wcscmp(vt.bstrVal, L"PI"))
2502 mono_test_marshal_variant_out_bool_true_unmanaged(VarRefFunc func)
2506 func (VT_BOOL, &vt);
2507 if (vt.vt == VT_BOOL && vt.boolVal == VARIANT_TRUE)
2513 mono_test_marshal_variant_out_bool_false_unmanaged(VarRefFunc func)
2517 func (VT_BOOL, &vt);
2518 if (vt.vt == VT_BOOL && vt.boolVal == VARIANT_TRUE)
2524 #define COM_STDCALL __stdcall
2526 #define COM_STDCALL __attribute__((stdcall))
2529 typedef struct MonoComObject MonoComObject;
2533 int (COM_STDCALL *QueryInterface)(MonoComObject* pUnk, gpointer riid, gpointer* ppv);
2534 int (COM_STDCALL *AddRef)(MonoComObject* pUnk);
2535 int (COM_STDCALL *Release)(MonoComObject* pUnk);
2536 int (COM_STDCALL *Add)(MonoComObject* pUnk, int a, int b, int* c);
2537 int (COM_STDCALL *Subtract)(MonoComObject* pUnk, int a, int b, int* c);
2538 int (COM_STDCALL *Same)(MonoComObject* pUnk, MonoComObject* *pOut);
2539 int (COM_STDCALL *Different)(MonoComObject* pUnk, MonoComObject* *pOut);
2542 struct MonoComObject
2548 DEFINE_GUID(IID_IMath, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
2549 DEFINE_GUID(IID_IMonoUnknown, 0, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0, 0x46);
2550 DEFINE_GUID(IID_IMonoDispatch, 0x00020400, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0, 0x46);
2552 int COM_STDCALL MonoQueryInterface(MonoComObject* pUnk, gpointer riid, gpointer* ppv)
2555 if (!memcmp(riid, &IID_IMonoUnknown, sizeof(GUID))) {
2559 else if (!memcmp(riid, &IID_IMath, sizeof(GUID))) {
2563 else if (!memcmp(riid, &IID_IMonoDispatch, sizeof(GUID))) {
2567 return E_NOINTERFACE;
2570 int COM_STDCALL MonoAddRef(MonoComObject* pUnk)
2572 return ++(pUnk->m_ref);
2575 int COM_STDCALL MonoRelease(MonoComObject* pUnk)
2577 return --(pUnk->m_ref);
2580 int COM_STDCALL Add(MonoComObject* pUnk, int a, int b, int* c)
2586 int COM_STDCALL Subtract(MonoComObject* pUnk, int a, int b, int* c)
2592 static void create_com_object (MonoComObject** pOut);
2593 static MonoComObject* same_com_object = NULL;
2595 int COM_STDCALL Same(MonoComObject* pUnk, MonoComObject** pOut)
2597 if (!same_com_object)
2598 create_com_object (&same_com_object);
2599 *pOut = same_com_object;
2603 int COM_STDCALL Different(MonoComObject* pUnk, MonoComObject** pOut)
2605 create_com_object (pOut);
2609 static void create_com_object (MonoComObject** pOut)
2611 *pOut = g_new0 (MonoComObject, 1);
2612 (*pOut)->vtbl = g_new0 (MonoIUnknown, 1);
2615 (*pOut)->vtbl->QueryInterface = MonoQueryInterface;
2616 (*pOut)->vtbl->AddRef = MonoAddRef;
2617 (*pOut)->vtbl->Release = MonoRelease;
2618 (*pOut)->vtbl->Add = Add;
2619 (*pOut)->vtbl->Subtract = Subtract;
2620 (*pOut)->vtbl->Same = Same;
2621 (*pOut)->vtbl->Different = Different;
2625 mono_test_marshal_com_object_create(MonoComObject* *pUnk)
2627 create_com_object (pUnk);
2633 mono_test_marshal_com_object_destroy(MonoComObject *pUnk)
2635 int ref = --(pUnk->m_ref);
2643 mono_test_marshal_com_object_ref_count(MonoComObject *pUnk)