Fix parsing decimal zero values. Fixes #6444
[mono.git] / mono / tests / pinvoke2.cs
index 10acef7457252e36db02d4090494d3dc4fc0bc27..d600170061792bf6acad84010be8ce204d6a6f4a 100644 (file)
@@ -1,7 +1,12 @@
+//
+// 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 {
 
@@ -283,6 +288,9 @@ public class Tests {
        [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out")]
        public static extern void mono_test_marshal_stringbuilder_out (out StringBuilder sb);
 
+       [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_ref")]
+       public static extern int mono_test_marshal_stringbuilder_ref (ref StringBuilder sb);
+
        [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out_unicode", CharSet=CharSet.Unicode)]
        public static extern void mono_test_marshal_stringbuilder_out_unicode (out StringBuilder sb);
 
@@ -811,6 +819,18 @@ public class Tests {
                return 0;
        }
 
+       public static int test_0_marshal_stringbuilder_ref () {
+               StringBuilder sb = new StringBuilder ();
+               sb.Append ("ABC");
+               int res = mono_test_marshal_stringbuilder_ref (ref sb);
+               if (res != 0)
+                       return 1;
+               
+               if (sb.ToString () != "This is my message.  Isn't it nice?")
+                       return 2;  
+               return 0;
+       }
+
        public static int test_0_marshal_empty_string_array () {
                return mono_test_marshal_empty_string_array (null);
        }
@@ -1270,7 +1290,12 @@ public class Tests {
        public static int test_0_marshal_byref_string () {
                string res = "TEST1";
 
-               return string_marshal_test2 (ref res);
+               int r = string_marshal_test2 (ref res);
+               if (r != 0)
+                       return 1;
+               if (res != "TEST2")
+                       return 2;
+               return 0;
        }
 
        public static int test_0_marshal_null_string () {
@@ -1299,6 +1324,8 @@ 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;
@@ -1320,6 +1347,7 @@ public class Tests {
                }
                return 1;
        }
+       */
 
        /*
         * LPWStr marshalling
@@ -1537,5 +1565,156 @@ public class Tests {
                        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;
+       }
+
 }