/
handleref.cs
109 lines (91 loc) · 2.38 KB
/
handleref.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
using System;
using System.Runtime.InteropServices;
public class Tests {
//
// This is not permitted, should throw an exception
//
[DllImport ("libtest")]
public static extern void mono_safe_handle_ref (ref HandleRef handle);
[DllImport ("libtest", EntryPoint="mono_xr_as_handle")]
public static extern HandleRef mono_xr_as_handle (HandleRef r);
[DllImport ("libtest")]
public static extern int mono_xr (HandleRef sh);
//
// Mono should throw exceptions on ref HandleRefs
//
public static int test_0_ref_handleref ()
{
object o = new object ();
HandleRef s = new HandleRef (o, (IntPtr) 0xeadcafe);
try {
mono_safe_handle_ref (ref s);
} catch (MarshalDirectiveException){
return 0;
}
// failed
return 1;
}
//
// Mono should throw excentions on return HandleRefs
//
public static int test_0_handleref_return ()
{
object o = new object ();
HandleRef s = new HandleRef (o, (IntPtr) 0xeadcafe);
try {
HandleRef ret = mono_xr_as_handle (s);
} catch (MarshalDirectiveException){
return 0;
}
// failed
return 1;
}
public static int test_0_marshal_handleref_argument ()
{
object o = new object ();
Console.WriteLine ("BEFORE");
HandleRef s = new HandleRef (o, (IntPtr) 0xeadcafe);
if (mono_xr (s) != (0xeadcafe + 1234))
return 1;
Console.WriteLine ("AFTER");
return 0;
}
[StructLayout (LayoutKind.Sequential)]
public struct StructTest {
public int a;
public HandleRef handle1;
public HandleRef handle2;
public int b;
}
[DllImport ("libtest")]
public static extern int mono_safe_handle_struct_ref (ref StructTest test);
[DllImport ("libtest")]
public static extern int mono_safe_handle_struct (StructTest test);
static StructTest x = new StructTest ();
public static int test_0_marshal_safehandle_field ()
{
x.a = 1234;
x.b = 8743;
object o = new object ();
x.handle1 = new HandleRef (o, (IntPtr) 0x7080feed);
x.handle2 = new HandleRef (o, (IntPtr) 0x1234abcd);
if (mono_safe_handle_struct (x) != 0xf00f)
return 1;
return 0;
}
public static int test_0_marshal_safehandle_field_ref ()
{
x.a = 1234;
x.b = 8743;
object o = new object ();
x.handle1 = new HandleRef (o, (IntPtr) 0x7080feed);
x.handle2 = new HandleRef (o, (IntPtr) 0x1234abcd);
if (mono_safe_handle_struct_ref (ref x) != 0xf00d)
return 1;
return 0;
}
static int Main ()
{
return TestDriver.RunTests (typeof (Tests));
}
}