2004-03-14 Zoltan Varga <vargaz@freemail.hu>
[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 (8);
49                         Marshal.WriteInt32 (ptr, 0);
50                         Marshal.WriteInt32 (new IntPtr (ptr.ToInt32 () + Marshal.SizeOf (typeof(int))), number);
51                         return ptr;
52                 } catch {
53                         return IntPtr.Zero;
54                 }
55         }
56
57
58         // Convert a pointer to unmanaged data into a System.Object.
59         // This method simply converts the unmanaged Ansi C-string
60         // into a System.String and surrounds it with asterisks
61         // to differentiate it from the default marshaler.
62         public object MarshalNativeToManaged (IntPtr pNativeData)
63         {
64                 return "*" + Marshal.PtrToStringAnsi( pNativeData ) + "*";
65         }
66 }
67
68 public class Testing
69 {
70         [DllImport("libtest")]
71         private static extern int printInt([MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MyMarshal ))] object number );
72
73         [DllImport("libtest")]
74         private static extern void callFunction (Delegate d);
75
76         delegate void Del ([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MyMarshal))] string x);
77
78         public static void TestMethod (string s)
79         {
80                 Console.WriteLine("s = {0}", s);
81                 if (s != "*ABC*")
82                         throw new Exception ("received wrong value");
83         }
84
85         public static int Main()
86         {
87                 object x = 5;
88                 if (printInt (x) != 6)
89                         return 1;
90
91                 Del del = new Del (TestMethod);
92                 callFunction (del);
93
94                 return 0;
95         }
96
97
98
99 }