* cpu-pentium.md basic-float.cs Fixed bug on fpu spills (see bug 54467),
[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                         IntPtr realPtr = new IntPtr (pNativeData.ToInt64 () - Marshal.SizeOf (typeof (int)));
29
30                         Marshal.FreeHGlobal (realPtr);
31                 }
32         }
33
34
35         // I really do not understand the purpose of this method
36         // or went it would be called. In fact, Rotor never seems
37         // to call it.
38         public int GetNativeDataSize ()
39         {
40                 Console.WriteLine("GetNativeDataSize() called");
41                 return 4;
42         }
43
44         public IntPtr MarshalManagedToNative (object managedObj)
45         {
46                 int number;
47                 IntPtr ptr;
48
49                 try {
50                         number = Convert.ToInt32 (managedObj);
51                         ptr = Marshal.AllocHGlobal (8);
52                         Marshal.WriteInt32 (ptr, 0);
53                         Marshal.WriteInt32 (new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (typeof(int))), number);
54                         return new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (typeof (int)));
55                 } catch {
56                         return IntPtr.Zero;
57                 }
58         }
59
60
61         // Convert a pointer to unmanaged data into a System.Object.
62         // This method simply converts the unmanaged Ansi C-string
63         // into a System.String and surrounds it with asterisks
64         // to differentiate it from the default marshaler.
65         public object MarshalNativeToManaged (IntPtr pNativeData)
66         {
67                 return "*" + Marshal.PtrToStringAnsi( pNativeData ) + "*";
68         }
69 }
70
71 public class Testing
72 {
73         [DllImport("libtest")]
74         private static extern int printInt([MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MyMarshal ))] object number );
75
76         [DllImport("libtest")]
77         private static extern void callFunction (Delegate d);
78
79         delegate void Del ([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MyMarshal))] string x);
80
81         public static void TestMethod (string s)
82         {
83                 Console.WriteLine("s = {0}", s);
84                 if (s != "*ABC*")
85                         throw new Exception ("received wrong value");
86         }
87
88         public static int Main()
89         {
90                 object x = 5;
91                 if (printInt (x) != 6)
92                         return 1;
93
94                 Del del = new Del (TestMethod);
95                 callFunction (del);
96
97                 return 0;
98         }
99
100
101
102 }