File: marshal5.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (66 lines) | stat: -rw-r--r-- 1,586 bytes parent folder | download | duplicates (11)
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
using System;
using System.Runtime.InteropServices;

public class Test 
{
	[DllImport ("libtest", EntryPoint="mono_test_byvalstr_gen")]
	public static extern IntPtr mono_test_byvalstr_gen();

	[DllImport ("libtest", EntryPoint="mono_test_byvalstr_check")]
	public static extern int mono_test_byvalstr_check(IntPtr data, string correctString);

	[DllImport ("libtest", EntryPoint="mono_test_byvalstr_check_unicode")]
	public static extern int mono_test_byvalstr_check_unicode(ref ByValStrStruct_Unicode var, int test);
	
	[StructLayout (LayoutKind.Sequential)]
	public struct ByValStrStruct 
	{
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
		public string a;
	}

	[StructLayout (LayoutKind.Sequential, CharSet=CharSet.Unicode)]
	public struct ByValStrStruct_Unicode
	{
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst=4)]
		public string a;

		public int flag;
	}
	
	public unsafe static int Main () 
	{
		string testString = "A small string";

		IntPtr udata = mono_test_byvalstr_gen();

		ByValStrStruct data = new ByValStrStruct();
		data.a = testString;

		Marshal.StructureToPtr(data, udata, false);

		int c = mono_test_byvalstr_check(udata, testString);
		if (c != 0)
			return 1;

		ByValStrStruct_Unicode a = new ByValStrStruct_Unicode ();
		a.flag = 0x1234abcd;
		a.a = "1234";
		c = mono_test_byvalstr_check_unicode (ref a, 1);
		if (c != 0)
			return 2;

		a.a = "12";
		c = mono_test_byvalstr_check_unicode (ref a, 2);
		if (c != 0)
			return 3;

		a.a = "1234567890";
		c = mono_test_byvalstr_check_unicode (ref a, 3);
		if (c != 0)
			return 4;

		return 0;
	}
}