summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.cs128
-rw-r--r--src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs145
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 bb03656bcb..570f942c5e 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;
+ }
+
/// <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
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 d3ad1c9790..d6b4d41452 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
/// <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))