Move Marshal::StringTo* methods to shared partition (#22266)
authorMarek Safar <marek.safar@gmail.com>
Wed, 30 Jan 2019 00:35:09 +0000 (01:35 +0100)
committerJan Kotas <jkotas@microsoft.com>
Wed, 30 Jan 2019 00:35:09 +0000 (16:35 -0800)
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs
src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs

index bb03656..570f942 100644 (file)
@@ -651,6 +651,134 @@ namespace System.Runtime.InteropServices
             return s.MarshalToString(globalAlloc: true, unicode: true); ;
         }
 
+        public static unsafe IntPtr StringToHGlobalAnsi(string s)
+        {
+            if (s == null)
+            {
+                return IntPtr.Zero;
+            }
+
+            long lnb = (s.Length + 1) * (long)SystemMaxDBCSCharSize;
+            int nb = (int)lnb;
+
+            // Overflow checking
+            if (nb != lnb)
+            {
+                throw new ArgumentOutOfRangeException(nameof(s));
+            }
+
+            IntPtr hglobal = AllocHGlobal((IntPtr)nb);
+
+            StringToAnsiString(s, (byte*)hglobal, nb);
+            return hglobal;
+        }
+
+        public static unsafe IntPtr StringToHGlobalUni(string s)
+        {
+            if (s == null)
+            {
+                return IntPtr.Zero;
+            }
+
+            int nb = (s.Length + 1) * 2;
+
+            // Overflow checking
+            if (nb < s.Length)
+            {
+                throw new ArgumentOutOfRangeException(nameof(s));
+            }
+
+            IntPtr hglobal = AllocHGlobal((IntPtr)nb);
+            
+            fixed (char* firstChar = s)
+            {
+                string.wstrcpy((char*)hglobal, firstChar, s.Length + 1);
+            }
+            return hglobal;
+        }
+
+        public static IntPtr StringToHGlobalAuto(string s)
+        {
+            // Ansi platforms are no longer supported
+            return StringToHGlobalUni(s);
+        }
+
+        public static unsafe IntPtr StringToCoTaskMemUni(string s)
+        {
+            if (s == null)
+            {
+                return IntPtr.Zero;
+            }
+
+            int nb = (s.Length + 1) * 2;
+
+            // Overflow checking
+            if (nb < s.Length)
+            {
+                throw new ArgumentOutOfRangeException(nameof(s));
+            }
+
+            IntPtr hglobal = AllocCoTaskMem(nb);
+
+            fixed (char* firstChar = s)
+            {
+                string.wstrcpy((char*)hglobal, firstChar, s.Length + 1);
+            }
+            return hglobal;
+        }
+
+        public static unsafe IntPtr StringToCoTaskMemUTF8(string s)
+        {
+            if (s == null)
+            {
+                return IntPtr.Zero;
+            }
+
+            int nb = Encoding.UTF8.GetMaxByteCount(s.Length);
+
+            IntPtr pMem = AllocCoTaskMem(nb + 1);
+
+            int nbWritten;
+            byte* pbMem = (byte*)pMem;
+
+            fixed (char* firstChar = s)
+            {
+                nbWritten = Encoding.UTF8.GetBytes(firstChar, s.Length, pbMem, nb);
+            }
+
+            pbMem[nbWritten] = 0;
+
+            return pMem;
+        }
+
+        public static IntPtr StringToCoTaskMemAuto(string s)
+        {
+            // Ansi platforms are no longer supported
+            return StringToCoTaskMemUni(s);
+        }
+
+        public static unsafe IntPtr StringToCoTaskMemAnsi(string s)
+        {
+            if (s == null)
+            {
+                return IntPtr.Zero;
+            }
+
+            long lnb = (s.Length + 1) * (long)SystemMaxDBCSCharSize;
+            int nb = (int)lnb;
+
+            // Overflow checking
+            if (nb != lnb)
+            {
+                throw new ArgumentOutOfRangeException(nameof(s));
+            }
+
+            IntPtr hglobal = AllocCoTaskMem(nb);
+
+            StringToAnsiString(s, (byte*)hglobal, nb);
+            return hglobal;
+        }
+
         /// <summary>
         /// Generates a GUID for the specified type. If the type has a GUID in the
         /// metadata then it is returned otherwise a stable guid is generated based
index d3ad1c9..d6b4d41 100644 (file)
@@ -337,68 +337,6 @@ namespace System.Runtime.InteropServices
 
             return pNewMem;
         }
-    
-        public static unsafe IntPtr StringToHGlobalAnsi(string s)
-        {
-            if (s == null)
-            {
-                return IntPtr.Zero;
-            }
-
-            long lnb = (s.Length + 1) * (long)SystemMaxDBCSCharSize;
-            int nb = (int)lnb;
-
-            // Overflow checking
-            if (nb != lnb)
-            {
-                throw new ArgumentOutOfRangeException(nameof(s));
-            }
-
-            UIntPtr len = new UIntPtr((uint)nb);
-            IntPtr hglobal = Win32Native.LocalAlloc_NoSafeHandle(LMEM_FIXED, len);
-            if (hglobal == IntPtr.Zero)
-            {
-                throw new OutOfMemoryException();
-            }
-
-            StringToAnsiString(s, (byte*)hglobal, nb);
-            return hglobal;
-        }
-
-        public static unsafe IntPtr StringToHGlobalUni(string s)
-        {
-            if (s == null)
-            {
-                return IntPtr.Zero;
-            }
-
-            int nb = (s.Length + 1) * 2;
-
-            // Overflow checking
-            if (nb < s.Length)
-            {
-                throw new ArgumentOutOfRangeException(nameof(s));
-            }
-
-            UIntPtr len = new UIntPtr((uint)nb);
-            IntPtr hglobal = Win32Native.LocalAlloc_NoSafeHandle(LMEM_FIXED, len);
-            if (hglobal == IntPtr.Zero)
-            {
-                throw new OutOfMemoryException();
-            }
-
-            fixed (char* firstChar = s)
-            {
-                string.wstrcpy((char*)hglobal, firstChar, s.Length + 1);
-            }
-            return hglobal;
-        }
-
-        public static IntPtr StringToHGlobalAuto(string s)
-        {
-            // Ansi platforms are no longer supported
-            return StringToHGlobalUni(s);
-        }
 
 #if FEATURE_COMINTEROP
         /// <summary>
@@ -531,89 +469,6 @@ namespace System.Runtime.InteropServices
             return pNewMem;
         }
 
-        public static unsafe IntPtr StringToCoTaskMemUni(string s)
-        {
-            if (s == null)
-            {
-                return IntPtr.Zero;
-            }
-
-            int nb = (s.Length + 1) * 2;
-
-            // Overflow checking
-            if (nb < s.Length)
-            {
-                throw new ArgumentOutOfRangeException(nameof(s));
-            }
-
-            IntPtr hglobal = Win32Native.CoTaskMemAlloc(new UIntPtr((uint)nb));
-            if (hglobal == IntPtr.Zero)
-            {
-                throw new OutOfMemoryException();
-            }
-
-            fixed (char* firstChar = s)
-            {
-                string.wstrcpy((char*)hglobal, firstChar, s.Length + 1);
-            }
-            return hglobal;
-        }
-
-        public static unsafe IntPtr StringToCoTaskMemUTF8(string s)
-        {
-            if (s == null)
-            {
-                return IntPtr.Zero;
-            }
-
-            int nb = Encoding.UTF8.GetMaxByteCount(s.Length);
-            IntPtr pMem = Win32Native.CoTaskMemAlloc(new UIntPtr((uint)nb + 1));
-            if (pMem == IntPtr.Zero)
-            {
-                throw new OutOfMemoryException();
-            }
-
-            fixed (char* firstChar = s)
-            {
-                byte* pbMem = (byte*)pMem;
-                int nbWritten = Encoding.UTF8.GetBytes(firstChar, s.Length, pbMem, nb);
-                pbMem[nbWritten] = 0;
-            }
-            return pMem;
-        }
-
-        public static IntPtr StringToCoTaskMemAuto(string s)
-        {
-            // Ansi platforms are no longer supported
-            return StringToCoTaskMemUni(s);
-        }
-
-        public static unsafe IntPtr StringToCoTaskMemAnsi(string s)
-        {
-            if (s == null)
-            {
-                return IntPtr.Zero;
-            }
-
-            long lnb = (s.Length + 1) * (long)SystemMaxDBCSCharSize;
-            int nb = (int)lnb;
-
-            // Overflow checking
-            if (nb != lnb)
-            {
-                throw new ArgumentOutOfRangeException(nameof(s));
-            }
-
-            IntPtr hglobal = Win32Native.CoTaskMemAlloc(new UIntPtr((uint)nb));
-            if (hglobal == IntPtr.Zero)
-            {
-                throw new OutOfMemoryException();
-            }
-
-            StringToAnsiString(s, (byte*)hglobal, nb);
-            return hglobal;
-        }
-
         public static void FreeCoTaskMem(IntPtr ptr)
         {
             if (!IsWin32Atom(ptr))