From 25345fd1e070fdda233a43b7be070d66f187c557 Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Wed, 30 Jan 2019 01:35:09 +0100 Subject: [PATCH] Move Marshal::StringTo* methods to shared partition (#22266) --- .../System/Runtime/InteropServices/Marshal.cs | 128 ++++++++++++++++++ .../Runtime/InteropServices/Marshal.CoreCLR.cs | 145 --------------------- 2 files changed, 128 insertions(+), 145 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs b/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs index bb03656..570f942 100644 --- a/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs +++ b/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs @@ -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; + } + /// /// 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 diff --git a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs index d3ad1c9..d6b4d41 100644 --- a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs +++ b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs @@ -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 /// @@ -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)) -- 2.7.4