Add managed implementation of GetSystemMaxDBCSCharSize (#22290)
authorMarek Safar <marek.safar@gmail.com>
Thu, 31 Jan 2019 06:57:39 +0000 (07:57 +0100)
committerJan Kotas <jkotas@microsoft.com>
Thu, 31 Jan 2019 06:57:39 +0000 (22:57 -0800)
src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetCPInfo.cs
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.Unix.cs
src/System.Private.CoreLib/shared/System/Runtime/InteropServices/Marshal.Windows.cs
src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs
src/vm/ecalllist.h
src/vm/marshalnative.cpp
src/vm/marshalnative.h

index 1665119..8d523e4 100644 (file)
@@ -2,14 +2,13 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using System.IO;
-using System.Text;
 using System.Runtime.InteropServices;
 
 internal partial class Interop
 {
     internal partial class Kernel32
     {
+        [StructLayout(LayoutKind.Sequential)]
         internal unsafe struct CPINFO
         {
             internal int MaxCharSize;
@@ -19,6 +18,6 @@ internal partial class Interop
         }
 
         [DllImport(Libraries.Kernel32)]
-        internal static extern unsafe int GetCPInfo(uint codePage, CPINFO* lpCpInfo);
+        internal static extern unsafe Interop.BOOL GetCPInfo(uint codePage, CPINFO* lpCpInfo);
     }
 }
index 32c7e63..1f049bd 100644 (file)
@@ -9,6 +9,8 @@ namespace System.Runtime.InteropServices
 {
     public static partial class Marshal
     {
+        private static int GetSystemMaxDBCSCharSize() => 3;
+
         private static bool IsWin32Atom(IntPtr ptr) => false;
 
         internal static unsafe int StringToAnsiString(string s, byte* buffer, int bufferLength, bool bestFit = false, bool throwOnUnmappableChar = false)
index 2750243..5a44e49 100644 (file)
@@ -8,6 +8,16 @@ namespace System.Runtime.InteropServices
 {
     public static partial class Marshal
     {
+        private static unsafe int GetSystemMaxDBCSCharSize()
+        {
+            Interop.Kernel32.CPINFO cpInfo = default;
+
+            if (Interop.Kernel32.GetCPInfo(Interop.Kernel32.CP_ACP, &cpInfo) == Interop.BOOL.FALSE)
+                return 2;
+
+            return cpInfo.MaxCharSize;
+        }
+
         // Win32 has the concept of Atoms, where a pointer can either be a pointer
         // or an int.  If it's less than 64K, this is guaranteed to NOT be a
         // pointer since the bottom 64K bytes are reserved in a process' page table.
index d6b4d41..8bbfd87 100644 (file)
@@ -39,12 +39,6 @@ namespace System.Runtime.InteropServices
         private const int LMEM_FIXED = 0;
         private const int LMEM_MOVEABLE = 2;
 
-        /// <summary>
-        /// Helper method to retrieve the system's maximum DBCS character size.
-        /// </summary>
-        [MethodImpl(MethodImplOptions.InternalCall)]
-        private static extern int GetSystemMaxDBCSCharSize();
-
         [MethodImpl(MethodImplOptions.InternalCall)]
         internal static extern int SizeOfHelper(Type t, bool throwIfNotMarshalable);
 
index fb182e3..bef2de9 100644 (file)
@@ -805,7 +805,6 @@ FCFuncStart(gInteropMarshalFuncs)
     FCFuncElement("GetLastWin32Error", MarshalNative::GetLastWin32Error)
     FCFuncElement("SetLastWin32Error", MarshalNative::SetLastWin32Error)
     FCFuncElement("SizeOfHelper", MarshalNative::SizeOfClass)
-    FCFuncElement("GetSystemMaxDBCSCharSize", MarshalNative::GetSystemMaxDBCSCharSize)
     FCFuncElement("StructureToPtr", MarshalNative::StructureToPtr)
     FCFuncElement("PtrToStructureHelper", MarshalNative::PtrToStructureHelper)
     FCFuncElement("DestroyStructure", MarshalNative::DestroyStructure)
index 908a7b3..3419e3f 100644 (file)
@@ -481,15 +481,6 @@ FCIMPL3(LPVOID, MarshalNative::GetManagedThunkForUnmanagedMethodPtr, LPVOID pfnM
 }
 FCIMPLEND
 
-
-FCIMPL0(UINT32, MarshalNative::GetSystemMaxDBCSCharSize)
-{
-    FCALL_CONTRACT;
-    
-    return GetMaxDBCSCharByteSize();
-}
-FCIMPLEND
-
 /************************************************************************
  * PInvoke.GetLastWin32Error
  */
index 8f31688..064cd61 100644 (file)
@@ -68,8 +68,6 @@ public:
     static FCDECL3(LPVOID, GetUnmanagedThunkForManagedMethodPtr, LPVOID pfnMethodToWrap, PCCOR_SIGNATURE pbSignature, ULONG cbSignature);
     static FCDECL3(LPVOID, GetManagedThunkForUnmanagedMethodPtr, LPVOID pfnMethodToWrap, PCCOR_SIGNATURE pbSignature, ULONG cbSignature);
 
-    static FCDECL0(UINT32, GetSystemMaxDBCSCharSize);
-
     static FCDECL2(LPVOID, GCHandleInternalAlloc, Object *obj, int type);
     static FCDECL1(VOID, GCHandleInternalFree, OBJECTHANDLE handle);
     static FCDECL1(LPVOID, GCHandleInternalGet, OBJECTHANDLE handle);