+
+ /*
+ * Marshalling of type 'object'
+ */
+
+ [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
+ public static extern SimpleClass mono_test_marshal_object (int i, int j, int k, object ss, int l);
+
+ public static int test_0_marshal_object () {
+ try {
+ mono_test_marshal_object (0, 0, 0, null, 0);
+ return 1;
+ } catch (Exception) {
+ return 0;
+ }
+ }
+
+ /*
+ * Marshalling of DateTime to OLE DATE (double)
+ */
+ [DllImport ("libtest", EntryPoint="mono_test_marshal_date_time")]
+ public static extern double mono_test_marshal_date_time (DateTime d, out DateTime d2);
+
+ public static int test_0_marshal_date_time () {
+ DateTime d = new DateTime (2009, 12, 6);
+ DateTime d2;
+ double d3 = mono_test_marshal_date_time (d, out d2);
+ if (d3 != 40153.0)
+ return 1;
+ if (d2 != d)
+ return 2;
+ return 0;
+ }
+
+ /*
+ * Calling pinvoke functions dynamically using calli
+ */
+
+ [DllImport("libtest")]
+ private static extern IntPtr mono_test_marshal_lookup_symbol (string fileName);
+
+ delegate void CalliDel (IntPtr a, int[] f);
+
+ public static int test_0_calli_dynamic () {
+ /* we need the cdecl version because the icall convention demands it under Windows */
+ IntPtr func = mono_test_marshal_lookup_symbol ("mono_test_marshal_inout_array_cdecl");
+
+ DynamicMethod dm = new DynamicMethod ("calli", typeof (void), new Type [] { typeof (IntPtr), typeof (int[]) });
+
+ var il = dm.GetILGenerator ();
+ var signature = SignatureHelper.GetMethodSigHelper (CallingConvention.Cdecl, typeof (void));
+
+ il.Emit (OpCodes.Ldarg, 1);
+ signature.AddArgument (typeof (byte[]));
+
+ il.Emit (OpCodes.Ldarg_0);
+
+ il.Emit (OpCodes.Calli, signature);
+ il.Emit (OpCodes.Ret);
+
+ var f = (CalliDel)dm.CreateDelegate (typeof (CalliDel));
+
+ int[] arr = new int [1000];
+ for (int i = 0; i < 50; ++i)
+ arr [i] = (int)i;
+ f (func, arr);
+ if (arr.Length != 1000)
+ return 1;
+ for (int i = 0; i < 50; ++i)
+ if (arr [i] != 50 - i)
+ return 2;
+
+ return 0;
+ }
+
+
+ /*char array marshaling */
+ [DllImport ("libtest", EntryPoint="mono_test_marshal_ansi_char_array", CharSet=CharSet.Ansi)]
+ public static extern int mono_test_marshal_ansi_char_array (char[] a1);
+
+ public static int test_0_marshal_ansi_char_array () {
+ char[] buf = new char [32];
+ buf [0] = 'q';
+ buf [1] = 'w';
+ buf [2] = 'e';
+ buf [3] = 'r';
+
+ if (mono_test_marshal_ansi_char_array (buf) != 0)
+ return 1;
+
+ string s = new string (buf);
+ if (s.StartsWith ("qwer"))
+ return 0;
+ else
+ return 2;
+ }
+
+ /*char array marshaling */
+ [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_char_array", CharSet=CharSet.Unicode)]
+ public static extern int mono_test_marshal_unicode_char_array (char[] a1);
+
+ public static int test_0_marshal_unicode_char_array () {
+ char[] buf = new char [32];
+ buf [0] = 'q';
+ buf [1] = 'w';
+ buf [2] = 'e';
+ buf [3] = 'r';
+
+ if (mono_test_marshal_unicode_char_array (buf) != 0)
+ return 1;
+
+ string s = new string (buf);
+ if (s.StartsWith ("abcdef"))
+ return 0;
+ else
+ return 2;
+ }
+
+ [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstr")]
+ public static extern int mono_test_marshal_lpstr ([MarshalAs(UnmanagedType.LPStr)] string str);
+
+ public static int test_0_mono_test_marshal_lpstr () {
+ string str = "ABC";
+
+ if (mono_test_marshal_lpstr (str) != 0)
+ return 1;
+
+ return 0;
+ }
+
+ [DllImport ("libtest", EntryPoint="mono_test_marshal_lpwstr")]
+ public static extern int mono_test_marshal_lpwstr ([MarshalAs(UnmanagedType.LPWStr)] string str);
+
+ public static int test_0_mono_test_marshal_lpwstr () {
+ string str = "ABC";
+
+ if (mono_test_marshal_lpwstr (str) != 0)
+ return 1;
+
+ return 0;
+ }
+
+
+ [method: DllImport ("libtest", EntryPoint="mono_test_marshal_return_lpstr")]
+ [return: MarshalAs(UnmanagedType.LPStr)]
+ public static extern string mono_test_marshal_return_lpstr ();
+
+ public static int test_0_mono_test_marshal_return_lpstr () {
+ string str = mono_test_marshal_return_lpstr ();
+ if ("XYZ" == str)
+ return 0;
+
+ return 1;
+ }
+
+ [method: DllImport ("libtest", EntryPoint="mono_test_marshal_return_lpwstr")]
+ [return: MarshalAs(UnmanagedType.LPWStr)]
+ public static extern string mono_test_marshal_return_lpwstr ();
+
+ public static int test_0_mono_test_marshal_return_lpwstr () {
+ string str = mono_test_marshal_return_lpwstr ();
+ if ("XYZ" == str)
+ return 0;
+
+ return 1;
+ }
+