/
pinvoke2.cs
1595 lines (1201 loc) · 40 KB
/
pinvoke2.cs
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
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Reflection.Emit;
public class Tests {
public int int_field;
public static int delegate_test (int a)
{
if (a == 2)
return 0;
return 1;
}
public int delegate_test_instance (int a)
{
return int_field + a;
}
[StructLayout (LayoutKind.Sequential)]
public struct SimpleStruct {
public bool a;
public bool b;
public bool c;
public string d;
[MarshalAs(UnmanagedType.LPWStr)]
public string d2;
}
[StructLayout (LayoutKind.Sequential)]
public struct SimpleStructGen<T> {
public bool a;
public bool b;
public bool c;
public string d;
[MarshalAs(UnmanagedType.LPWStr)]
public string d2;
}
[StructLayout (LayoutKind.Sequential)]
public struct SimpleStruct2 {
public bool a;
public bool b;
public bool c;
public string d;
public byte e;
public double f;
public byte g;
public long h;
}
[StructLayout (LayoutKind.Sequential, Size=0)]
public struct EmptyStruct {
}
[StructLayout (LayoutKind.Sequential)]
public struct DelegateStruct {
public int a;
public SimpleDelegate del;
[MarshalAs(UnmanagedType.FunctionPtr)]
public SimpleDelegate del2;
[MarshalAs(UnmanagedType.FunctionPtr)]
public SimpleDelegate del3;
}
/* sparcv9 has complex conventions when passing structs with doubles in them
by value, some simple tests for them */
[StructLayout (LayoutKind.Sequential)]
public struct Point {
public double x;
public double y;
}
[StructLayout (LayoutKind.Sequential)]
public struct MixedPoint {
public int x;
public double y;
}
[StructLayout (LayoutKind.Sequential)]
public class SimpleClass {
public bool a;
public bool b;
public bool c;
public string d;
public byte e;
public double f;
public byte g;
public long h;
}
[StructLayout (LayoutKind.Sequential)]
public class EmptyClass {
}
[StructLayout (LayoutKind.Sequential)]
public struct LongAlignStruct {
public int a;
public long b;
public long c;
}
[StructLayout(LayoutKind.Sequential)]
public class BlittableClass
{
public int a = 1;
public int b = 2;
}
[StructLayout (LayoutKind.Sequential)]
class SimpleObj
{
public string str;
public int i;
}
[StructLayout(LayoutKind.Sequential)]
struct AsAnyStruct
{
public int i;
public int j;
public int k;
public String s;
public AsAnyStruct(int i, int j, int k, String s) {
this.i = i;
this.j = j;
this.k = k;
this.s = s;
}
}
[StructLayout(LayoutKind.Sequential)]
class AsAnyClass
{
public int i;
public int j;
public int k;
public String s;
public AsAnyClass(int i, int j, int k, String s) {
this.i = i;
this.j = j;
this.k = k;
}
}
[DllImport ("libnot-found", EntryPoint="not_found")]
public static extern int mono_library_not_found ();
[DllImport ("libtest", EntryPoint="not_found")]
public static extern int mono_entry_point_not_found ();
[DllImport ("libtest.dll", EntryPoint="mono_test_marshal_char")]
public static extern int mono_test_marshal_char_2 (char a1);
[DllImport ("test", EntryPoint="mono_test_marshal_char")]
public static extern int mono_test_marshal_char_3 (char a1);
[DllImport ("libtest", EntryPoint="mono_test_marshal_char")]
public static extern int mono_test_marshal_char (char a1);
[DllImport ("libtest", EntryPoint="mono_test_marshal_char_array", CharSet=CharSet.Unicode)]
public static extern int mono_test_marshal_char_array (char[] a1);
[DllImport ("libtest", EntryPoint="mono_test_marshal_bool_byref")]
public static extern int mono_test_marshal_bool_byref (int a, ref bool b, int c);
[DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
public static extern int mono_test_marshal_bool_in_as_I1 ([MarshalAs (UnmanagedType.I1)] bool bTrue, [MarshalAs (UnmanagedType.I1)] bool bFalse);
[DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
public static extern int mono_test_marshal_bool_in_as_U1 ([MarshalAs (UnmanagedType.U1)] bool bTrue, [MarshalAs (UnmanagedType.U1)] bool bFalse);
[DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
public static extern int mono_test_marshal_bool_out_as_I1 ([MarshalAs (UnmanagedType.I1)] out bool bTrue, [MarshalAs (UnmanagedType.I1)] out bool bFalse);
[DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
public static extern int mono_test_marshal_bool_out_as_U1 ([MarshalAs (UnmanagedType.U1)] out bool bTrue, [MarshalAs (UnmanagedType.U1)] out bool bFalse);
[DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
public static extern int mono_test_marshal_bool_ref_as_I1 ([MarshalAs (UnmanagedType.I1)] ref bool bTrue, [MarshalAs (UnmanagedType.I1)] ref bool bFalse);
[DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
public static extern int mono_test_marshal_bool_ref_as_U1 ([MarshalAs (UnmanagedType.U1)] ref bool bTrue, [MarshalAs (UnmanagedType.U1)] ref bool bFalse);
[DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
public static extern int mono_test_marshal_array (int [] a1);
[DllImport ("libtest", EntryPoint="mono_test_marshal_empty_string_array")]
public static extern int mono_test_marshal_empty_string_array (string [] a1);
[DllImport ("libtest", EntryPoint="mono_test_marshal_string_array")]
public static extern int mono_test_marshal_string_array (string [] a1);
[DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_string_array", CharSet=CharSet.Unicode)]
public static extern int mono_test_marshal_unicode_string_array (string [] a1, [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]string [] a2);
[DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_array")]
public static extern int mono_test_marshal_stringbuilder_array (StringBuilder [] a1);
[DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
[DllImport ("libtest", EntryPoint="mono_test_marshal_out_array")]
public static extern int mono_test_marshal_out_array ([Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] int [] a1, int n);
[DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
[DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
public static extern int mono_test_marshal_struct (SimpleStruct ss);
[DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
public static extern int mono_test_marshal_struct_gen (SimpleStructGen<string> ss);
[DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
[DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
[DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
public static extern int mono_test_marshal_byref_struct (ref SimpleStruct ss, bool a, bool b, bool c, String d);
[DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
public static extern int mono_test_marshal_byref_struct_in ([In] ref SimpleStruct ss, bool a, bool b, bool c, String d);
[DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
public static extern int mono_test_marshal_byref_struct_inout ([In, Out] ref SimpleStruct ss, bool a, bool b, bool c, String d);
[DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
public static extern int mono_test_marshal_point (Point p);
[DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
public static extern int mono_test_marshal_mixed_point (MixedPoint p);
[DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
[DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct")]
public static extern int mono_test_marshal_lpstruct ([In, MarshalAs(UnmanagedType.LPStruct)] SimpleStruct ss);
[DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct_blittable")]
public static extern int mono_test_marshal_lpstruct_blittable ([In, MarshalAs(UnmanagedType.LPStruct)] Point p);
[DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
[DllImport ("libtest", EntryPoint="mono_test_marshal_long_align_struct_array")]
public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct[] ss);
[DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
[DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
[DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
public static extern int mono_test_marshal_delegate (SimpleDelegate d);
[DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_struct")]
public static extern DelegateStruct mono_test_marshal_delegate_struct (DelegateStruct d);
[DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
public static extern SimpleDelegate mono_test_marshal_return_delegate (SimpleDelegate d);
[DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
[DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
[DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_default")]
public static extern void mono_test_marshal_stringbuilder_default (StringBuilder sb, int len);
[DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
[DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out")]
public static extern void mono_test_marshal_stringbuilder_out (out StringBuilder sb);
[DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out_unicode", CharSet=CharSet.Unicode)]
public static extern void mono_test_marshal_stringbuilder_out_unicode (out StringBuilder sb);
[DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
public static extern void mono_test_last_error (int err);
[DllImport ("libtest", EntryPoint="mono_test_asany")]
public static extern int mono_test_asany ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
[DllImport ("libtest", EntryPoint="mono_test_asany", CharSet=CharSet.Unicode)]
public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
[DllImport("libtest", EntryPoint="mono_test_marshal_asany_in")]
static extern void mono_test_asany_in ([MarshalAs(UnmanagedType.AsAny)][In] object obj);
[DllImport("libtest", EntryPoint="mono_test_marshal_asany_out")]
static extern void mono_test_asany_out ([MarshalAs(UnmanagedType.AsAny)][Out] object obj);
[DllImport("libtest", EntryPoint="mono_test_marshal_asany_inout")]
static extern void mono_test_asany_inout ([MarshalAs(UnmanagedType.AsAny)][In, Out] object obj);
[DllImport ("libtest")]
static extern int class_marshal_test0 (SimpleObj obj);
[DllImport ("libtest")]
static extern void class_marshal_test1 (out SimpleObj obj);
[DllImport ("libtest")]
static extern int class_marshal_test4 (SimpleObj obj);
[DllImport ("libtest")]
static extern int string_marshal_test0 (string str);
[DllImport ("libtest")]
static extern void string_marshal_test1 (out string str);
[DllImport ("libtest")]
static extern int string_marshal_test2 (ref string str);
[DllImport ("libtest")]
static extern int string_marshal_test3 (string str);
public delegate int SimpleDelegate (int a);
public static int Main (string[] args) {
return TestDriver.RunTests (typeof (Tests), args);
}
public static int test_0_marshal_char () {
return mono_test_marshal_char ('a');
}
public static int test_0_marshal_char_array () {
// a unicode char[] is implicitly marshalled as [Out]
char[] buf = new char [32];
mono_test_marshal_char_array (buf);
string s = new string (buf);
if (s.StartsWith ("abcdef"))
return 0;
else
return 1;
}
public static int test_1225_marshal_array () {
int [] a1 = new int [50];
for (int i = 0; i < 50; i++)
a1 [i] = i;
return mono_test_marshal_array (a1);
}
public static int test_1225_marshal_inout_array () {
int [] a1 = new int [50];
for (int i = 0; i < 50; i++)
a1 [i] = i;
int res = mono_test_marshal_inout_array (a1);
for (int i = 0; i < 50; i++)
if (a1 [i] != 50 - i) {
Console.WriteLine ("X: " + i + " " + a1 [i]);
return 2;
}
return res;
}
public static int test_0_marshal_out_array () {
int [] a1 = new int [50];
int res = mono_test_marshal_out_array (a1, 0);
for (int i = 0; i < 50; i++)
if (a1 [i] != i) {
Console.WriteLine ("X: " + i + " " + a1 [i]);
return 2;
}
return 0;
}
public static int test_0_marshal_inout_nonblittable_array () {
char [] a1 = new char [10];
for (int i = 0; i < 10; i++)
a1 [i] = "Hello, World" [i];
int res = mono_test_marshal_inout_nonblittable_array (a1);
for (int i = 0; i < 10; i++)
if (a1 [i] != 'F')
return 2;
return res;
}
public static int test_0_marshal_struct () {
SimpleStruct ss = new SimpleStruct ();
ss.b = true;
ss.d = "TEST";
return mono_test_marshal_struct (ss);
}
public static int test_0_marshal_struct_gen () {
SimpleStructGen<string> ss = new SimpleStructGen<string> ();
ss.b = true;
ss.d = "TEST";
return mono_test_marshal_struct_gen (ss);
}
public static int test_0_marshal_struct2 () {
SimpleStruct2 ss2 = new SimpleStruct2 ();
ss2.b = true;
ss2.d = "TEST";
ss2.e = 99;
ss2.f = 1.5;
ss2.g = 42;
ss2.h = 123L;
return mono_test_marshal_struct2 (ss2);
}
public static int test_0_marshal_struct3 () {
SimpleStruct2 ss2 = new SimpleStruct2 ();
ss2.b = true;
ss2.d = "TEST";
ss2.e = 99;
ss2.f = 1.5;
ss2.g = 42;
ss2.h = 123L;
return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
}
public static int test_0_marshal_empty_struct () {
EmptyStruct es = new EmptyStruct ();
if (mono_test_empty_struct (1, es, 2) != 0)
return 1;
return 0;
}
public static int test_0_marshal_lpstruct () {
SimpleStruct ss = new SimpleStruct ();
ss.b = true;
ss.d = "TEST";
return mono_test_marshal_lpstruct (ss);
}
public static int test_0_marshal_lpstruct_blittable () {
Point p = new Point ();
p.x = 1.0;
p.y = 2.0;
return mono_test_marshal_lpstruct_blittable (p);
}
public static int test_0_marshal_struct_array () {
SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
SimpleStruct2 ss2 = new SimpleStruct2 ();
ss2.b = true;
ss2.d = "TEST";
ss2.e = 99;
ss2.f = 1.5;
ss2.g = 42;
ss2.h = 123L;
ss_arr [0] = ss2;
ss2.b = false;
ss2.d = "TEST2";
ss2.e = 100;
ss2.f = 2.5;
ss2.g = 43;
ss2.h = 124L;
ss_arr [1] = ss2;
return mono_test_marshal_struct_array (ss_arr);
}
public static int test_105_marshal_long_align_struct_array () {
LongAlignStruct[] ss_arr = new LongAlignStruct [2];
LongAlignStruct ss = new LongAlignStruct ();
ss.a = 5;
ss.b = 10;
ss.c = 15;
ss_arr [0] = ss;
ss.a = 20;
ss.b = 25;
ss.c = 30;
ss_arr [1] = ss;
return mono_test_marshal_long_align_struct_array (ss_arr);
}
/* Test classes as arguments and return values */
public static int test_0_marshal_class () {
SimpleClass ss = new SimpleClass ();
ss.b = true;
ss.d = "TEST";
ss.e = 99;
ss.f = 1.5;
ss.g = 42;
ss.h = 123L;
SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
if (res == null)
return 1;
if (! (res.a == ss.a && res.b == ss.b && res.c == ss.c &&
res.d == ss.d && res.e == ss.e && res.f == ss.f &&
res.g == ss.g && res.h == ss.h))
return 2;
/* Test null arguments and results */
res = mono_test_marshal_class (10, 11, 12, null, 14);
if (res != null)
return 3;
return 0;
}
public static int test_0_marshal_byref_class () {
SimpleClass ss = new SimpleClass ();
ss.b = true;
ss.d = "TEST";
ss.e = 99;
ss.f = 1.5;
ss.g = 42;
ss.h = 123L;
int res = mono_test_marshal_byref_class (ref ss);
if (ss.d != "TEST-RES")
return 1;
return 0;
}
public static int test_0_marshal_delegate () {
SimpleDelegate d = new SimpleDelegate (delegate_test);
return mono_test_marshal_delegate (d);
}
public static int test_34_marshal_instance_delegate () {
Tests t = new Tests ();
t.int_field = 32;
SimpleDelegate d = new SimpleDelegate (t.delegate_test_instance);
return mono_test_marshal_delegate (d);
}
/* Static delegates closed over their first argument */
public static int closed_delegate (Tests t, int a) {
return t.int_field + a;
}
public static int test_34_marshal_closed_static_delegate () {
Tests t = new Tests ();
t.int_field = 32;
SimpleDelegate d = (SimpleDelegate)Delegate.CreateDelegate (typeof (SimpleDelegate), t, typeof (Tests).GetMethod ("closed_delegate"));
return mono_test_marshal_delegate (d);
}
public static int test_0_marshal_return_delegate () {
SimpleDelegate d = new SimpleDelegate (delegate_test);
SimpleDelegate d2 = mono_test_marshal_return_delegate (d);
return d2 (2);
}
public static int test_0_marshal_delegate_struct () {
DelegateStruct s = new DelegateStruct ();
s.a = 2;
s.del = new SimpleDelegate (delegate_test);
s.del2 = new SimpleDelegate (delegate_test);
s.del3 = null;
DelegateStruct res = mono_test_marshal_delegate_struct (s);
if (res.a != 0)
return 1;
if (res.del (2) != 0)
return 2;
if (res.del2 (2) != 0)
return 3;
if (res.del3 != null)
return 4;
return 0;
}
[DllImport ("libtest", EntryPoint="mono_test_marshal_out_delegate")]
public static extern int mono_test_marshal_out_delegate (out SimpleDelegate d);
public static int test_3_marshal_out_delegate () {
SimpleDelegate d = null;
mono_test_marshal_out_delegate (out d);
return d (2);
}
public static int test_0_marshal_byref_struct () {
SimpleStruct s = new SimpleStruct ();
s.a = true;
s.b = false;
s.c = true;
s.d = "ABC";
s.d2 = "DEF";
int res = mono_test_marshal_byref_struct (ref s, true, false, true, "ABC");
if (res != 0)
return 1;
if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
return 2;
return 0;
}
public static int test_0_marshal_byref_struct_in () {
SimpleStruct s = new SimpleStruct ();
s.a = true;
s.b = false;
s.c = true;
s.d = "ABC";
s.d2 = "DEF";
int res = mono_test_marshal_byref_struct_in (ref s, true, false, true, "ABC");
if (res != 0)
return 1;
if (s.a != true || s.b != false || s.c != true || s.d != "ABC")
return 2;
return 0;
}
public static int test_0_marshal_byref_struct_inout () {
SimpleStruct s = new SimpleStruct ();
s.a = true;
s.b = false;
s.c = true;
s.d = "ABC";
s.d2 = "DEF";
int res = mono_test_marshal_byref_struct_inout (ref s, true, false, true, "ABC");
if (res != 0)
return 1;
if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
return 2;
return 0;
}
public static int test_0_marshal_point () {
Point pt = new Point();
pt.x = 1.25;
pt.y = 3.5;
return mono_test_marshal_point(pt);
}
public static int test_0_marshal_mixed_point () {
MixedPoint mpt = new MixedPoint();
mpt.x = 5;
mpt.y = 6.75;
return mono_test_marshal_mixed_point(mpt);
}
public static int test_0_marshal_bool_byref () {
bool b = true;
if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
return 1;
b = false;
if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
return 12;
if (b != true)
return 13;
return 0;
}
public static int test_0_marshal_bool_as_I1 () {
int ret;
bool bTrue, bFalse;
if ((ret = mono_test_marshal_bool_in_as_I1 (true, false)) != 0)
return ret;
if ((ret = mono_test_marshal_bool_out_as_I1 (out bTrue, out bFalse)) != 0)
return ret;
if(!bTrue)
return 10;
if(bFalse)
return 11;
if ((ret = mono_test_marshal_bool_ref_as_I1 (ref bTrue, ref bFalse)) != 0)
return ret;
if(bTrue)
return 12;
if(!bFalse)
return 13;
return 0;
}
public static int test_0_marshal_bool_as_U1 () {
int ret;
bool bTrue, bFalse;
if ((ret = mono_test_marshal_bool_in_as_U1 (true, false)) != 0)
return ret;
if ((ret = mono_test_marshal_bool_out_as_U1 (out bTrue, out bFalse)) != 0)
return ret;
if(!bTrue)
return 10;
if(bFalse)
return 11;
if ((ret = mono_test_marshal_bool_ref_as_U1 (ref bTrue, ref bFalse)) != 0)
return ret;
if(bTrue)
return 12;
if(!bFalse)
return 13;
return 0;
}
public static int test_0_return_vtype () {
SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
if (!ss.a && ss.b && !ss.c && ss.d == "TEST" && ss.d2 == "TEST2")
return 0;
return 1;
}
public static int test_0_marshal_stringbuilder () {
StringBuilder sb = new StringBuilder(255);
sb.Append ("ABCD");
mono_test_marshal_stringbuilder (sb, sb.Capacity);
String res = sb.ToString();
if (res != "This is my message. Isn't it nice?")
return 1;
// Test StringBuilder with default capacity (16)
StringBuilder sb2 = new StringBuilder();
mono_test_marshal_stringbuilder_default (sb2, sb2.Capacity);
if (sb2.ToString () != "This is my messa")
return 2;
return 0;
}
public static int test_0_marshal_stringbuilder_unicode () {
StringBuilder sb = new StringBuilder(255);
mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
String res = sb.ToString();
if (res != "This is my message. Isn't it nice?")
return 1;
// Test StringBuilder with default capacity (16)
StringBuilder sb2 = new StringBuilder();
mono_test_marshal_stringbuilder_unicode (sb2, sb2.Capacity);
if (sb2.ToString () != "This is my messa")
return 2;
return 0;
}
public static int test_0_marshal_stringbuilder_out () {
StringBuilder sb;
mono_test_marshal_stringbuilder_out (out sb);
if (sb.ToString () != "This is my message. Isn't it nice?")
return 1;
return 0;
}
public static int test_0_marshal_stringbuilder_out_unicode () {
StringBuilder sb;
mono_test_marshal_stringbuilder_out_unicode (out sb);
if (sb.ToString () != "This is my message. Isn't it nice?")
return 1;
return 0;
}
public static int test_0_marshal_empty_string_array () {
return mono_test_marshal_empty_string_array (null);
}
public static int test_0_marshal_string_array () {
return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
}
public static int test_0_marshal_unicode_string_array () {
return mono_test_marshal_unicode_string_array (new String [] { "ABC", "DEF" }, new String [] { "ABC", "DEF" });
}
public static int test_0_marshal_stringbuilder_array () {
StringBuilder sb1 = new StringBuilder ("ABC");
StringBuilder sb2 = new StringBuilder ("DEF");
int res = mono_test_marshal_stringbuilder_array (new StringBuilder [] { sb1, sb2 });
if (res != 0)
return res;
if (sb1.ToString () != "DEF")
return 5;
if (sb2.ToString () != "ABC")
return 6;
return 0;
}
public static int test_0_last_error () {
mono_test_last_error (5);
if (Marshal.GetLastWin32Error () == 5)
return 0;
else
return 1;
}
public static int test_0_entry_point_not_found () {
try {
mono_entry_point_not_found ();
return 1;
}
catch (EntryPointNotFoundException) {
}
return 0;
}
public static int test_0_library_not_found () {
try {
mono_library_not_found ();
return 1;
}
catch (DllNotFoundException) {
}
return 0;
}
/* Check that the runtime trims .dll from the library name */
public static int test_0_trim_dll_from_name () {
mono_test_marshal_char_2 ('A');
return 0;
}
/* Check that the runtime adds lib to to the library name */
public static int test_0_add_lib_to_name () {
mono_test_marshal_char_3 ('A');
return 0;
}
class C {
public int i;
}
public static int test_0_asany () {
if (mono_test_asany (5, 1) != 0)
return 1;
if (mono_test_asany ("ABC", 2) != 0)
return 2;
SimpleStruct2 ss2 = new SimpleStruct2 ();
ss2.b = true;
ss2.d = "TEST";
ss2.e = 99;
ss2.f = 1.5;
ss2.g = 42;
ss2.h = 123L;
if (mono_test_asany (ss2, 3) != 0)
return 3;
if (mono_test_asany_unicode ("ABC", 4) != 0)
return 4;
try {
C c = new C ();
c.i = 5;
mono_test_asany (c, 0);
return 5;
}
catch (ArgumentException) {
}
try {
mono_test_asany (new Object (), 0);
return 6;
}
catch (ArgumentException) {
}
return 0;
}
/* AsAny marshalling + [In, Out] */
public static int test_0_asany_in () {
// Struct
AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
mono_test_asany_in (str);
// Formatted Class
AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
mono_test_asany_in (cls);
if ((cls.i != 1) || (cls.j != 2) || (cls.k != 3))
return 1;
// Boxed Struct
object obj = new AsAnyStruct(1,2,3, "ABC");
mono_test_asany_in (obj);
str = (AsAnyStruct)obj;
if ((str.i != 1) || (str.j != 2) || (str.k != 3))
return 2;
return 0;
}
public static int test_0_asany_out () {
// Struct
AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
mono_test_asany_out (str);
// Formatted Class
AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
mono_test_asany_out (cls);
if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
return 1;
// Boxed Struct
object obj = new AsAnyStruct(1,2,3, "ABC");
mono_test_asany_out (obj);
str = (AsAnyStruct)obj;
if ((str.i != 10) || (str.j != 20) || (str.k != 30))
return 2;
return 0;
}
public static int test_0_asany_inout () {
// Struct
AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
mono_test_asany_inout (str);
// Formatted Class
AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
mono_test_asany_inout (cls);
if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
return 1;
// Boxed Struct
object obj = new AsAnyStruct(1,2,3, "ABC");
mono_test_asany_inout (obj);
str = (AsAnyStruct)obj;
if ((str.i != 10) || (str.j != 20) || (str.k != 30))
return 2;
return 0;
}
/* Byref String Array */
[DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_array")]