+//
+// Copyright 2011 Xamarin Inc (http://www.xamarin.com).
+//
+
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
+using System.Reflection.Emit;
public class Tests {
* Pointers to structures can not be passed
*/
+ /* This seems to be allowed by MS in some cases */
+ /*
public struct CharInfo {
public char Character;
public short Attributes;
}
return 1;
}
+ */
/*
* LPWStr marshalling
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;
+ }
+
}