3 using System
.Runtime
.InteropServices
;
4 using System
.Runtime
.CompilerServices
;
5 using Microsoft
.Win32
.SafeHandles
;
9 public class MyHandle
: SafeHandle
{
10 public MyHandle () : base (IntPtr
.Zero
, true)
14 public MyHandle (IntPtr x
) : base (x
, true)
19 public override bool IsInvalid
{
25 protected override bool ReleaseHandle ()
32 // No default public constructor here, this is so we can test
33 // that proper exceptions are thrown
35 public class MyHandleNoCtor
: SafeHandle
{
36 public MyHandleNoCtor (IntPtr handle
) : base (handle
, true)
40 public override bool IsInvalid
{
46 protected override bool ReleaseHandle ()
52 [DllImport ("libtest")]
53 public static extern void mono_safe_handle_ref (ref MyHandle handle
);
55 [DllImport ("libtest", EntryPoint
="mono_safe_handle_ref")]
56 public static extern void mono_safe_handle_ref2 (ref MyHandleNoCtor handle
);
58 public static int test_0_safehandle_ref_noctor ()
60 MyHandleNoCtor m
= new MyHandleNoCtor ((IntPtr
) 0xdead);
63 mono_safe_handle_ref2 (ref m
);
64 } catch (MissingMethodException e
){
65 Console
.WriteLine ("Good: got exception requried");
72 public static int test_0_safehandle_ref ()
74 MyHandle m
= new MyHandle ((IntPtr
) 0xdead);
76 mono_safe_handle_ref (ref m
);
78 if (m
.DangerousGetHandle () != (IntPtr
) 0x800d){
79 Console
.WriteLine ("test_0_safehandle_ref: fail; Expected 0x800d, got: {0:x}", m
.DangerousGetHandle ());
82 Console
.WriteLine ("test_0_safehandle_ref: pass");
86 [DllImport ("libtest")]
87 public static extern int mono_xr (SafeHandle sh
);
89 public static int test_0_marshal_safehandle_argument ()
91 SafeHandle s
= new SafeFileHandle ((IntPtr
) 0xeadcafe, true);
92 if (mono_xr (s
) != (0xeadcafe + 1234))
97 public static int test_0_marshal_safehandle_argument_null ()
101 } catch (ArgumentNullException
){
108 [StructLayout (LayoutKind
.Sequential
)]
109 public struct StringOnStruct
{
113 [StructLayout (LayoutKind
.Sequential
)]
114 public struct StructTest
{
116 public SafeHandle handle1
;
117 public SafeHandle handle2
;
121 [StructLayout (LayoutKind
.Sequential
)]
122 public struct StructTest1
{
126 [DllImport ("libtest")]
127 public static extern int mono_safe_handle_struct_ref (ref StructTest test
);
129 [DllImport ("libtest")]
130 public static extern int mono_safe_handle_struct (StructTest test
);
132 [DllImport ("libtest")]
133 public static extern int mono_safe_handle_struct_simple (StructTest1 test
);
135 [DllImport ("libtest", EntryPoint
="mono_safe_handle_return")]
136 public static extern SafeHandle
mono_safe_handle_return_1 ();
138 [DllImport ("libtest", EntryPoint
="mono_safe_handle_return")]
139 public static extern MyHandle
mono_safe_handle_return ();
141 [DllImport ("libtest", EntryPoint
="mono_safe_handle_return")]
142 public static extern MyHandleNoCtor
mono_safe_handle_return_2 ();
144 static StructTest x
= new StructTest ();
146 public static int test_0_safehandle_return_noctor ()
149 MyHandleNoCtor m
= mono_safe_handle_return_2 ();
150 } catch (MissingMethodException e
){
151 Console
.WriteLine ("GOOD: got exception required: " + e
);
155 Console
.WriteLine ("Failed, expected an exception because there is no paramterless ctor");
159 public static int test_0_safehandle_return_exc ()
162 SafeHandle x
= mono_safe_handle_return_1 ();
163 } catch (MarshalDirectiveException
){
164 Console
.WriteLine ("GOOD: got exception required");
168 Console
.WriteLine ("Error: should have generated an exception, since SafeHandle is abstract");
172 public static int test_0_safehandle_return ()
174 SafeHandle x
= mono_safe_handle_return ();
175 Console
.WriteLine ("Got the following handle: {0}", x
.DangerousGetHandle ());
176 return x
.DangerousGetHandle () == (IntPtr
) 0x1000f00d ? 0 : 1;
179 public static int test_0_marshal_safehandle_field ()
183 x
.handle1
= new SafeFileHandle ((IntPtr
) 0x7080feed, false);
184 x
.handle2
= new SafeFileHandle ((IntPtr
) 0x1234abcd, false);
186 if (mono_safe_handle_struct (x
) != 0xf00f)
192 public static int test_0_marshal_safehandle_field_ref ()
196 x
.handle1
= new SafeFileHandle ((IntPtr
) 0x7080feed, false);
197 x
.handle2
= new SafeFileHandle ((IntPtr
) 0x1234abcd, false);
199 if (mono_safe_handle_struct_ref (ref x
) != 0xf00d)
205 public static int test_0_simple ()
207 StructTest1 s
= new StructTest1 ();
208 s
.a
= new SafeFileHandle ((IntPtr
)1234, false);
210 return mono_safe_handle_struct_simple (s
) == 2468 ? 0 : 1;
213 public static int test_0_struct_empty ()
215 StructTest1 s
= new StructTest1 ();
218 mono_safe_handle_struct_simple (s
);
219 } catch (ArgumentNullException
){
225 public static int test_0_sf_dispose ()
227 SafeFileHandle sf
= new SafeFileHandle ((IntPtr
) 0x0d00d, false);
231 } catch (ObjectDisposedException
){
238 static int Error (string msg
)
240 Console
.WriteLine ("Error: " + msg
);
246 return TestDriver
.RunTests (typeof (Tests
));