1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.Runtime.CompilerServices; 6 using System.Security; 7 8 namespace System.Runtime.InteropServices 9 { 10 /// <summary> 11 /// This PInvokeMarshal class should provide full public Marshal 12 /// implementation for all things related to P/Invoke marshalling 13 /// </summary> 14 public partial class PInvokeMarshal 15 { SaveLastWin32Error()16 public static void SaveLastWin32Error() 17 { 18 s_lastWin32Error = Interop.Sys.GetErrNo(); 19 } 20 ClearLastWin32Error()21 public static void ClearLastWin32Error() 22 { 23 Interop.Sys.ClearErrNo(); 24 } 25 IsWin32Atom(IntPtr ptr)26 private static bool IsWin32Atom(IntPtr ptr) 27 { 28 return false; 29 } 30 PtrToStringAnsi(IntPtr ptr)31 public static unsafe String PtrToStringAnsi(IntPtr ptr) 32 { 33 if (IntPtr.Zero == ptr) 34 { 35 return null; 36 } 37 38 int len = Internal.Runtime.CompilerHelpers.InteropHelpers.strlen((byte*)ptr); 39 if (len == 0) 40 { 41 return string.Empty; 42 } 43 44 return System.Text.Encoding.UTF8.GetString((byte*)ptr, len); 45 } 46 MemAlloc(IntPtr cb)47 public static unsafe IntPtr MemAlloc(IntPtr cb) 48 { 49 return Interop.MemAlloc((UIntPtr)(void*)cb); 50 } 51 MemFree(IntPtr hglobal)52 public static void MemFree(IntPtr hglobal) 53 { 54 Interop.MemFree(hglobal); 55 } 56 MemReAlloc(IntPtr pv, IntPtr cb)57 public static unsafe IntPtr MemReAlloc(IntPtr pv, IntPtr cb) 58 { 59 return Interop.MemReAlloc(pv, new UIntPtr((void*)cb)); 60 } 61 CoTaskMemAlloc(UIntPtr bytes)62 public static IntPtr CoTaskMemAlloc(UIntPtr bytes) 63 { 64 return Interop.MemAlloc(bytes); 65 } 66 CoTaskMemFree(IntPtr allocatedMemory)67 public static void CoTaskMemFree(IntPtr allocatedMemory) 68 { 69 Interop.MemFree(allocatedMemory); 70 } 71 CoTaskMemReAlloc(IntPtr pv, IntPtr cb)72 public static unsafe IntPtr CoTaskMemReAlloc(IntPtr pv, IntPtr cb) 73 { 74 return Interop.MemReAlloc(pv, new UIntPtr((void*)cb)); 75 } 76 SecureStringToBSTR(SecureString s)77 public static IntPtr SecureStringToBSTR(SecureString s) 78 { 79 if (s == null) 80 { 81 throw new ArgumentNullException(nameof(s)); 82 } 83 throw new PlatformNotSupportedException(); 84 } 85 86 // In CoreRT on Unix, there is not yet a BSTR implementation. On Windows, we would use SysAllocStringLen from OleAut32.dll. AllocBSTR(int length)87 internal static IntPtr AllocBSTR(int length) 88 { 89 throw new PlatformNotSupportedException(); 90 } 91 FreeBSTR(IntPtr ptr)92 internal static void FreeBSTR(IntPtr ptr) 93 { 94 throw new PlatformNotSupportedException(); 95 } 96 97 #region String marshalling 98 ConvertMultiByteToWideChar(byte* multiByteStr, int multiByteLen, char* wideCharStr, int wideCharLen)99 public static unsafe int ConvertMultiByteToWideChar(byte* multiByteStr, 100 int multiByteLen, 101 char* wideCharStr, 102 int wideCharLen) 103 { 104 return System.Text.Encoding.UTF8.GetChars(multiByteStr, multiByteLen, wideCharStr, wideCharLen); 105 } 106 ConvertWideCharToMultiByte(char* wideCharStr, int wideCharLen, byte* multiByteStr, int multiByteLen, bool bestFit, bool throwOnUnmappableChar)107 public static unsafe int ConvertWideCharToMultiByte(char* wideCharStr, 108 int wideCharLen, 109 byte* multiByteStr, 110 int multiByteLen, 111 bool bestFit, 112 bool throwOnUnmappableChar) 113 { 114 return System.Text.Encoding.UTF8.GetBytes(wideCharStr, wideCharLen, multiByteStr, multiByteLen); 115 } 116 ConvertWideCharToMultiByte(char* wideCharStr, int wideCharLen, byte* multiByteStr, int multiByteLen)117 public static unsafe int ConvertWideCharToMultiByte(char* wideCharStr, 118 int wideCharLen, 119 byte* multiByteStr, 120 int multiByteLen) 121 { 122 return System.Text.Encoding.UTF8.GetBytes(wideCharStr, wideCharLen, multiByteStr, multiByteLen); 123 } 124 GetByteCount(char* wideCharStr, int wideCharLen)125 public static unsafe int GetByteCount(char* wideCharStr, int wideCharLen) 126 { 127 return System.Text.Encoding.UTF8.GetByteCount(wideCharStr, wideCharLen); 128 } 129 GetCharCount(byte* multiByteStr, int multiByteLen)130 public static unsafe int GetCharCount(byte* multiByteStr, int multiByteLen) 131 { 132 return System.Text.Encoding.UTF8.GetCharCount(multiByteStr, multiByteLen); 133 } 134 GetSystemMaxDBCSCharSize()135 public static unsafe int GetSystemMaxDBCSCharSize() 136 { 137 return 3; 138 } 139 #endregion 140 } 141 } 142