Fri Dec 19 17:58:28 CET 2003 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / tests / marshal9.cs
1 // A demonstration of a custom marshaler that marshals
2 // unmanaged to managed data.
3
4 using System;
5 using System.Runtime.InteropServices;
6
7 public class MyMarshal: ICustomMarshaler
8 {
9
10         // GetInstance() is not part of ICustomMarshaler, but
11         // custom marshalers are required to implement this
12         // method.
13         public static ICustomMarshaler GetInstance (string s)
14         {
15                 Console.WriteLine ("GetInstance called");
16                 return new MyMarshal ();
17         }
18         
19         public void CleanUpManagedData (object managedObj)
20         {
21                 Console.WriteLine ("CleanUpManagedData called");
22         }
23
24         public void CleanUpNativeData (IntPtr pNativeData)
25         {
26                 Console.WriteLine("CleanUpNativeData called");
27                 if (pNativeData != IntPtr.Zero)
28                         Marshal.FreeHGlobal (pNativeData);
29         }
30
31
32         // I really do not understand the purpose of this method
33         // or went it would be called. In fact, Rotor never seems
34         // to call it.
35         public int GetNativeDataSize ()
36         {
37                 Console.WriteLine("GetNativeDataSize() called");
38                 return 4;
39         }
40
41         public IntPtr MarshalManagedToNative (object managedObj)
42         {
43                 int number;
44                 IntPtr ptr;
45
46                 try {
47                         number = Convert.ToInt32 (managedObj);
48                         ptr = Marshal.AllocHGlobal (4);
49                         Marshal.WriteInt32 (ptr, number);
50                         return ptr;
51                 } catch {
52                         return IntPtr.Zero;
53                 }
54         }
55
56
57         // Convert a pointer to unmanaged data into a System.Object.
58         // This method simply converts the unmanaged Ansi C-string
59         // into a System.String and surrounds it with asterisks
60         // to differentiate it from the default marshaler.
61         public object MarshalNativeToManaged (IntPtr pNativeData)
62         {
63                 return "*" + Marshal.PtrToStringAnsi( pNativeData ) + "*";
64         }
65 }
66
67 public class Testing
68 {
69         [DllImport("libtest")]
70         private static extern int printInt([MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MyMarshal ))] object number );
71
72         [DllImport("libtest")]
73         private static extern void callFunction (Delegate d);
74
75         delegate void Del ([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MyMarshal))] string x);
76
77         public static void TestMethod (string s)
78         {
79                 Console.WriteLine("s = {0}", s);
80                 if (s != "*ABC*")
81                         throw new Exception ("received wrong value");
82         }
83
84         public static int Main()
85         {
86                 object x = 5;
87                 if (printInt (x) != 6)
88                         return 1;
89
90                 Del del = new Del (TestMethod);
91                 callFunction (del);
92
93                 return 0;
94         }
95
96
97
98 }