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 static void* marshal_alloc (gsize size)
37 CoTaskMemAlloc (size);
43 STDCALL unsigned short*
44 test_lpwstr_marshal (unsigned short* chars, long length)
49 res = marshal_alloc (2 * (length + 1));
51 // printf("test_lpwstr_marshal()\n");
53 while ( i < length ) {
54 // printf("X|%u|\n", chars[i]);
66 test_lpwstr_marshal_out (unsigned short** chars)
69 const char abc[] = "ABC";
70 glong len = strlen(abc);
72 *chars = marshal_alloc (2 * (len + 1));
75 (*chars) [i] = abc[i];
89 mono_union_test_1 (union_test_1_type u1) {
90 // printf ("Got values %d %d %d\n", u1.b, u1.a, u1.c);
91 return u1.a + u1.b + u1.c;
95 mono_return_int (int a) {
96 // printf ("Got value %d\n", a);
106 mono_return_int_ss (struct ss a) {
107 // printf ("Got value %d\n", a.i);
112 mono_return_ss (struct ss a) {
113 // printf ("Got value %d\n", a.i);
124 mono_return_sc1 (struct sc1 a) {
125 // printf ("Got value %d\n", a.c[0]);
137 mono_return_sc3 (struct sc3 a) {
138 // printf ("Got values %d %d %d\n", a.c[0], a.c[1], a.c[2]);
151 mono_return_sc5 (struct sc5 a) {
152 // printf ("Got values %d %d %d %d %d\n", a.c[0], a.c[1], a.c[2], a.c[3], a.c[4]);
168 mono_return_int_su (union su a) {
169 // printf ("Got value %d\n", a.i1);
174 mono_test_many_int_arguments (int a, int b, int c, int d, int e,
175 int f, int g, int h, int i, int j);
177 mono_test_many_short_arguments (short a, short b, short c, short d, short e,
178 short f, short g, short h, short i, short j);
180 mono_test_many_char_arguments (char a, char b, char c, char d, char e,
181 char f, char g, char h, char i, char j);
184 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)
186 return a + b + c + d + e + f + g + h + i + j;
190 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)
192 return a + b + c + d + e + f + g + h + i + j;
196 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)
198 return a + b + c + d + e + f + g + h + i + j;
202 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)
204 return a + b + c + d + e + f + g + h + i + j;
208 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)
210 return a + b + c + d + e + f + g + h + i + j;
214 mono_test_split_double_arguments (double a, double b, float c, double d, double e)
216 return a + b + c + d + e;
220 mono_test_puts_static (char *s)
222 // printf ("TEST %s\n", s);
226 typedef int (STDCALL *SimpleDelegate3) (int a, int b);
229 mono_invoke_delegate (SimpleDelegate3 delegate)
233 // printf ("start invoke %p\n", delegate);
235 res = delegate (2, 3);
237 // printf ("end invoke\n");
243 mono_test_marshal_char (short a1)
252 mono_test_marshal_char_array (gunichar2 *s)
254 const char m[] = "abcdef";
258 s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
267 mono_test_empty_pinvoke (int i)
273 mono_test_marshal_bool_byref (int a, int *b, int c)
283 mono_test_marshal_bool_in_as_I1_U1 (char bTrue, char bFalse)
293 mono_test_marshal_bool_out_as_I1_U1 (char* bTrue, char* bFalse)
295 if (!bTrue || !bFalse)
305 mono_test_marshal_bool_ref_as_I1_U1 (char* bTrue, char* bFalse)
307 if (!bTrue || !bFalse)
322 mono_test_marshal_array (int *a1)
326 for (i = 0; i < 50; i++)
333 mono_test_marshal_inout_array (int *a1)
337 for (i = 0; i < 50; i++) {
339 a1 [i] = 50 - a1 [i];
346 mono_test_marshal_out_array (int *a1)
350 for (i = 0; i < 50; i++) {
358 mono_test_marshal_inout_nonblittable_array (gunichar2 *a1)
362 for (i = 0; i < 10; i++) {
383 mono_test_return_vtype (int i)
386 static gunichar2 test2 [] = { 'T', 'E', 'S', 'T', '2', 0 };
398 mono_test_delegate_struct (void)
400 // printf ("TEST\n");
403 typedef char* (STDCALL *ReturnStringDelegate) (const char *s);
406 mono_test_return_string (ReturnStringDelegate func)
410 // printf ("mono_test_return_string\n");
415 // printf ("got string: %s\n", res);
416 return g_strdup ("12345");
419 typedef int (STDCALL *RefVTypeDelegate) (int a, simplestruct *ss, int b);
422 mono_test_ref_vtype (int a, simplestruct *ss, int b, RefVTypeDelegate func)
424 if (a == 1 && b == 2 && ss->a == 0 && ss->b == 1 && ss->c == 0 &&
425 !strcmp (ss->d, "TEST1")) {
431 return func (a, ss, b);
437 typedef int (STDCALL *OutVTypeDelegate) (int a, simplestruct *ss, int b);
440 mono_test_marshal_out_struct (int a, simplestruct *ss, int b, OutVTypeDelegate func)
442 /* Check that the input pointer is ignored */
443 ss->d = (gpointer)0x12345678;
447 if (ss->a && ss->b && ss->c && !strcmp (ss->d, "TEST3"))
455 SimpleDelegate func, func2;
458 STDCALL DelegateStruct
459 mono_test_marshal_delegate_struct (DelegateStruct ds)
463 res.a = ds.func (ds.a) + ds.func2 (ds.a);
465 res.func2 = ds.func2;
471 mono_test_marshal_struct (simplestruct ss)
473 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
474 !strcmp (ss.d, "TEST"))
481 mono_test_marshal_byref_struct (simplestruct *ss, int a, int b, int c, char *d)
483 gboolean res = (ss->a == a && ss->b == b && ss->c == c && strcmp (ss->d, d) == 0);
485 marshal_free (ss->d);
490 ss->d = g_strdup ("DEF");
507 mono_test_marshal_struct2 (simplestruct2 ss)
509 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
510 !strcmp (ss.d, "TEST") &&
511 ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
517 /* on HP some of the struct should be on the stack and not in registers */
519 mono_test_marshal_struct2_2 (int i, int j, int k, simplestruct2 ss)
521 if (i != 10 || j != 11 || k != 12)
523 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
524 !strcmp (ss.d, "TEST") &&
525 ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
532 mono_test_marshal_lpstruct (simplestruct *ss)
534 if (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
535 !strcmp (ss->d, "TEST"))
542 mono_test_marshal_lpstruct_blittable (point *p)
544 if (p->x == 1.0 && p->y == 2.0)
551 mono_test_marshal_struct_array (simplestruct2 *ss)
553 if (! (ss[0].a == 0 && ss[0].b == 1 && ss[0].c == 0 &&
554 !strcmp (ss[0].d, "TEST") &&
555 ss[0].e == 99 && ss[0].f == 1.5 && ss[0].g == 42 && ss[0].h == (guint64)123))
558 if (! (ss[1].a == 0 && ss[1].b == 0 && ss[1].c == 0 &&
559 !strcmp (ss[1].d, "TEST2") &&
560 ss[1].e == 100 && ss[1].f == 2.5 && ss[1].g == 43 && ss[1].h == (guint64)124))
566 typedef struct long_align_struct {
573 mono_test_marshal_long_align_struct_array (long_align_struct *ss)
575 return ss[0].a + ss[0].b + ss[0].c + ss[1].a + ss[1].b + ss[1].c;
578 STDCALL simplestruct2 *
579 mono_test_marshal_class (int i, int j, int k, simplestruct2 *ss, int l)
586 if (i != 10 || j != 11 || k != 12 || l != 14)
588 if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
589 !strcmp (ss->d, "TEST") &&
590 ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
593 res = g_new0 (simplestruct2, 1);
594 memcpy (res, ss, sizeof (simplestruct2));
595 res->d = g_strdup ("TEST");
600 mono_test_marshal_byref_class (simplestruct2 **ssp)
602 simplestruct2 *ss = *ssp;
605 if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
606 !strcmp (ss->d, "TEST") &&
607 ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
610 res = g_new0 (simplestruct2, 1);
611 memcpy (res, ss, sizeof (simplestruct2));
612 res->d = g_strdup ("TEST-RES");
624 /* Yes, this is correct, we are only trying to determine the value of the stack here */
630 reliable_delegate (int a)
636 * Checks whether get_sp() works as expected. It doesn't work with gcc-2.95.3 on linux.
639 is_get_sp_reliable (void)
643 reliable_delegate(1);
645 reliable_delegate(1);
651 mono_test_marshal_delegate (SimpleDelegate delegate)
655 /* Check that the delegate wrapper is stdcall */
660 if (is_get_sp_reliable())
661 g_assert (sp1 == sp2);
666 STDCALL SimpleDelegate
667 mono_test_marshal_return_delegate (SimpleDelegate delegate)
673 return_plus_one (int i)
678 STDCALL SimpleDelegate
679 mono_test_marshal_return_delegate_2 ()
681 return return_plus_one;
684 typedef simplestruct (STDCALL *SimpleDelegate2) (simplestruct ss);
687 is_utf16_equals (gunichar2 *s1, const char *s2)
692 s = g_utf16_to_utf8 (s1, -1, NULL, NULL, NULL);
693 res = strcmp (s, s2);
700 mono_test_marshal_delegate2 (SimpleDelegate2 delegate)
702 simplestruct ss, res;
708 ss.d2 = g_utf8_to_utf16 ("TEST2", -1, NULL, NULL, NULL);
711 if (! (res.a && !res.b && res.c && !strcmp (res.d, "TEST-RES") && is_utf16_equals (res.d2, "TEST2-RES")))
717 typedef simplestruct* (STDCALL *SimpleDelegate4) (simplestruct *ss);
720 mono_test_marshal_delegate4 (SimpleDelegate4 delegate)
731 res = delegate (&ss);
735 /* Check return value */
736 if (! (!res->a && res->b && !res->c && !strcmp (res->d, "TEST")))
739 /* Check NULL argument and NULL result */
740 res = delegate (NULL);
747 typedef int (STDCALL *SimpleDelegate5) (simplestruct **ss);
750 mono_test_marshal_delegate5 (SimpleDelegate5 delegate)
763 res = delegate (&ptr);
767 if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
774 mono_test_marshal_delegate6 (SimpleDelegate5 delegate)
778 res = delegate (NULL);
783 typedef int (STDCALL *SimpleDelegate7) (simplestruct **ss);
786 mono_test_marshal_delegate7 (SimpleDelegate7 delegate)
791 /* Check that the input pointer is ignored */
792 ptr = (gpointer)0x12345678;
794 res = delegate (&ptr);
798 if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
804 typedef int (STDCALL *InOutByvalClassDelegate) (simplestruct *ss);
807 mono_test_marshal_inout_byval_class_delegate (InOutByvalClassDelegate delegate)
815 ss.d = g_strdup_printf ("%s", "FOO");
817 res = delegate (&ss);
821 if (!(ss.a && !ss.b && ss.c && !strcmp (ss.d, "RES")))
827 typedef int (STDCALL *SimpleDelegate8) (gunichar2 *s);
830 mono_test_marshal_delegate8 (SimpleDelegate8 delegate, gunichar2 *s)
835 typedef int (STDCALL *return_int_fnt) (int i);
836 typedef int (STDCALL *SimpleDelegate9) (return_int_fnt d);
839 mono_test_marshal_delegate9 (SimpleDelegate9 delegate, gpointer ftn)
841 return delegate (ftn);
851 mono_test_marshal_delegate10 (SimpleDelegate9 delegate)
853 return delegate (return_self);
856 typedef int (STDCALL *PrimitiveByrefDelegate) (int *i);
859 mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate delegate)
863 int res = delegate (&i);
873 typedef int (STDCALL *return_int_delegate) (int i);
875 typedef return_int_delegate (STDCALL *ReturnDelegateDelegate) (void);
878 mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d)
884 mono_test_marshal_stringbuilder (char *s, int n)
886 const char m[] = "This is my message. Isn't it nice?";
888 if (strcmp (s, "ABCD") != 0)
896 mono_test_marshal_stringbuilder_default (char *s, int n)
898 const char m[] = "This is my message. Isn't it nice?";
906 mono_test_marshal_stringbuilder_unicode (gunichar2 *s, int n)
908 const char m[] = "This is my message. Isn't it nice?";
912 s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
931 mono_test_marshal_empty_string_array (char **array)
933 return (array == NULL) ? 0 : 1;
937 mono_test_marshal_string_array (char **array)
939 if (strcmp (array [0], "ABC"))
941 if (strcmp (array [1], "DEF"))
944 if (array [2] != NULL)
951 mono_test_marshal_byref_string_array (char ***array)
956 if (strcmp ((*array) [0], "Alpha"))
958 if (strcmp ((*array) [1], "Beta"))
960 if (strcmp ((*array) [2], "Gamma"))
967 mono_test_marshal_stringbuilder_array (char **array)
969 if (strcmp (array [0], "ABC"))
971 if (strcmp (array [1], "DEF"))
974 strcpy (array [0], "DEF");
975 strcpy (array [1], "ABC");
981 mono_test_marshal_unicode_string_array (gunichar2 **array, char **array2)
983 GError *error = NULL;
986 s = g_utf16_to_utf8 (array [0], -1, NULL, NULL, &error);
987 if (strcmp (s, "ABC")) {
994 s = g_utf16_to_utf8 (array [1], -1, NULL, NULL, &error);
995 if (strcmp (s, "DEF")) {
1002 if (strcmp (array2 [0], "ABC"))
1005 if (strcmp (array2 [1], "DEF"))
1011 /* this does not work on Redhat gcc 2.96 */
1013 mono_test_empty_struct (int a, EmptyStruct es, int b)
1015 // printf ("mono_test_empty_struct %d %d\n", a, b);
1017 // Intel icc on ia64 passes 'es' in 2 registers
1018 #if defined(__ia64) && defined(__INTEL_COMPILER)
1021 if (a == 1 && b == 2)
1031 STDCALL ByValStrStruct *
1032 mono_test_byvalstr_gen (void)
1034 ByValStrStruct *ret;
1036 ret = malloc(sizeof(ByValStrStruct));
1037 memset(ret, 'a', sizeof(ByValStrStruct)-1);
1038 ret->a[sizeof(ByValStrStruct)-1] = 0;
1044 mono_test_byvalstr_check (ByValStrStruct* data, char* correctString)
1048 ret = strcmp(data->a, correctString);
1049 // printf ("T1: %s\n", data->a);
1050 // printf ("T2: %s\n", correctString);
1052 marshal_free (data);
1059 } ByValStrStruct_Unicode;
1062 mono_test_byvalstr_check_unicode (ByValStrStruct_Unicode *ref, int test)
1064 if (ref->flag != 0x1234abcd){
1065 printf ("overwritten data");
1069 if (test == 1 || test == 3){
1070 if (ref->a [0] != '1' ||
1071 ref->a [1] != '2' ||
1077 if (ref->a [0] != '1' ||
1086 NameManglingAnsi (char *data)
1088 return data [0] + data [1] + data [2];
1092 NameManglingAnsiA (char *data)
1094 g_assert_not_reached ();
1098 NameManglingAnsiW (char *data)
1100 g_assert_not_reached ();
1104 NameManglingAnsi2A (char *data)
1106 return data [0] + data [1] + data [2];
1110 NameManglingAnsi2W (char *data)
1112 g_assert_not_reached ();
1116 NameManglingUnicode (char *data)
1118 g_assert_not_reached ();
1122 NameManglingUnicodeW (gunichar2 *data)
1124 return data [0] + data [1] + data [2];
1128 NameManglingUnicode2 (gunichar2 *data)
1130 return data [0] + data [1] + data [2];
1134 NameManglingAutoW (char *data)
1137 return (data [0] + data [1] + data [2]) == 131 ? 0 : 1;
1139 g_assert_not_reached ();
1144 NameManglingAuto (char *data)
1147 return (data [0] + data [1] + data [2]) == 198 ? 0 : 1;
1149 g_assert_not_reached ();
1153 typedef int (STDCALL *intcharFunc)(const char*);
1156 callFunction (intcharFunc f)
1167 class_marshal_test0 (SimpleObj *obj1)
1169 // printf ("class_marshal_test0 %s %d\n", obj1->str, obj1->i);
1171 if (strcmp(obj1->str, "T1"))
1180 class_marshal_test4 (SimpleObj *obj1)
1189 class_marshal_test1 (SimpleObj **obj1)
1191 SimpleObj *res = malloc (sizeof (SimpleObj));
1193 res->str = g_strdup ("ABC");
1200 class_marshal_test2 (SimpleObj **obj1)
1202 // printf ("class_marshal_test2 %s %d\n", (*obj1)->str, (*obj1)->i);
1204 if (strcmp((*obj1)->str, "ABC"))
1206 if ((*obj1)->i != 5)
1213 string_marshal_test0 (char *str)
1215 if (strcmp (str, "TEST0"))
1222 string_marshal_test1 (const char **str)
1224 *str = g_strdup ("TEST1");
1228 string_marshal_test2 (char **str)
1230 // printf ("string_marshal_test2 %s\n", *str);
1232 if (strcmp (*str, "TEST1"))
1239 string_marshal_test3 (char *str)
1252 STDCALL BlittableClass*
1253 TestBlittableClass (BlittableClass *vl)
1255 BlittableClass *res;
1257 // printf ("TestBlittableClass %d %d\n", vl->a, vl->b);
1263 res = g_new0 (BlittableClass, 1);
1264 memcpy (res, vl, sizeof (BlittableClass));
1266 res = g_new0 (BlittableClass, 1);
1274 typedef struct OSVERSIONINFO_STRUCT
1278 } OSVERSIONINFO_STRUCT;
1281 MyGetVersionEx (OSVERSIONINFO_STRUCT *osvi)
1284 // printf ("GOT %d %d\n", osvi->a, osvi->b);
1289 return osvi->a + osvi->b;
1293 BugGetVersionEx (int a, int b, int c, int d, int e, int f, int g, int h, OSVERSIONINFO_STRUCT *osvi)
1296 // printf ("GOT %d %d\n", osvi->a, osvi->b);
1301 return osvi->a + osvi->b;
1305 mono_test_marshal_point (point pt)
1307 // printf("point %g %g\n", pt.x, pt.y);
1308 if (pt.x == 1.25 && pt.y == 3.5)
1320 mono_test_marshal_mixed_point (mixed_point pt)
1322 // printf("mixed point %d %g\n", pt.x, pt.y);
1323 if (pt.x == 5 && pt.y == 6.75)
1330 mono_test_marshal_mixed_point_2 (mixed_point *pt)
1332 if (pt->x != 5 || pt->y != 6.75)
1342 marshal_test_ref_bool(int i, char *b1, short *b2, int *b3)
1345 if (*b1 != 0 && *b1 != 1)
1347 if (*b2 != 0 && *b2 != -1) /* variant_bool */
1349 if (*b3 != 0 && *b3 != 1)
1351 if (i == ((*b1 << 2) | (-*b2 << 1) | *b3))
1363 short b2; /* variant_bool */
1368 marshal_test_bool_struct(struct BoolStruct *s)
1371 if (s->b1 != 0 && s->b1 != 1)
1373 if (s->b2 != 0 && s->b2 != -1)
1375 if (s->b3 != 0 && s->b3 != 1)
1377 if (s->i == ((s->b1 << 2) | (-s->b2 << 1) | s->b3))
1386 mono_test_last_error (int err)
1396 mono_test_asany (void *ptr, int what)
1400 return (*(int*)ptr == 5) ? 0 : 1;
1402 return strcmp (ptr, "ABC") == 0 ? 0 : 1;
1404 simplestruct2 ss = *(simplestruct2*)ptr;
1406 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
1407 !strcmp (ss.d, "TEST") &&
1408 ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
1414 GError *error = NULL;
1417 s = g_utf16_to_utf8 (ptr, -1, NULL, NULL, &error);
1418 if (!strcmp (s, "ABC")) {
1428 g_assert_not_reached ();
1443 mono_test_marshal_asany_in (void* ptr)
1445 AsAnyStruct* asAny = ptr;
1446 int res = asAny->i + asAny->j + asAny->k;
1452 mono_test_marshal_asany_inout (void* ptr)
1454 AsAnyStruct* asAny = ptr;
1455 int res = asAny->i + asAny->j + asAny->k;
1457 marshal_free (asAny->s);
1468 mono_test_marshal_asany_out (void* ptr)
1470 AsAnyStruct* asAny = ptr;
1471 int res = asAny->i + asAny->j + asAny->k;
1482 * AMD64 marshalling tests.
1485 typedef struct amd64_struct1 {
1492 STDCALL amd64_struct1
1493 mono_test_marshal_amd64_pass_return_struct1 (amd64_struct1 s)
1503 typedef struct amd64_struct2 {
1508 STDCALL amd64_struct2
1509 mono_test_marshal_amd64_pass_return_struct2 (amd64_struct2 s)
1517 typedef struct amd64_struct3 {
1521 STDCALL amd64_struct3
1522 mono_test_marshal_amd64_pass_return_struct3 (amd64_struct3 s)
1529 typedef struct amd64_struct4 {
1533 STDCALL amd64_struct4
1534 mono_test_marshal_amd64_pass_return_struct4 (amd64_struct4 s)
1543 * IA64 marshalling tests.
1545 typedef struct test_struct5 {
1549 STDCALL test_struct5
1550 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, test_struct5 s, double d3, double d4)
1558 typedef struct test_struct6 {
1562 STDCALL test_struct6
1563 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, test_struct6 s, double d3, double d4)
1571 static guint32 custom_res [2];
1574 mono_test_marshal_pass_return_custom (int i, guint32 *ptr, int j)
1576 /* ptr will be freed by CleanupNative, so make a copy */
1577 custom_res [0] = 0; /* not allocated by AllocHGlobal */
1578 custom_res [1] = ptr [1];
1584 mono_test_marshal_pass_out_custom (int i, guint32 **ptr, int j)
1587 custom_res [1] = i + j + 10;
1595 mono_test_marshal_pass_inout_custom (int i, guint32 *ptr, int j)
1598 ptr [1] = i + ptr [1] + j;
1604 mono_test_marshal_pass_out_byval_custom (int i, guint32 *ptr, int j)
1606 return ptr == NULL ? 0 : 1;
1610 mono_test_marshal_pass_byref_custom (int i, guint32 **ptr, int j)
1618 mono_test_marshal_pass_return_custom2 (int i, guint32 *ptr, int j)
1620 g_assert_not_reached ();
1626 mono_test_marshal_pass_return_custom_null (int i, guint32 *ptr, int j)
1628 g_assert (ptr == NULL);
1633 typedef void *(STDCALL *PassReturnPtrDelegate) (void *ptr);
1636 mono_test_marshal_pass_return_custom_in_delegate (PassReturnPtrDelegate del)
1650 /* FIXME: Freed with FreeHGlobal */
1659 mono_test_marshal_pass_return_custom_null_in_delegate (PassReturnPtrDelegate del)
1661 void *ptr = del (NULL);
1663 return (ptr == NULL) ? 15 : 0;
1666 typedef void (STDCALL *CustomOutParamDelegate) (void **pptr);
1669 mono_test_marshal_custom_out_param_delegate (CustomOutParamDelegate del)
1681 typedef int (STDCALL *ReturnEnumDelegate) (int e);
1684 mono_test_marshal_return_enum_delegate (ReturnEnumDelegate func)
1694 typedef BlittableStruct (STDCALL *SimpleDelegate10) (BlittableStruct ss);
1697 mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 delegate)
1699 BlittableStruct ss, res;
1706 res = delegate (ss);
1707 if (! ((res.a == -1) && (res.b == -2) && (res.c == -3) && (res.d == -55)))
1714 mono_test_stdcall_name_mangling (int a, int b, int c)
1720 * PASSING AND RETURNING SMALL STRUCTURES FROM DELEGATES TESTS
1727 typedef SmallStruct1 (STDCALL *SmallStructDelegate1) (SmallStruct1 ss);
1730 mono_test_marshal_small_struct_delegate1 (SmallStructDelegate1 delegate)
1732 SmallStruct1 ss, res;
1736 res = delegate (ss);
1737 if (! (res.i == -1))
1747 typedef SmallStruct2 (STDCALL *SmallStructDelegate2) (SmallStruct2 ss);
1750 mono_test_marshal_small_struct_delegate2 (SmallStructDelegate2 delegate)
1752 SmallStruct2 ss, res;
1757 res = delegate (ss);
1758 if (! ((res.i == -2) && (res.j == -3)))
1769 typedef SmallStruct3 (STDCALL *SmallStructDelegate3) (SmallStruct3 ss);
1772 mono_test_marshal_small_struct_delegate3 (SmallStructDelegate3 delegate)
1774 SmallStruct3 ss, res;
1779 res = delegate (ss);
1780 if (! ((res.i == -1) && (res.j == -2)))
1790 typedef SmallStruct4 (STDCALL *SmallStructDelegate4) (SmallStruct4 ss);
1793 mono_test_marshal_small_struct_delegate4 (SmallStructDelegate4 delegate)
1795 SmallStruct4 ss, res;
1799 res = delegate (ss);
1800 if (! (res.i == -1))
1810 typedef SmallStruct5 (STDCALL *SmallStructDelegate5) (SmallStruct5 ss);
1813 mono_test_marshal_small_struct_delegate5 (SmallStructDelegate5 delegate)
1815 SmallStruct5 ss, res;
1819 res = delegate (ss);
1820 if (! (res.i == -5))
1830 typedef SmallStruct6 (STDCALL *SmallStructDelegate6) (SmallStruct6 ss);
1833 mono_test_marshal_small_struct_delegate6 (SmallStructDelegate6 delegate)
1835 SmallStruct6 ss, res;
1840 res = delegate (ss);
1841 if (! ((res.i == -1) && (res.j == -2)))
1852 typedef SmallStruct7 (STDCALL *SmallStructDelegate7) (SmallStruct7 ss);
1855 mono_test_marshal_small_struct_delegate7 (SmallStructDelegate7 delegate)
1857 SmallStruct7 ss, res;
1862 res = delegate (ss);
1863 if (! ((res.i == -1) && (res.j == -2)))
1873 typedef SmallStruct8 (STDCALL *SmallStructDelegate8) (SmallStruct8 ss);
1876 mono_test_marshal_small_struct_delegate8 (SmallStructDelegate8 delegate)
1878 SmallStruct8 ss, res;
1882 res = delegate (ss);
1883 if (! ((res.i == -1.0)))
1893 typedef SmallStruct9 (STDCALL *SmallStructDelegate9) (SmallStruct9 ss);
1896 mono_test_marshal_small_struct_delegate9 (SmallStructDelegate9 delegate)
1898 SmallStruct9 ss, res;
1902 res = delegate (ss);
1903 if (! ((res.i == -1.0)))
1913 typedef SmallStruct10 (STDCALL *SmallStructDelegate10) (SmallStruct10 ss);
1916 mono_test_marshal_small_struct_delegate10 (SmallStructDelegate10 delegate)
1918 SmallStruct10 ss, res;
1923 res = delegate (ss);
1924 if (! ((res.i == -1.0) && (res.j == -2.0)))
1935 typedef SmallStruct11 (STDCALL *SmallStructDelegate11) (SmallStruct11 ss);
1938 mono_test_marshal_small_struct_delegate11 (SmallStructDelegate11 delegate)
1940 SmallStruct11 ss, res;
1945 res = delegate (ss);
1946 if (! ((res.i == -1.0) && (res.j == -2)))
1952 typedef int (STDCALL *ArrayDelegate) (int i, char *j, void *arr);
1955 mono_test_marshal_array_delegate (void *arr, int len, ArrayDelegate del)
1957 return del (len, NULL, arr);
1961 mono_test_marshal_out_array_delegate (int *arr, int len, ArrayDelegate del)
1963 del (len, NULL, arr);
1965 if ((arr [0] != 1) || (arr [1] != 2))
1971 typedef gunichar2* (STDCALL *UnicodeStringDelegate) (gunichar2 *message);
1974 mono_test_marshal_return_unicode_string_delegate (UnicodeStringDelegate del)
1976 const char m[] = "abcdef";
1977 gunichar2 *s2, *res;
1980 s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
1990 mono_test_marshal_out_string_array_delegate (char **arr, int len, ArrayDelegate del)
1992 del (len, NULL, arr);
1994 if (!strcmp (arr [0], "ABC") && !strcmp (arr [1], "DEF"))
2000 typedef int (*CdeclDelegate) (int i, int j);
2003 mono_test_marshal_cdecl_delegate (CdeclDelegate del)
2007 for (i = 0; i < 1000; ++i)
2013 typedef char** (*ReturnStringArrayDelegate) (int i);
2016 mono_test_marshal_return_string_array_delegate (ReturnStringArrayDelegate d)
2024 if (strcmp (arr [0], "ABC") || strcmp (arr [1], "DEF"))
2035 add_delegate (int i, int j)
2041 mono_test_marshal_return_fnptr (void)
2043 return &add_delegate;
2049 printf ("codigo %x\n", code);
2058 mono_xr_as_handle (int code)
2073 mono_safe_handle_struct_ref (HandleStructs *x)
2075 printf ("Dingus Ref! \n");
2076 printf ("Values: %d %d %d %d\n", x->a, x->b, x->handle1, x->handle2);
2082 if (x->handle1 != (void*) 0x7080feed)
2085 if (x->handle2 != (void*) 0x1234abcd)
2092 mono_safe_handle_struct (HandleStructs x)
2094 printf ("Dingus Standard! \n");
2095 printf ("Values: %d %d %d %d\n", x.a, x.b, x.handle1, x.handle2);
2101 if (x.handle1 != (void*) 0x7080feed)
2104 if (x.handle2 != (void*) 0x1234abcd)
2115 mono_safe_handle_struct_simple (TrivialHandle x)
2117 printf ("The value is %d\n", x.a);
2118 return ((int)x.a) * 2;
2122 mono_safe_handle_return ()
2128 mono_safe_handle_ref (void **handle)
2131 *handle = (void *) 0xbad;
2135 *handle = (void *) 0x800d;
2144 mono_test_marshal_bstr_in(BSTR bstr)
2146 if (!wcscmp(bstr, L"mono_test_marshal_bstr_in"))
2152 mono_test_marshal_bstr_out(BSTR* bstr)
2154 *bstr = SysAllocString(L"mono_test_marshal_bstr_out");
2159 mono_test_marshal_bstr_in_null(BSTR bstr)
2167 mono_test_marshal_bstr_out_null(BSTR* bstr)
2174 mono_test_marshal_variant_in_sbyte(VARIANT variant)
2176 if (variant.vt == VT_I1 && variant.cVal == 100)
2182 mono_test_marshal_variant_in_byte(VARIANT variant)
2184 if (variant.vt == VT_UI1 && variant.bVal == 100)
2190 mono_test_marshal_variant_in_short(VARIANT variant)
2192 if (variant.vt == VT_I2 && variant.iVal == 314)
2198 mono_test_marshal_variant_in_ushort(VARIANT variant)
2200 if (variant.vt == VT_UI2 && variant.uiVal == 314)
2206 mono_test_marshal_variant_in_int(VARIANT variant)
2208 if (variant.vt == VT_I4 && variant.lVal == 314)
2214 mono_test_marshal_variant_in_uint(VARIANT variant)
2216 if (variant.vt == VT_UI4 && variant.ulVal == 314)
2222 mono_test_marshal_variant_in_long(VARIANT variant)
2224 if (variant.vt == VT_I8 && variant.llVal == 314)
2230 mono_test_marshal_variant_in_ulong(VARIANT variant)
2232 if (variant.vt == VT_UI8 && variant.ullVal == 314)
2238 mono_test_marshal_variant_in_float(VARIANT variant)
2240 if (variant.vt == VT_R4 && (variant.fltVal - 3.14)/3.14 < .001)
2246 mono_test_marshal_variant_in_double(VARIANT variant)
2248 if (variant.vt == VT_R8 && (variant.dblVal - 3.14)/3.14 < .001)
2254 mono_test_marshal_variant_in_bstr(VARIANT variant)
2256 if (variant.vt == VT_BSTR && !wcscmp(variant.bstrVal, L"PI"))
2262 mono_test_marshal_variant_in_bool_true (VARIANT variant)
2264 if (variant.vt == VT_BOOL && variant.boolVal == VARIANT_TRUE)
2270 mono_test_marshal_variant_in_bool_false (VARIANT variant)
2272 if (variant.vt == VT_BOOL && variant.boolVal == VARIANT_FALSE)
2278 mono_test_marshal_variant_out_sbyte(VARIANT* variant)
2280 variant->vt = VT_I1;
2281 variant->cVal = 100;
2287 mono_test_marshal_variant_out_byte(VARIANT* variant)
2289 variant->vt = VT_UI1;
2290 variant->bVal = 100;
2296 mono_test_marshal_variant_out_short(VARIANT* variant)
2298 variant->vt = VT_I2;
2299 variant->iVal = 314;
2305 mono_test_marshal_variant_out_ushort(VARIANT* variant)
2307 variant->vt = VT_UI2;
2308 variant->uiVal = 314;
2314 mono_test_marshal_variant_out_int(VARIANT* variant)
2316 variant->vt = VT_I4;
2317 variant->lVal = 314;
2323 mono_test_marshal_variant_out_uint(VARIANT* variant)
2325 variant->vt = VT_UI4;
2326 variant->ulVal = 314;
2332 mono_test_marshal_variant_out_long(VARIANT* variant)
2334 variant->vt = VT_I8;
2335 variant->llVal = 314;
2341 mono_test_marshal_variant_out_ulong(VARIANT* variant)
2343 variant->vt = VT_UI8;
2344 variant->ullVal = 314;
2350 mono_test_marshal_variant_out_float(VARIANT* variant)
2352 variant->vt = VT_R4;
2353 variant->fltVal = 3.14;
2359 mono_test_marshal_variant_out_double(VARIANT* variant)
2361 variant->vt = VT_R8;
2362 variant->dblVal = 3.14;
2368 mono_test_marshal_variant_out_bstr(VARIANT* variant)
2370 variant->vt = VT_BSTR;
2371 variant->bstrVal = SysAllocString(L"PI");
2377 mono_test_marshal_variant_out_bool_true (VARIANT* variant)
2379 variant->vt = VT_BOOL;
2380 variant->boolVal = VARIANT_TRUE;
2386 mono_test_marshal_variant_out_bool_false (VARIANT* variant)
2388 variant->vt = VT_BOOL;
2389 variant->boolVal = VARIANT_FALSE;
2394 typedef int (STDCALL *VarFunc) (int vt, VARIANT variant);
2395 typedef int (STDCALL *VarRefFunc) (int vt, VARIANT* variant);
2398 mono_test_marshal_variant_in_sbyte_unmanaged(VarFunc func)
2403 return func (VT_I1, vt);
2407 mono_test_marshal_variant_in_byte_unmanaged(VarFunc func)
2412 return func (VT_UI1, vt);
2416 mono_test_marshal_variant_in_short_unmanaged(VarFunc func)
2421 return func (VT_I2, vt);
2425 mono_test_marshal_variant_in_ushort_unmanaged(VarFunc func)
2430 return func (VT_UI2, vt);
2434 mono_test_marshal_variant_in_int_unmanaged(VarFunc func)
2439 return func (VT_I4, vt);
2443 mono_test_marshal_variant_in_uint_unmanaged(VarFunc func)
2448 return func (VT_UI4, vt);
2452 mono_test_marshal_variant_in_long_unmanaged(VarFunc func)
2457 return func (VT_I8, vt);
2461 mono_test_marshal_variant_in_ulong_unmanaged(VarFunc func)
2466 return func (VT_UI8, vt);
2470 mono_test_marshal_variant_in_float_unmanaged(VarFunc func)
2475 return func (VT_R4, vt);
2479 mono_test_marshal_variant_in_double_unmanaged(VarFunc func)
2484 return func (VT_R8, vt);
2488 mono_test_marshal_variant_in_bstr_unmanaged(VarFunc func)
2492 vt.bstrVal = SysAllocString(L"PI");
2493 return func (VT_BSTR, vt);
2497 mono_test_marshal_variant_in_bool_true_unmanaged(VarFunc func)
2501 vt.boolVal = VARIANT_TRUE;
2502 return func (VT_BOOL, vt);
2506 mono_test_marshal_variant_in_bool_false_unmanaged(VarFunc func)
2510 vt.boolVal = VARIANT_FALSE;
2511 return func (VT_BOOL, vt);
2515 mono_test_marshal_variant_out_sbyte_unmanaged(VarRefFunc func)
2520 if (vt.vt == VT_I1 && vt.cVal == -100)
2526 mono_test_marshal_variant_out_byte_unmanaged(VarRefFunc func)
2531 if (vt.vt == VT_UI1 && vt.bVal == 100)
2537 mono_test_marshal_variant_out_short_unmanaged(VarRefFunc func)
2542 if (vt.vt == VT_I2 && vt.iVal == -100)
2548 mono_test_marshal_variant_out_ushort_unmanaged(VarRefFunc func)
2553 if (vt.vt == VT_UI2 && vt.uiVal == 100)
2559 mono_test_marshal_variant_out_int_unmanaged(VarRefFunc func)
2564 if (vt.vt == VT_I4 && vt.lVal == -100)
2570 mono_test_marshal_variant_out_uint_unmanaged(VarRefFunc func)
2575 if (vt.vt == VT_UI4 && vt.ulVal == 100)
2581 mono_test_marshal_variant_out_long_unmanaged(VarRefFunc func)
2586 if (vt.vt == VT_I8 && vt.llVal == -100)
2592 mono_test_marshal_variant_out_ulong_unmanaged(VarRefFunc func)
2597 if (vt.vt == VT_UI8 && vt.ullVal == 100)
2603 mono_test_marshal_variant_out_float_unmanaged(VarRefFunc func)
2608 if (vt.vt == VT_R4 && fabs (vt.fltVal - 3.14f) < 1e-10)
2614 mono_test_marshal_variant_out_double_unmanaged(VarRefFunc func)
2619 if (vt.vt == VT_R8 && fabs (vt.dblVal - 3.14) < 1e-10)
2625 mono_test_marshal_variant_out_bstr_unmanaged(VarRefFunc func)
2629 func (VT_BSTR, &vt);
2630 if (vt.vt == VT_BSTR && !wcscmp(vt.bstrVal, L"PI"))
2636 mono_test_marshal_variant_out_bool_true_unmanaged(VarRefFunc func)
2640 func (VT_BOOL, &vt);
2641 if (vt.vt == VT_BOOL && vt.boolVal == VARIANT_TRUE)
2647 mono_test_marshal_variant_out_bool_false_unmanaged(VarRefFunc func)
2651 func (VT_BOOL, &vt);
2652 if (vt.vt == VT_BOOL && vt.boolVal == VARIANT_TRUE)
2657 typedef struct MonoComObject MonoComObject;
2661 int (STDCALL *QueryInterface)(MonoComObject* pUnk, gpointer riid, gpointer* ppv);
2662 int (STDCALL *AddRef)(MonoComObject* pUnk);
2663 int (STDCALL *Release)(MonoComObject* pUnk);
2664 int (STDCALL *get_ITest)(MonoComObject* pUnk, MonoComObject* *ppUnk);
2665 int (STDCALL *SByteIn)(MonoComObject* pUnk, char a);
2666 int (STDCALL *ByteIn)(MonoComObject* pUnk, unsigned char a);
2667 int (STDCALL *ShortIn)(MonoComObject* pUnk, short a);
2668 int (STDCALL *UShortIn)(MonoComObject* pUnk, unsigned short a);
2669 int (STDCALL *IntIn)(MonoComObject* pUnk, int a);
2670 int (STDCALL *UIntIn)(MonoComObject* pUnk, unsigned int a);
2671 int (STDCALL *LongIn)(MonoComObject* pUnk, LONGLONG a);
2672 int (STDCALL *ULongIn)(MonoComObject* pUnk, ULONGLONG a);
2673 int (STDCALL *FloatIn)(MonoComObject* pUnk, float a);
2674 int (STDCALL *DoubleIn)(MonoComObject* pUnk, double a);
2675 int (STDCALL *ITestIn)(MonoComObject* pUnk, MonoComObject* pUnk2);
2676 int (STDCALL *ITestOut)(MonoComObject* pUnk, MonoComObject* *ppUnk);
2679 struct MonoComObject
2685 DEFINE_GUID(IID_ITest, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
2686 DEFINE_GUID(IID_IMonoUnknown, 0, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0, 0x46);
2687 DEFINE_GUID(IID_IMonoDispatch, 0x00020400, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0, 0x46);
2689 int STDCALL MonoQueryInterface(MonoComObject* pUnk, gpointer riid, gpointer* ppv)
2692 if (!memcmp(riid, &IID_IMonoUnknown, sizeof(GUID))) {
2696 else if (!memcmp(riid, &IID_ITest, sizeof(GUID))) {
2700 else if (!memcmp(riid, &IID_IMonoDispatch, sizeof(GUID))) {
2704 return E_NOINTERFACE;
2707 int STDCALL MonoAddRef(MonoComObject* pUnk)
2709 return ++(pUnk->m_ref);
2712 int STDCALL MonoRelease(MonoComObject* pUnk)
2714 return --(pUnk->m_ref);
2717 int STDCALL SByteIn(MonoComObject* pUnk, char a)
2722 int STDCALL ByteIn(MonoComObject* pUnk, unsigned char a)
2727 int STDCALL ShortIn(MonoComObject* pUnk, short a)
2732 int STDCALL UShortIn(MonoComObject* pUnk, unsigned short a)
2737 int STDCALL IntIn(MonoComObject* pUnk, int a)
2742 int STDCALL UIntIn(MonoComObject* pUnk, unsigned int a)
2747 int STDCALL LongIn(MonoComObject* pUnk, LONGLONG a)
2752 int STDCALL ULongIn(MonoComObject* pUnk, ULONGLONG a)
2757 int STDCALL FloatIn(MonoComObject* pUnk, float a)
2762 int STDCALL DoubleIn(MonoComObject* pUnk, double a)
2767 int STDCALL ITestIn(MonoComObject* pUnk, MonoComObject *pUnk2)
2772 int STDCALL ITestOut(MonoComObject* pUnk, MonoComObject* *ppUnk)
2777 int STDCALL get_ITest(MonoComObject* pUnk, MonoComObject* *ppUnk)
2782 static void create_com_object (MonoComObject** pOut)
2784 *pOut = g_new0 (MonoComObject, 1);
2785 (*pOut)->vtbl = g_new0 (MonoIUnknown, 1);
2788 (*pOut)->vtbl->QueryInterface = MonoQueryInterface;
2789 (*pOut)->vtbl->AddRef = MonoAddRef;
2790 (*pOut)->vtbl->Release = MonoRelease;
2791 (*pOut)->vtbl->SByteIn = SByteIn;
2792 (*pOut)->vtbl->ByteIn = ByteIn;
2793 (*pOut)->vtbl->ShortIn = ShortIn;
2794 (*pOut)->vtbl->UShortIn = UShortIn;
2795 (*pOut)->vtbl->IntIn = IntIn;
2796 (*pOut)->vtbl->UIntIn = UIntIn;
2797 (*pOut)->vtbl->LongIn = LongIn;
2798 (*pOut)->vtbl->ULongIn = ULongIn;
2799 (*pOut)->vtbl->FloatIn = FloatIn;
2800 (*pOut)->vtbl->DoubleIn = DoubleIn;
2801 (*pOut)->vtbl->ITestIn = ITestIn;
2802 (*pOut)->vtbl->ITestOut = ITestOut;
2803 (*pOut)->vtbl->get_ITest = get_ITest;
2806 static MonoComObject* same_object = NULL;
2809 mono_test_marshal_com_object_create(MonoComObject* *pUnk)
2811 create_com_object (pUnk);
2814 same_object = *pUnk;
2820 mono_test_marshal_com_object_same(MonoComObject* *pUnk)
2822 *pUnk = same_object;
2828 mono_test_marshal_com_object_destroy(MonoComObject *pUnk)
2830 int ref = --(pUnk->m_ref);
2838 mono_test_marshal_com_object_ref_count(MonoComObject *pUnk)
2844 mono_test_marshal_ccw_itest (MonoComObject *pUnk)
2847 MonoComObject* pTest;
2852 hr = pUnk->vtbl->SByteIn (pUnk, -100);
2855 hr = pUnk->vtbl->ByteIn (pUnk, 100);
2858 hr = pUnk->vtbl->ShortIn (pUnk, -100);
2861 hr = pUnk->vtbl->UShortIn (pUnk, 100);
2864 hr = pUnk->vtbl->IntIn (pUnk, -100);
2867 hr = pUnk->vtbl->UIntIn (pUnk, 100);
2870 hr = pUnk->vtbl->LongIn (pUnk, -100);
2873 hr = pUnk->vtbl->ULongIn (pUnk, 100);
2876 hr = pUnk->vtbl->FloatIn (pUnk, 3.14f);
2879 hr = pUnk->vtbl->DoubleIn (pUnk, 3.14);
2882 hr = pUnk->vtbl->ITestIn (pUnk, pUnk);
2885 hr = pUnk->vtbl->ITestOut (pUnk, &pTest);