/
libtest.c
4323 lines (3524 loc) · 77.2 KB
/
libtest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gmodule.h>
#include <errno.h>
#include <time.h>
#include <math.h>
#ifdef WIN32
#include <windows.h>
#include "initguid.h"
#endif
#ifdef WIN32
#define STDCALL __stdcall
#else
#define STDCALL
#endif
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
#endif
#ifdef WIN32
extern __declspec(dllimport) void __stdcall CoTaskMemFree(void *ptr);
#endif
typedef int (STDCALL *SimpleDelegate) (int a);
#if defined(WIN32) && defined (_MSC_VER)
#define LIBTEST_API __declspec(dllexport)
#else
#define LIBTEST_API
#endif
static void marshal_free (void *ptr)
{
#ifdef WIN32
CoTaskMemFree (ptr);
#else
g_free (ptr);
#endif
}
static void* marshal_alloc (gsize size)
{
#ifdef WIN32
return CoTaskMemAlloc (size);
#else
return g_malloc (size);
#endif
}
static gunichar2* marshal_bstr_alloc(const gchar* str)
{
#ifdef WIN32
gunichar2* ret = NULL;
gunichar2* temp = NULL;
temp = g_utf8_to_utf16 (str, -1, NULL, NULL, NULL);
ret = SysAllocString (temp);
g_free (temp);
return ret;
#else
gchar* ret = NULL;
int slen = strlen (str);
gunichar2* temp;
/* allocate len + 1 utf16 characters plus 4 byte integer for length*/
ret = g_malloc ((slen + 1) * sizeof(gunichar2) + sizeof(guint32));
if (ret == NULL)
return NULL;
temp = g_utf8_to_utf16 (str, -1, NULL, NULL, NULL);
memcpy (ret + sizeof(guint32), temp, slen * sizeof(gunichar2));
* ((guint32 *) ret) = slen * sizeof(gunichar2);
ret [4 + slen * sizeof(gunichar2)] = 0;
ret [5 + slen * sizeof(gunichar2)] = 0;
return (gunichar2*)(ret + 4);
#endif
}
LIBTEST_API int STDCALL
mono_cominterop_is_supported ()
{
#if defined(TARGET_X86) || defined(TARGET_AMD64)
return 1;
#endif
return 0;
}
LIBTEST_API unsigned short* STDCALL
test_lpwstr_marshal (unsigned short* chars, long length)
{
int i = 0;
unsigned short *res;
res = marshal_alloc (2 * (length + 1));
// printf("test_lpwstr_marshal()\n");
while ( i < length ) {
// printf("X|%u|\n", chars[i]);
res [i] = chars[i];
i++;
}
res [i] = 0;
return res;
}
LIBTEST_API void STDCALL
test_lpwstr_marshal_out (unsigned short** chars)
{
int i = 0;
const char abc[] = "ABC";
glong len = strlen(abc);
*chars = marshal_alloc (2 * (len + 1));
while ( i < len ) {
(*chars) [i] = abc[i];
i++;
}
(*chars) [i] = 0;
}
typedef struct {
int b;
int a;
int c;
} union_test_1_type;
LIBTEST_API int STDCALL
mono_union_test_1 (union_test_1_type u1) {
// printf ("Got values %d %d %d\n", u1.b, u1.a, u1.c);
return u1.a + u1.b + u1.c;
}
LIBTEST_API int STDCALL
mono_return_int (int a) {
// printf ("Got value %d\n", a);
return a;
}
LIBTEST_API float STDCALL
mono_test_marshal_pass_return_float (float f) {
return f + 1.0;
}
struct ss
{
int i;
};
LIBTEST_API int STDCALL
mono_return_int_ss (struct ss a) {
// printf ("Got value %d\n", a.i);
return a.i;
}
LIBTEST_API struct ss STDCALL
mono_return_ss (struct ss a) {
// printf ("Got value %d\n", a.i);
a.i++;
return a;
}
struct sc1
{
char c[1];
};
LIBTEST_API struct sc1 STDCALL
mono_return_sc1 (struct sc1 a) {
// printf ("Got value %d\n", a.c[0]);
a.c[0]++;
return a;
}
struct sc3
{
char c[3];
};
LIBTEST_API struct sc3 STDCALL
mono_return_sc3 (struct sc3 a) {
// printf ("Got values %d %d %d\n", a.c[0], a.c[1], a.c[2]);
a.c[0]++;
a.c[1] += 2;
a.c[2] += 3;
return a;
}
struct sc5
{
char c[5];
};
LIBTEST_API struct sc5 STDCALL
mono_return_sc5 (struct sc5 a) {
// printf ("Got values %d %d %d %d %d\n", a.c[0], a.c[1], a.c[2], a.c[3], a.c[4]);
a.c[0]++;
a.c[1] += 2;
a.c[2] += 3;
a.c[3] += 4;
a.c[4] += 5;
return a;
}
union su
{
int i1;
int i2;
};
LIBTEST_API int STDCALL
mono_return_int_su (union su a) {
// printf ("Got value %d\n", a.i1);
return a.i1;
}
LIBTEST_API int STDCALL
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);
LIBTEST_API short STDCALL
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);
LIBTEST_API char STDCALL
mono_test_many_char_arguments (char a, char b, char c, char d, char e,
char f, char g, char h, char i, char j);
LIBTEST_API int STDCALL
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)
{
return a + b + c + d + e + f + g + h + i + j;
}
LIBTEST_API short STDCALL
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)
{
return a + b + c + d + e + f + g + h + i + j;
}
LIBTEST_API char STDCALL
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)
{
return a + b + c + d + e + f + g + h + i + j;
}
LIBTEST_API float STDCALL
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)
{
return a + b + c + d + e + f + g + h + i + j;
}
LIBTEST_API double STDCALL
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)
{
return a + b + c + d + e + f + g + h + i + j;
}
LIBTEST_API double STDCALL
mono_test_split_double_arguments (double a, double b, float c, double d, double e)
{
return a + b + c + d + e;
}
LIBTEST_API int STDCALL
mono_test_puts_static (char *s)
{
// printf ("TEST %s\n", s);
return 1;
}
typedef int (STDCALL *SimpleDelegate3) (int a, int b);
LIBTEST_API int STDCALL
mono_invoke_delegate (SimpleDelegate3 delegate)
{
int res;
// printf ("start invoke %p\n", delegate);
res = delegate (2, 3);
// printf ("end invoke\n");
return res;
}
LIBTEST_API int STDCALL
mono_invoke_simple_delegate (SimpleDelegate d)
{
return d (4);
}
LIBTEST_API int STDCALL
mono_test_marshal_char (short a1)
{
if (a1 == 'a')
return 0;
return 1;
}
LIBTEST_API void STDCALL
mono_test_marshal_char_array (gunichar2 *s)
{
const char m[] = "abcdef";
gunichar2* s2;
glong len;
s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
len = (len * 2) + 2;
memcpy (s, s2, len);
g_free (s2);
}
LIBTEST_API int STDCALL
mono_test_empty_pinvoke (int i)
{
return i;
}
LIBTEST_API int STDCALL
mono_test_marshal_bool_byref (int a, int *b, int c)
{
int res = *b;
*b = 1;
return res;
}
LIBTEST_API int STDCALL
mono_test_marshal_bool_in_as_I1_U1 (char bTrue, char bFalse)
{
if (!bTrue)
return 1;
if (bFalse)
return 2;
return 0;
}
LIBTEST_API int STDCALL
mono_test_marshal_bool_out_as_I1_U1 (char* bTrue, char* bFalse)
{
if (!bTrue || !bFalse)
return 3;
*bTrue = 1;
*bFalse = 0;
return 0;
}
LIBTEST_API int STDCALL
mono_test_marshal_bool_ref_as_I1_U1 (char* bTrue, char* bFalse)
{
if (!bTrue || !bFalse)
return 4;
if (!(*bTrue))
return 5;
if (*bFalse)
return 6;
*bFalse = 1;
*bTrue = 0;
return 0;
}
LIBTEST_API int STDCALL
mono_test_marshal_array (int *a1)
{
int i, sum = 0;
for (i = 0; i < 50; i++)
sum += a1 [i];
return sum;
}
LIBTEST_API int STDCALL
mono_test_marshal_inout_array (int *a1)
{
int i, sum = 0;
for (i = 0; i < 50; i++) {
sum += a1 [i];
a1 [i] = 50 - a1 [i];
}
return sum;
}
LIBTEST_API int STDCALL
mono_test_marshal_out_array (int *a1)
{
int i;
for (i = 0; i < 50; i++) {
a1 [i] = i;
}
return 0;
}
LIBTEST_API int STDCALL
mono_test_marshal_inout_nonblittable_array (gunichar2 *a1)
{
int i, sum = 0;
for (i = 0; i < 10; i++) {
a1 [i] = 'F';
}
return sum;
}
typedef struct {
int a;
int b;
int c;
const char *d;
gunichar2 *d2;
} simplestruct;
typedef struct {
double x;
double y;
} point;
LIBTEST_API simplestruct STDCALL
mono_test_return_vtype (int i)
{
simplestruct res;
static gunichar2 test2 [] = { 'T', 'E', 'S', 'T', '2', 0 };
res.a = 0;
res.b = 1;
res.c = 0;
res.d = "TEST";
res.d2 = test2;
return res;
}
LIBTEST_API void STDCALL
mono_test_delegate_struct (void)
{
// printf ("TEST\n");
}
typedef char* (STDCALL *ReturnStringDelegate) (const char *s);
LIBTEST_API char * STDCALL
mono_test_return_string (ReturnStringDelegate func)
{
char *res;
// printf ("mono_test_return_string\n");
res = func ("TEST");
marshal_free (res);
// printf ("got string: %s\n", res);
return g_strdup ("12345");
}
typedef int (STDCALL *RefVTypeDelegate) (int a, simplestruct *ss, int b);
LIBTEST_API int STDCALL
mono_test_ref_vtype (int a, simplestruct *ss, int b, RefVTypeDelegate func)
{
if (a == 1 && b == 2 && ss->a == 0 && ss->b == 1 && ss->c == 0 &&
!strcmp (ss->d, "TEST1")) {
ss->a = 1;
ss->b = 0;
ss->c = 1;
ss->d = "TEST2";
return func (a, ss, b);
}
return 1;
}
typedef int (STDCALL *OutVTypeDelegate) (int a, simplestruct *ss, int b);
LIBTEST_API int STDCALL
mono_test_marshal_out_struct (int a, simplestruct *ss, int b, OutVTypeDelegate func)
{
/* Check that the input pointer is ignored */
ss->d = (gpointer)0x12345678;
func (a, ss, b);
if (ss->a && ss->b && ss->c && !strcmp (ss->d, "TEST3"))
return 0;
else
return 1;
}
typedef struct {
int a;
SimpleDelegate func, func2, func3;
} DelegateStruct;
LIBTEST_API DelegateStruct STDCALL
mono_test_marshal_delegate_struct (DelegateStruct ds)
{
DelegateStruct res;
res.a = ds.func (ds.a) + ds.func2 (ds.a) + (ds.func3 == NULL ? 0 : 1);
res.func = ds.func;
res.func2 = ds.func2;
res.func3 = NULL;
return res;
}
LIBTEST_API int STDCALL
mono_test_marshal_struct (simplestruct ss)
{
if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
!strcmp (ss.d, "TEST"))
return 0;
return 1;
}
LIBTEST_API int STDCALL
mono_test_marshal_byref_struct (simplestruct *ss, int a, int b, int c, char *d)
{
gboolean res = (ss->a == a && ss->b == b && ss->c == c && strcmp (ss->d, d) == 0);
marshal_free ((char*)ss->d);
ss->a = !ss->a;
ss->b = !ss->b;
ss->c = !ss->c;
ss->d = g_strdup ("DEF");
return res ? 0 : 1;
}
typedef struct {
int a;
int b;
int c;
char *d;
unsigned char e;
double f;
unsigned char g;
guint64 h;
} simplestruct2;
LIBTEST_API int STDCALL
mono_test_marshal_struct2 (simplestruct2 ss)
{
if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
!strcmp (ss.d, "TEST") &&
ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
return 0;
return 1;
}
/* on HP some of the struct should be on the stack and not in registers */
LIBTEST_API int STDCALL
mono_test_marshal_struct2_2 (int i, int j, int k, simplestruct2 ss)
{
if (i != 10 || j != 11 || k != 12)
return 1;
if (ss.a == 0 && ss.b == 1 && ss.c == 0 &&
!strcmp (ss.d, "TEST") &&
ss.e == 99 && ss.f == 1.5 && ss.g == 42 && ss.h == (guint64)123)
return 0;
return 1;
}
LIBTEST_API int STDCALL
mono_test_marshal_lpstruct (simplestruct *ss)
{
if (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
!strcmp (ss->d, "TEST"))
return 0;
return 1;
}
LIBTEST_API int STDCALL
mono_test_marshal_lpstruct_blittable (point *p)
{
if (p->x == 1.0 && p->y == 2.0)
return 0;
else
return 1;
}
LIBTEST_API int STDCALL
mono_test_marshal_struct_array (simplestruct2 *ss)
{
if (! (ss[0].a == 0 && ss[0].b == 1 && ss[0].c == 0 &&
!strcmp (ss[0].d, "TEST") &&
ss[0].e == 99 && ss[0].f == 1.5 && ss[0].g == 42 && ss[0].h == (guint64)123))
return 1;
if (! (ss[1].a == 0 && ss[1].b == 0 && ss[1].c == 0 &&
!strcmp (ss[1].d, "TEST2") &&
ss[1].e == 100 && ss[1].f == 2.5 && ss[1].g == 43 && ss[1].h == (guint64)124))
return 1;
return 0;
}
typedef struct long_align_struct {
gint32 a;
gint64 b;
gint64 c;
} long_align_struct;
LIBTEST_API int STDCALL
mono_test_marshal_long_align_struct_array (long_align_struct *ss)
{
return ss[0].a + ss[0].b + ss[0].c + ss[1].a + ss[1].b + ss[1].c;
}
LIBTEST_API simplestruct2 * STDCALL
mono_test_marshal_class (int i, int j, int k, simplestruct2 *ss, int l)
{
simplestruct2 *res;
if (!ss)
return NULL;
if (i != 10 || j != 11 || k != 12 || l != 14)
return NULL;
if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
!strcmp (ss->d, "TEST") &&
ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
return NULL;
res = g_new0 (simplestruct2, 1);
memcpy (res, ss, sizeof (simplestruct2));
res->d = g_strdup ("TEST");
return res;
}
LIBTEST_API int STDCALL
mono_test_marshal_byref_class (simplestruct2 **ssp)
{
simplestruct2 *ss = *ssp;
simplestruct2 *res;
if (! (ss->a == 0 && ss->b == 1 && ss->c == 0 &&
!strcmp (ss->d, "TEST") &&
ss->e == 99 && ss->f == 1.5 && ss->g == 42 && ss->h == (guint64)123))
return 1;
res = g_new0 (simplestruct2, 1);
memcpy (res, ss, sizeof (simplestruct2));
res->d = g_strdup ("TEST-RES");
*ssp = res;
return 0;
}
static void *
get_sp (void)
{
int i;
void *p;
/* Yes, this is correct, we are only trying to determine the value of the stack here */
p = &i;
return p;
}
LIBTEST_API int STDCALL
reliable_delegate (int a)
{
return a;
}
/*
* Checks whether get_sp() works as expected. It doesn't work with gcc-2.95.3 on linux.
*/
static gboolean
is_get_sp_reliable (void)
{
void *sp1, *sp2;
reliable_delegate(1);
sp1 = get_sp();
reliable_delegate(1);
sp2 = get_sp();
return sp1 == sp2;
}
LIBTEST_API int STDCALL
mono_test_marshal_delegate (SimpleDelegate delegate)
{
void *sp1, *sp2;
/* Check that the delegate wrapper is stdcall */
delegate (2);
sp1 = get_sp ();
delegate (2);
sp2 = get_sp ();
if (is_get_sp_reliable())
g_assert (sp1 == sp2);
return delegate (2);
}
static int STDCALL inc_cb (int i)
{
return i + 1;
}
LIBTEST_API int STDCALL
mono_test_marshal_out_delegate (SimpleDelegate *delegate)
{
*delegate = inc_cb;
return 0;
}
LIBTEST_API SimpleDelegate STDCALL
mono_test_marshal_return_delegate (SimpleDelegate delegate)
{
return delegate;
}
static int STDCALL
return_plus_one (int i)
{
return i + 1;
}
LIBTEST_API SimpleDelegate STDCALL
mono_test_marshal_return_delegate_2 (void)
{
return return_plus_one;
}
typedef simplestruct (STDCALL *SimpleDelegate2) (simplestruct ss);
static gboolean
is_utf16_equals (gunichar2 *s1, const char *s2)
{
char *s;
int res;
s = g_utf16_to_utf8 (s1, -1, NULL, NULL, NULL);
res = strcmp (s, s2);
g_free (s);
return res == 0;
}
LIBTEST_API int STDCALL
mono_test_marshal_delegate2 (SimpleDelegate2 delegate)
{
simplestruct ss, res;
ss.a = 0;
ss.b = 1;
ss.c = 0;
ss.d = "TEST";
ss.d2 = g_utf8_to_utf16 ("TEST2", -1, NULL, NULL, NULL);
res = delegate (ss);
if (! (res.a && !res.b && res.c && !strcmp (res.d, "TEST-RES") && is_utf16_equals (res.d2, "TEST2-RES")))
return 1;
return 0;
}
typedef simplestruct* (STDCALL *SimpleDelegate4) (simplestruct *ss);
LIBTEST_API int STDCALL
mono_test_marshal_delegate4 (SimpleDelegate4 delegate)
{
simplestruct ss;
simplestruct *res;
ss.a = 0;
ss.b = 1;
ss.c = 0;
ss.d = "TEST";
/* Check argument */
res = delegate (&ss);
if (!res)
return 1;
/* Check return value */
if (! (!res->a && res->b && !res->c && !strcmp (res->d, "TEST")))
return 2;
/* Check NULL argument and NULL result */
res = delegate (NULL);
if (res)
return 3;
return 0;
}
typedef int (STDCALL *SimpleDelegate5) (simplestruct **ss);
LIBTEST_API int STDCALL
mono_test_marshal_delegate5 (SimpleDelegate5 delegate)
{
simplestruct ss;
int res;
simplestruct *ptr;
ss.a = 0;
ss.b = 1;
ss.c = 0;
ss.d = "TEST";
ptr = &ss;
res = delegate (&ptr);
if (res != 0)
return 1;
if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
return 2;
return 0;
}
LIBTEST_API int STDCALL
mono_test_marshal_delegate6 (SimpleDelegate5 delegate)
{
int res;
res = delegate (NULL);
return 0;
}
typedef int (STDCALL *SimpleDelegate7) (simplestruct **ss);
LIBTEST_API int STDCALL
mono_test_marshal_delegate7 (SimpleDelegate7 delegate)
{
int res;
simplestruct *ptr;
/* Check that the input pointer is ignored */
ptr = (gpointer)0x12345678;
res = delegate (&ptr);
if (res != 0)
return 1;
if (!(ptr->a && !ptr->b && ptr->c && !strcmp (ptr->d, "RES")))
return 2;
return 0;
}
typedef int (STDCALL *InOutByvalClassDelegate) (simplestruct *ss);
LIBTEST_API int STDCALL
mono_test_marshal_inout_byval_class_delegate (InOutByvalClassDelegate delegate)
{
int res;
simplestruct ss;
ss.a = FALSE;
ss.b = TRUE;
ss.c = FALSE;
ss.d = g_strdup_printf ("%s", "FOO");
res = delegate (&ss);
if (res != 0)
return 1;
if (!(ss.a && !ss.b && ss.c && !strcmp (ss.d, "RES")))
return 2;
return 0;
}
typedef int (STDCALL *SimpleDelegate8) (gunichar2 *s);
LIBTEST_API int STDCALL
mono_test_marshal_delegate8 (SimpleDelegate8 delegate, gunichar2 *s)
{
return delegate (s);
}
typedef int (STDCALL *return_int_fnt) (int i);
typedef int (STDCALL *SimpleDelegate9) (return_int_fnt d);
LIBTEST_API int STDCALL
mono_test_marshal_delegate9 (SimpleDelegate9 delegate, gpointer ftn)
{
return delegate (ftn);
}
static int STDCALL
return_self (int i)
{
return i;
}
LIBTEST_API int STDCALL
mono_test_marshal_delegate10 (SimpleDelegate9 delegate)
{
return delegate (return_self);
}
typedef int (STDCALL *PrimitiveByrefDelegate) (int *i);
LIBTEST_API int STDCALL
mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate delegate)
{
int i = 1;
int res = delegate (&i);
if (res != 0)
return res;
if (i != 2)
return 2;
return 0;
}
typedef int (STDCALL *return_int_delegate) (int i);
typedef return_int_delegate (STDCALL *ReturnDelegateDelegate) (void);
LIBTEST_API int STDCALL
mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d)
{
return (d ()) (55);
}
LIBTEST_API int STDCALL
mono_test_marshal_stringbuilder (char *s, int n)
{
const char m[] = "This is my message. Isn't it nice?";
if (strcmp (s, "ABCD") != 0)
return 1;
strncpy(s, m, n);
s [n] = '\0';
return 0;
}
LIBTEST_API int STDCALL
mono_test_marshal_stringbuilder_default (char *s, int n)
{
const char m[] = "This is my message. Isn't it nice?";
strncpy(s, m, n);
s [n] = '\0';
return 0;
}
LIBTEST_API int STDCALL
mono_test_marshal_stringbuilder_unicode (gunichar2 *s, int n)
{
const char m[] = "This is my message. Isn't it nice?";
gunichar2* s2;
glong len;
s2 = g_utf8_to_utf16 (m, -1, NULL, &len, NULL);
len = (len * 2) + 2;
if (len > (n * 2))
len = n * 2;
memcpy (s, s2, len);
g_free (s2);
return 0;
}
typedef struct {
#ifndef __GNUC__