9 #define STDCALL __stdcall
15 extern __declspec(dllimport) __stdcall void CoTaskMemFree(void *ptr);
18 typedef int (STDCALL *SimpleDelegate) (int a);
20 static void marshal_free (void *ptr)
30 test_lpwstr_marshal (unsigned short* chars, long length)
35 res = malloc (2 * (length + 1));
37 // printf("test_lpwstr_marshal()\n");
39 while ( i < length ) {
40 // printf("X|%u|\n", chars[i]);
56 int mono_union_test_1 (union_test_1_type u1) {
57 // printf ("Got values %d %d %d\n", u1.b, u1.a, u1.c);
58 return u1.a + u1.b + u1.c;
61 int mono_return_int (int a) {
62 // printf ("Got value %d\n", a);
71 int mono_return_int_ss (struct ss a) {
72 // printf ("Got value %d\n", a.i);
76 struct ss mono_return_ss (struct ss a) {
77 // printf ("Got value %d\n", a.i);
87 struct sc1 mono_return_sc1 (struct sc1 a) {
88 // printf ("Got value %d\n", a.c[0]);
99 struct sc3 mono_return_sc3 (struct sc3 a) {
100 // printf ("Got values %d %d %d\n", a.c[0], a.c[1], a.c[2]);
112 struct sc5 mono_return_sc5 (struct sc5 a) {
113 // printf ("Got values %d %d %d %d %d\n", a.c[0], a.c[1], a.c[2], a.c[3], a.c[4]);
128 int mono_return_int_su (union su a) {
129 // printf ("Got value %d\n", a.i1);
133 int mono_test_many_int_arguments (int a, int b, int c, int d, int e,
134 int f, int g, int h, int i, int j);
135 short mono_test_many_short_arguments (short a, short b, short c, short d, short e,
136 short f, short g, short h, short i, short j);
137 char mono_test_many_char_arguments (char a, char b, char c, char d, char e,
138 char f, char g, char h, char i, char j);
141 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)
143 return a + b + c + d + e + f + g + h + i + j;
147 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)
149 return a + b + c + d + e + f + g + h + i + j;
153 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)
155 return a + b + c + d + e + f + g + h + i + j;
159 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)
161 return a + b + c + d + e + f + g + h + i + j;
165 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)
167 return a + b + c + d + e + f + g + h + i + j;
171 mono_test_split_double_arguments (double a, double b, float c, double d, double e)
173 return a + b + c + d + e;
177 mono_test_puts_static (char *s)
179 // printf ("TEST %s\n", s);
183 typedef int (STDCALL *SimpleDelegate3) (int a, int b);
186 mono_invoke_delegate (SimpleDelegate3 delegate)
190 // printf ("start invoke %p\n", delegate);
192 res = delegate (2, 3);
194 // printf ("end invoke\n");
200 mono_test_marshal_char (short a1)
209 mono_test_marshal_char_array (gunichar2 *s)
211 const char m[] = "abcdef";
215 s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
224 mono_test_empty_pinvoke (int i)
230 mono_test_marshal_bool_byref (int a, int *b, int c)
240 mono_test_marshal_array (int *a1)
244 for (i = 0; i < 50; i++)
251 mono_test_marshal_inout_array (int *a1)
255 for (i = 0; i < 50; i++) {
257 a1 [i] = 50 - a1 [i];
264 mono_test_marshal_inout_nonblittable_array (gunichar2 *a1)
268 for (i = 0; i < 10; i++) {
283 mono_test_return_vtype (int i)
296 mono_test_delegate_struct (void)
298 // printf ("TEST\n");
301 typedef char* (STDCALL *ReturnStringDelegate) (const char *s);
304 mono_test_return_string (ReturnStringDelegate func)
308 // printf ("mono_test_return_string\n");
313 // printf ("got string: %s\n", res);
314 return g_strdup ("12345");
317 typedef int (STDCALL *RefVTypeDelegate) (int a, simplestruct *ss, int b);
320 mono_test_ref_vtype (int a, simplestruct *ss, int b, RefVTypeDelegate func)
322 if (a == 1 && b == 2 && ss->a == 0 && ss->b == 1 && ss->c == 0 &&
323 !strcmp (ss->d, "TEST1")) {
329 return func (a, ss, b);
335 typedef int (STDCALL *OutVTypeDelegate) (int a, simplestruct *ss, int b);
338 mono_test_marshal_out_struct (int a, simplestruct *ss, int b, OutVTypeDelegate func)
342 /* Check that the input pointer is ignored */
343 ss->d = (gpointer)0x12345678;
347 if (ss->a && ss->b && ss->c && !strcmp (ss->d, "TEST3"))
355 SimpleDelegate func, func2;
359 mono_test_marshal_delegate_struct (DelegateStruct ds)
361 return ds.func (ds.a) + ds.func2 (ds.a);
365 mono_test_marshal_struct (simplestruct ss)
367 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
368 !strcmp (ss.d, "TEST"))
386 mono_test_marshal_struct2 (simplestruct2 ss)
388 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
389 !strcmp (ss.d, "TEST") &&
390 ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
396 /* on HP some of the struct should be on the stack and not in registers */
398 mono_test_marshal_struct2_2 (int i, int j, int k, simplestruct2 ss)
400 if (i != 10 || j != 11 || k != 12)
402 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
403 !strcmp (ss.d, "TEST") &&
404 ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
411 mono_test_marshal_struct_array (simplestruct2 *ss)
413 if (! (ss[0].a == 0 && ss[0].b == 1 && ss[0].c == 0 &&
414 !strcmp (ss[0].d, "TEST") &&
415 ss[0].e == 99 && ss[0].f == 1.5 && ss[0].g == 42 && ss[0].h == (guint64)123))
418 if (! (ss[1].a == 0 && ss[1].b == 0 && ss[1].c == 0 &&
419 !strcmp (ss[1].d, "TEST2") &&
420 ss[1].e == 100 && ss[1].f == 2.5 && ss[1].g == 43 && ss[1].h == (guint64)124))
426 typedef struct long_align_struct {
433 mono_test_marshal_long_align_struct_array (long_align_struct *ss)
435 return ss[0].a + ss[0].b + ss[0].c + ss[1].a + ss[1].b + ss[1].c;
439 mono_test_marshal_class (int i, int j, int k, simplestruct2 *ss, int l)
446 if (i != 10 || j != 11 || k != 12 || l != 14)
448 if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
449 !strcmp (ss->d, "TEST") &&
450 ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
453 res = g_new0 (simplestruct2, 1);
454 memcpy (res, ss, sizeof (simplestruct2));
455 res->d = g_strdup ("TEST");
460 mono_test_marshal_byref_class (simplestruct2 **ssp)
462 simplestruct2 *ss = *ssp;
465 if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
466 !strcmp (ss->d, "TEST") &&
467 ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
470 res = g_new0 (simplestruct2, 1);
471 memcpy (res, ss, sizeof (simplestruct2));
472 res->d = g_strdup ("TEST-RES");
489 mono_test_marshal_delegate (SimpleDelegate delegate)
493 /* Check that the delegate wrapper is stdcall */
498 g_assert (sp1 == sp2);
504 mono_test_marshal_return_delegate (SimpleDelegate delegate)
509 typedef simplestruct (STDCALL *SimpleDelegate2) (simplestruct ss);
512 mono_test_marshal_delegate2 (SimpleDelegate2 delegate)
514 simplestruct ss, res;
522 if (! (res.a && !res.b && res.c && !strcmp (res.d, "TEST-RES")))
528 typedef simplestruct* (STDCALL *SimpleDelegate4) (simplestruct *ss);
531 mono_test_marshal_delegate4 (SimpleDelegate4 delegate)
542 res = delegate (&ss);
546 /* Check return value */
547 if (! (!res->a && res->b && !res->c && !strcmp (res->d, "TEST")))
550 /* Check NULL argument and NULL result */
551 res = delegate (NULL);
558 typedef int (STDCALL *SimpleDelegate5) (simplestruct **ss);
561 mono_test_marshal_delegate5 (SimpleDelegate5 delegate)
574 res = delegate (&ptr);
578 if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
585 mono_test_marshal_delegate6 (SimpleDelegate5 delegate)
589 res = delegate (NULL);
594 typedef int (STDCALL *SimpleDelegate7) (simplestruct **ss);
597 mono_test_marshal_delegate7 (SimpleDelegate7 delegate)
602 /* Check that the input pointer is ignored */
603 ptr = (gpointer)0x12345678;
605 res = delegate (&ptr);
609 if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
615 typedef int (STDCALL *SimpleDelegate8) (gunichar2 *s);
618 mono_test_marshal_delegate8 (SimpleDelegate8 delegate, gunichar2 *s)
623 typedef int (STDCALL *return_int_fnt) (int i);
624 typedef int (STDCALL *SimpleDelegate9) (return_int_fnt *d);
627 mono_test_marshal_delegate9 (SimpleDelegate9 delegate, gpointer ftn)
629 return delegate (ftn);
639 mono_test_marshal_delegate10 (SimpleDelegate9 delegate)
641 return delegate (return_self);
644 typedef int (STDCALL *PrimitiveByrefDelegate) (int *i);
647 mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate delegate)
651 int res = delegate (&i);
661 typedef int (STDCALL *return_int_delegate) (int i);
663 typedef return_int_delegate (STDCALL *ReturnDelegateDelegate) ();
666 mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d)
673 mono_test_marshal_stringbuilder (char *s, int n)
675 const char m[] = "This is my message. Isn't it nice?";
677 if (strcmp (s, "ABCD") != 0)
684 mono_test_marshal_stringbuilder_unicode (gunichar2 *s, int n)
686 const char m[] = "This is my message. Isn't it nice?";
690 s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
709 mono_test_marshal_empty_string_array (char **array)
711 return (array == NULL) ? 0 : 1;
715 mono_test_marshal_string_array (char **array)
717 if (strcmp (array [0], "ABC"))
719 if (strcmp (array [1], "DEF"))
722 if (array [2] != NULL)
729 mono_test_marshal_byref_string_array (char ***array)
734 if (strcmp ((*array) [0], "Alpha"))
736 if (strcmp ((*array) [1], "Beta"))
738 if (strcmp ((*array) [2], "Gamma"))
745 mono_test_marshal_stringbuilder_array (char **array)
747 if (strcmp (array [0], "ABC"))
749 if (strcmp (array [1], "DEF"))
752 strcpy (array [0], "DEF");
753 strcpy (array [1], "ABC");
759 mono_test_marshal_unicode_string_array (gunichar2 **array, char **array2)
761 GError *error = NULL;
764 s = g_utf16_to_utf8 (array [0], -1, NULL, NULL, &error);
765 if (strcmp (s, "ABC")) {
772 s = g_utf16_to_utf8 (array [1], -1, NULL, NULL, &error);
773 if (strcmp (s, "DEF")) {
780 if (strcmp (array2 [0], "ABC"))
783 if (strcmp (array2 [1], "DEF"))
789 /* this does not work on Redhat gcc 2.96 */
791 mono_test_empty_struct (int a, EmptyStruct es, int b)
793 // printf ("mono_test_empty_struct %d %d\n", a, b);
795 if (a == 1 && b == 2)
805 mono_test_byvalstr_gen (void)
809 ret = malloc(sizeof(ByValStrStruct));
810 memset(ret, 'a', sizeof(ByValStrStruct)-1);
811 ret->a[sizeof(ByValStrStruct)-1] = 0;
817 mono_test_byvalstr_check (ByValStrStruct* data, char* correctString)
821 ret = strcmp(data->a, correctString);
822 // printf ("T1: %s\n", data->a);
823 // printf ("T2: %s\n", correctString);
835 printf ("HEXDUMP DEFAULT VERSION\n");
838 for (i=0; i < 8; ++i)
841 printf("%0x ", (int) *(p++));
854 printf ("HEXDUMP ANSI VERSION\n");
857 for (i=0; i < 8; ++i)
860 printf("%0x ", (int) *(p++));
868 HexDump1W(char *data)
873 printf ("HEXDUMP UNICODE VERSION\n");
876 for (i=0; i < 8; ++i)
879 printf("%0x ", (int) *(p++));
883 return res + 1000000;
886 typedef int (STDCALL *intcharFunc)(const char*);
889 callFunction (intcharFunc f)
900 class_marshal_test0 (SimpleObj *obj1)
902 // printf ("class_marshal_test0 %s %d\n", obj1->str, obj1->i);
904 if (strcmp(obj1->str, "T1"))
913 class_marshal_test4 (SimpleObj *obj1)
922 class_marshal_test1 (SimpleObj **obj1)
924 SimpleObj *res = malloc (sizeof (SimpleObj));
926 res->str = g_strdup ("ABC");
933 class_marshal_test2 (SimpleObj **obj1)
935 // printf ("class_marshal_test2 %s %d\n", (*obj1)->str, (*obj1)->i);
937 if (strcmp((*obj1)->str, "ABC"))
946 string_marshal_test0 (char *str)
948 if (strcmp (str, "TEST0"))
955 string_marshal_test1 (const char **str)
961 string_marshal_test2 (char **str)
963 // printf ("string_marshal_test2 %s\n", *str);
965 if (strcmp (*str, "TEST1"))
972 string_marshal_test3 (char *str)
986 VectorList* TestVectorList (VectorList *vl)
990 // printf ("TestVectorList %d %d\n", vl->a, vl->b);
995 res = g_new0 (VectorList, 1);
996 memcpy (res, vl, sizeof (VectorList));
1002 typedef struct _OSVERSIONINFO
1009 GetVersionEx (OSVERSIONINFO *osvi)
1012 // printf ("GOT %d %d\n", osvi->a, osvi->b);
1017 return osvi->a + osvi->b;
1021 BugGetVersionEx (int a, int b, int c, int d, int e, int f, int g, int h, OSVERSIONINFO *osvi)
1024 // printf ("GOT %d %d\n", osvi->a, osvi->b);
1029 return osvi->a + osvi->b;
1038 mono_test_marshal_point (point pt)
1040 // printf("point %g %g\n", pt.x, pt.y);
1041 if (pt.x == 1.25 && pt.y == 3.5)
1053 mono_test_marshal_mixed_point (mixed_point pt)
1055 // printf("mixed point %d %g\n", pt.x, pt.y);
1056 if (pt.x == 5 && pt.y == 6.75)
1063 mono_test_marshal_mixed_point_2 (mixed_point *pt)
1065 if (pt->x != 5 || pt->y != 6.75)
1075 marshal_test_ref_bool(int i, char *b1, short *b2, int *b3)
1078 if (*b1 != 0 && *b1 != 1)
1080 if (*b2 != 0 && *b2 != -1) /* variant_bool */
1082 if (*b3 != 0 && *b3 != 1)
1084 if (i == ((*b1 << 2) | (-*b2 << 1) | *b3))
1096 short b2; /* variant_bool */
1101 marshal_test_bool_struct(struct BoolStruct *s)
1104 if (s->b1 != 0 && s->b1 != 1)
1106 if (s->b2 != 0 && s->b2 != -1)
1108 if (s->b3 != 0 && s->b3 != 1)
1110 if (s->i == ((s->b1 << 2) | (-s->b2 << 1) | s->b3))
1119 extern __declspec(dllimport) __stdcall void SetLastError(int x);
1123 mono_test_last_error (int err)
1133 mono_test_asany (void *ptr, int what)
1137 return (*(int*)ptr == 5) ? 0 : 1;
1139 return strcmp (ptr, "ABC") == 0 ? 0 : 1;
1141 simplestruct2 ss = *(simplestruct2*)ptr;
1143 if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
1144 !strcmp (ss.d, "TEST") &&
1145 ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
1151 GError *error = NULL;
1154 s = g_utf16_to_utf8 (ptr, -1, NULL, NULL, &error);
1155 if (!strcmp (s, "ABC")) {
1165 g_assert_not_reached ();
1172 * AMD64 marshalling tests.
1175 typedef struct amd64_struct1 {
1183 mono_test_marshal_amd64_pass_return_struct1 (amd64_struct1 s)
1193 typedef struct amd64_struct2 {
1199 mono_test_marshal_amd64_pass_return_struct2 (amd64_struct2 s)
1207 typedef struct amd64_struct3 {
1212 mono_test_marshal_amd64_pass_return_struct3 (amd64_struct3 s)
1219 typedef struct amd64_struct4 {
1224 mono_test_marshal_amd64_pass_return_struct4 (amd64_struct4 s)
1232 static guint32 custom_res [2];
1235 mono_test_marshal_pass_return_custom (int i, guint32 *ptr, int j)
1237 /* ptr will be freed by CleanupNative, so make a copy */
1238 custom_res [0] = 0; /* not allocated by AllocHGlobal */
1239 custom_res [1] = ptr [1];
1244 typedef void *(STDCALL *PassReturnPtrDelegate) (void *ptr);
1247 mono_test_marshal_pass_return_custom_in_delegate (PassReturnPtrDelegate del)
1261 /* FIXME: Freed with FreeHGlobal */
1269 typedef int (STDCALL *ReturnEnumDelegate) (int e);
1272 mono_test_marshal_return_enum_delegate (ReturnEnumDelegate func)
1282 typedef BlittableStruct (STDCALL *SimpleDelegate10) (BlittableStruct ss);
1285 mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 delegate)
1287 BlittableStruct ss, res;
1294 res = delegate (ss);
1295 if (! ((res.a == -1) && (res.b == -2) && (res.c == -3) && (res.d == -55)))
1302 mono_test_stdcall_name_mangling (int a, int b, int c)