Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / marshal5.cs
1 using System;
2 using System.Runtime.InteropServices;
3
4 public class Test 
5 {
6         [DllImport ("libtest", EntryPoint="mono_test_byvalstr_gen")]
7         public static extern IntPtr mono_test_byvalstr_gen();
8
9         [DllImport ("libtest", EntryPoint="mono_test_byvalstr_check")]
10         public static extern int mono_test_byvalstr_check(IntPtr data, string correctString);
11
12         [DllImport ("libtest", EntryPoint="mono_test_byvalstr_check_unicode")]
13         public static extern int mono_test_byvalstr_check_unicode(ref ByValStrStruct_Unicode var, int test);
14         
15         [StructLayout (LayoutKind.Sequential)]
16         public struct ByValStrStruct 
17         {
18                 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
19                 public string a;
20         }
21
22         [StructLayout (LayoutKind.Sequential, CharSet=CharSet.Unicode)]
23         public struct ByValStrStruct_Unicode
24         {
25                 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=4)]
26                 public string a;
27
28                 public int flag;
29         }
30         
31         public unsafe static int Main () 
32         {
33                 string testString = "A small string";
34
35                 IntPtr udata = mono_test_byvalstr_gen();
36
37                 ByValStrStruct data = new ByValStrStruct();
38                 data.a = testString;
39
40                 Marshal.StructureToPtr(data, udata, false);
41
42                 int c = mono_test_byvalstr_check(udata, testString);
43                 if (c != 0)
44                         return 1;
45
46                 ByValStrStruct_Unicode a = new ByValStrStruct_Unicode ();
47                 a.flag = 0x1234abcd;
48                 a.a = "1234";
49                 c = mono_test_byvalstr_check_unicode (ref a, 1);
50                 if (c != 0)
51                         return 2;
52
53                 a.a = "12";
54                 c = mono_test_byvalstr_check_unicode (ref a, 2);
55                 if (c != 0)
56                         return 3;
57
58                 a.a = "1234567890";
59                 c = mono_test_byvalstr_check_unicode (ref a, 3);
60                 if (c != 0)
61                         return 4;
62
63                 return 0;
64         }
65 }
66