Merge pull request #3913 from omwok/master
[mono.git] / mcs / class / corlib / System.Runtime.InteropServices / Marshal.cs
index 48c5db2ea62adb508e662063fc3fca9e4ad276a6..1516216ddc202f9758f32eb7f794620576b18332 100644 (file)
@@ -37,10 +37,11 @@ using System;
 using System.Security;
 using System.Reflection;
 using System.Threading;
+using System.Runtime.InteropServices.ComTypes;
+using System.Text;
 
 using System.Runtime.ConstrainedExecution;
 #if !FULL_AOT_RUNTIME
-using System.Runtime.InteropServices.ComTypes;
 using Mono.Interop;
 #endif
 
@@ -68,8 +69,24 @@ namespace System.Runtime.InteropServices
 #endif
                }
 
+               [MonoTODO]
+               public static bool AreComObjectsAvailableForCleanup ()
+               {
+                       return false;
+               }
+
+               [MonoTODO]
+               public static void CleanupUnusedObjectsInCurrentContext ()
+               {
+                       if (Environment.IsRunningOnWindows)
+                               throw new PlatformNotSupportedException ();
+               }
+
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr AllocCoTaskMem (int cb);
+               
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               internal extern static IntPtr AllocCoTaskMemSize (UIntPtr sizet);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
@@ -187,15 +204,15 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-#if NET_4_5
                public static IntPtr CreateAggregatedObject<T> (IntPtr pOuter, T o) {
                        return CreateAggregatedObject (pOuter, (object)o);
                }
-#endif
 
-#if !FULL_AOT_RUNTIME
                public static object CreateWrapperOfType (object o, Type t)
                {
+#if FULL_AOT_RUNTIME
+                       throw new PlatformNotSupportedException ();
+#else
                        __ComObject co = o as __ComObject;
                        if (co == null)
                                throw new ArgumentException ("o must derive from __ComObject", "o");
@@ -209,24 +226,20 @@ namespace System.Runtime.InteropServices
                        }
 
                        return ComInteropProxy.GetProxy (co.IUnknown, t).GetTransparentProxy ();
+#endif
                }
 
-#if NET_4_5
                public static TWrapper CreateWrapperOfType<T, TWrapper> (T o) {
                        return (TWrapper)CreateWrapperOfType ((object)o, typeof (TWrapper));
                }
-#endif
-#endif
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                [ComVisible (true)]
                public extern static void DestroyStructure (IntPtr ptr, Type structuretype);
 
-#if NET_4_5
                public static void DestroyStructure<T> (IntPtr ptr) {
                        DestroyStructure (ptr, typeof (T));
                }
-#endif                 
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static void FreeBSTR (IntPtr ptr);
@@ -276,6 +289,12 @@ namespace System.Runtime.InteropServices
                        FreeCoTaskMem (s);
                }
 
+               public static void ZeroFreeCoTaskMemUTF8 (IntPtr s)
+               {
+                       ClearAnsi (s);
+                       FreeCoTaskMem (s);
+               }
+               
                public static void ZeroFreeGlobalAllocAnsi (IntPtr s)
                {
                        ClearAnsi (s);
@@ -335,24 +354,30 @@ namespace System.Runtime.InteropServices
                                return GetCCW (o, T);
                }
 #endif
+#endif // !FULL_AOT_RUNTIME
 
                public static IntPtr GetComInterfaceForObject (object o, Type T)
                {
-#if !MOBILE
+#if MOBILE
+                       throw new PlatformNotSupportedException ();
+#else
                        IntPtr pItf = GetComInterfaceForObjectInternal (o, T);
                        AddRef (pItf);
                        return pItf;
-#else
-                       throw new NotImplementedException ();
 #endif
                }
 
-#if NET_4_5
+               [MonoTODO]
+               public static IntPtr GetComInterfaceForObject (object o, Type T, CustomQueryInterfaceMode mode)
+               {
+                       throw new NotImplementedException ();
+               }
+
                public static IntPtr GetComInterfaceForObject<T, TInterface> (T o) {
                        return GetComInterfaceForObject ((object)o, typeof (T));
                }
-#endif                 
 
+#if !FULL_AOT_RUNTIME
                [MonoTODO]
                public static IntPtr GetComInterfaceForObjectInContext (object o, Type t)
                {
@@ -391,12 +416,6 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
-               public static int GetExceptionCode()
-               {
-                       throw new NotImplementedException ();
-               }
-
                [MonoTODO]
                [ComVisible (true)]
                public static IntPtr GetExceptionPointers()
@@ -413,22 +432,35 @@ namespace System.Runtime.InteropServices
                }
 #endif // !FULL_AOT_RUNTIME
 
-#if !FULL_AOT_RUNTIME
+               public static int GetExceptionCode ()
+               {
+                       throw new PlatformNotSupportedException ();
+               }
+
                public static int GetHRForException (Exception e)
                {
+                       if (e == null) return 0;
+
+#if FEATURE_COMINTEROP
                        var errorInfo = new ManagedErrorInfo(e);
                        SetErrorInfo (0, errorInfo);
+#endif
 
-                       return e.hresult;
+                       return e._HResult;
                }
 
                [MonoTODO]
                [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
                public static int GetHRForLastWin32Error()
                {
+#if FULL_AOT_RUNTIME
+                       throw new PlatformNotSupportedException ();
+#else
                        throw new NotImplementedException ();
+#endif
                }
 
+#if !FULL_AOT_RUNTIME
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern static IntPtr GetIDispatchForObjectInternal (object o);
 
@@ -452,17 +484,6 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-               [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               private extern static IntPtr GetIUnknownForObjectInternal (object o);
-
-               public static IntPtr GetIUnknownForObject (object o)
-               {
-                       IntPtr pUnk = GetIUnknownForObjectInternal (o);
-                       // Internal method does not AddRef
-                       AddRef (pUnk);
-                       return pUnk;
-               }
-
                [MonoTODO]
                public static IntPtr GetIUnknownForObjectInContext (object o)
                {
@@ -482,27 +503,48 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               private extern static IntPtr GetIUnknownForObjectInternal (object o);
+
+#endif // !FULL_AOT_RUNTIME
+
+               public static IntPtr GetIUnknownForObject (object o)
+               {
+#if FULL_AOT_RUNTIME
+                       throw new PlatformNotSupportedException ();
+#else
+                       IntPtr pUnk = GetIUnknownForObjectInternal (o);
+                       // Internal method does not AddRef
+                       AddRef (pUnk);
+                       return pUnk;
+#endif
+               }
+
                public static void GetNativeVariantForObject (object obj, IntPtr pDstNativeVariant)
                {
+#if FULL_AOT_RUNTIME
+                       throw new PlatformNotSupportedException ();
+#else
                        Variant vt = new Variant();
                        vt.SetValue(obj);
                        Marshal.StructureToPtr(vt, pDstNativeVariant, false);
+#endif
                }
 
-#if NET_4_5
                public static void GetNativeVariantForObject<T> (T obj, IntPtr pDstNativeVariant) {
                        GetNativeVariantForObject ((object)obj, pDstNativeVariant);
                }
-#endif
 
-#if !MOBILE
+#if !MOBILE && !FULL_AOT_RUNTIME
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private static extern object GetObjectForCCW (IntPtr pUnk);
 #endif
 
                public static object GetObjectForIUnknown (IntPtr pUnk)
                {
-#if !MOBILE
+#if MOBILE || FULL_AOT_RUNTIME
+                       throw new PlatformNotSupportedException ();
+#else
                        object obj = GetObjectForCCW (pUnk);
                        // was not a CCW
                        if (obj == null) {
@@ -510,26 +552,34 @@ namespace System.Runtime.InteropServices
                                obj = proxy.GetTransparentProxy ();
                        }
                        return obj;
-#else
-                       throw new NotImplementedException ();
 #endif
                }
 
                public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant)
                {
+#if FULL_AOT_RUNTIME
+                       throw new PlatformNotSupportedException ();
+#else
                        Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
                        return vt.GetValue();
+#endif
                }
 
-#if NET_4_5
-               public static T GetObjectForNativeVariant<T> (IntPtr pSrcNativeVariant) {
+               public static T GetObjectForNativeVariant<T> (IntPtr pSrcNativeVariant)
+               {
+#if FULL_AOT_RUNTIME
+                       throw new PlatformNotSupportedException ();
+#else
                        Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
                        return (T)vt.GetValue();
-               }
 #endif
+               }
 
                public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars)
                {
+#if FULL_AOT_RUNTIME
+                       throw new PlatformNotSupportedException ();
+#else
                        if (cVars < 0)
                                throw new ArgumentOutOfRangeException ("cVars", "cVars cannot be a negative number.");
                        object[] objects = new object[cVars];
@@ -537,10 +587,14 @@ namespace System.Runtime.InteropServices
                                objects[i] = GetObjectForNativeVariant ((IntPtr)(aSrcNativeVariant.ToInt64 () +
                                        i * SizeOf (typeof(Variant))));
                        return objects;
+#endif
                }
 
-#if NET_4_5
-               public static T[] GetObjectsForNativeVariants<T> (IntPtr aSrcNativeVariant, int cVars) {
+               public static T[] GetObjectsForNativeVariants<T> (IntPtr aSrcNativeVariant, int cVars)
+               {
+#if FULL_AOT_RUNTIME
+                       throw new PlatformNotSupportedException ();
+#else
                        if (cVars < 0)
                                throw new ArgumentOutOfRangeException ("cVars", "cVars cannot be a negative number.");
                        T[] objects = new T[cVars];
@@ -548,15 +602,20 @@ namespace System.Runtime.InteropServices
                                objects[i] = GetObjectForNativeVariant<T> ((IntPtr)(aSrcNativeVariant.ToInt64 () +
                                        i * SizeOf (typeof(Variant))));
                        return objects;
-               }
 #endif
+               }
 
                [MonoTODO]
                public static int GetStartComSlot (Type t)
                {
+#if FULL_AOT_RUNTIME
+                       throw new PlatformNotSupportedException ();
+#else
                        throw new NotImplementedException ();
+#endif
                }
 
+#if !FULL_AOT_RUNTIME
                [MonoTODO]
                [Obsolete ("This method has been deprecated")]
                public static Thread GetThreadFromFiberCookie (int cookie)
@@ -583,14 +642,6 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-#if NET_4_5
-               public static Type GetTypeFromCLSID (Guid clsid)
-               {
-                       throw new NotImplementedException ();                   
-               }
-#endif
-
-#if !FULL_AOT_RUNTIME
                [Obsolete]
                [MonoTODO]
                public static string GetTypeInfoName (UCOMITypeInfo pTI)
@@ -598,11 +649,6 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-               public static string GetTypeInfoName (ITypeInfo typeInfo)
-               {
-                       throw new NotImplementedException ();
-               }
-
                [Obsolete]
                [MonoTODO]
                public static Guid GetTypeLibGuid (UCOMITypeLib pTLB)
@@ -654,12 +700,6 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-               public static object GetUniqueObjectForIUnknown (IntPtr unknown)
-               {
-                       throw new NotImplementedException ();
-               }
-#endif
-
                [MonoTODO]
                [Obsolete ("This method has been deprecated")]
                public static IntPtr GetUnmanagedThunkForManagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
@@ -667,16 +707,6 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-#if !MOBILE
-               [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               public extern static bool IsComObject (object o);
-#else
-               public static bool IsComObject (object o)
-               {
-                       throw new NotImplementedException ();
-               }
-#endif         
-
                [MonoTODO]
                public static bool IsTypeVisibleFromCom (Type t)
                {
@@ -688,6 +718,31 @@ namespace System.Runtime.InteropServices
                {
                        throw new NotImplementedException ();
                }
+#endif // !FULL_AOT_RUNTIME
+
+               public static Type GetTypeFromCLSID (Guid clsid)
+               {
+                       throw new PlatformNotSupportedException ();
+               }
+
+               public static string GetTypeInfoName (ITypeInfo typeInfo)
+               {
+                       throw new PlatformNotSupportedException ();
+               }
+
+               public static object GetUniqueObjectForIUnknown (IntPtr unknown)
+               {
+                       throw new PlatformNotSupportedException ();
+               }
+
+#if !MOBILE
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               public extern static bool IsComObject (object o);
+#else
+               public static bool IsComObject (object o)
+               {
+                       throw new PlatformNotSupportedException ();
+               }
 #endif
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
@@ -697,11 +752,9 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr OffsetOf (Type t, string fieldName);
 
-#if NET_4_5
                public static IntPtr OffsetOf<T> (string fieldName) {
                        return OffsetOf (typeof (T), fieldName);
                }
-#endif
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static void Prelink (MethodInfo m);
@@ -715,6 +768,16 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static string PtrToStringAnsi (IntPtr ptr, int len);
 
+               public static string PtrToStringUTF8 (IntPtr ptr)
+               {
+                       return PtrToStringAnsi (ptr);
+               }
+               
+               public static string PtrToStringUTF8 (IntPtr ptr, int byteLen)
+               {
+                       return PtrToStringAnsi (ptr, byteLen);
+               }
+               
                public static string PtrToStringAuto (IntPtr ptr)
                {
                        return SystemDefaultCharSize == 2
@@ -733,15 +796,8 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static string PtrToStringUni (IntPtr ptr, int len);
 
-#if !MOBILE
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static string PtrToStringBSTR (IntPtr ptr);
-#else
-               public static string PtrToStringBSTR (IntPtr ptr)
-               {
-                       throw new NotImplementedException ();
-               }
-#endif
                
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                [ComVisible (true)]
@@ -751,7 +807,6 @@ namespace System.Runtime.InteropServices
                [ComVisible (true)]
                public extern static object PtrToStructure (IntPtr ptr, Type structureType);
 
-#if NET_4_5
                public static void PtrToStructure<T> (IntPtr ptr, T structure) {
                        PtrToStructure (ptr, (object)structure);
                }
@@ -759,7 +814,6 @@ namespace System.Runtime.InteropServices
                public static T PtrToStructure<T> (IntPtr ptr) {
                        return (T) PtrToStructure (ptr, typeof (T));
                }
-#endif
 
 #if !MOBILE
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
@@ -808,7 +862,7 @@ namespace System.Runtime.InteropServices
                                return *(short*)addr;
 
                        short s;
-                       String.memcpy ((byte*)&s, (byte*)ptr, 2);
+                       Buffer.Memcpy ((byte*)&s, (byte*)ptr, 2);
                        return s;
                }
 
@@ -820,7 +874,7 @@ namespace System.Runtime.InteropServices
                                return *(short*)addr;
 
                        short s;
-                       String.memcpy ((byte*)&s, addr, 2);
+                       Buffer.Memcpy ((byte*)&s, addr, 2);
                        return s;
                }
 
@@ -840,7 +894,7 @@ namespace System.Runtime.InteropServices
                                return *(int*)addr;
 
                        int s;
-                       String.memcpy ((byte*)&s, addr, 4);
+                       Buffer.Memcpy ((byte*)&s, addr, 4);
                        return s;
                }
 
@@ -853,7 +907,7 @@ namespace System.Runtime.InteropServices
                                return *(int*)addr;
                        else {
                                int s;
-                               String.memcpy ((byte*)&s, addr, 4);
+                               Buffer.Memcpy ((byte*)&s, addr, 4);
                                return s;
                        }
                }
@@ -877,7 +931,7 @@ namespace System.Runtime.InteropServices
                                return *(long*)ptr;
 
                        long s;
-                       String.memcpy ((byte*)&s, addr, 8);
+                       Buffer.Memcpy ((byte*)&s, addr, 8);
                        return s;
                }
 
@@ -889,7 +943,7 @@ namespace System.Runtime.InteropServices
                                return *(long*)addr;
                        
                        long s;
-                       String.memcpy ((byte*)&s, addr, 8);
+                       Buffer.Memcpy ((byte*)&s, addr, 8);
                        return s;
                }
 
@@ -954,16 +1008,22 @@ namespace System.Runtime.InteropServices
 #if !FULL_AOT_RUNTIME
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern static int ReleaseComObjectInternal (object co);
+#endif
 
                public static int ReleaseComObject (object o)
                {
+#if FULL_AOT_RUNTIME
+                       throw new PlatformNotSupportedException ();
+#else
                        if (o == null)
                                throw new ArgumentException ("Value cannot be null.", "o");
                        if (!IsComObject (o))
                                throw new ArgumentException ("Value must be a Com object.", "o");
                        return ReleaseComObjectInternal (o);
+#endif
                }
 
+#if !FULL_AOT_RUNTIME
                [Obsolete]
                [MonoTODO]
                public static void ReleaseThreadCache()
@@ -987,7 +1047,6 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static int SizeOf (Type t);
 
-#if NET_4_5
                public static int SizeOf<T> () {
                        return SizeOf (typeof (T));
                }
@@ -995,28 +1054,28 @@ namespace System.Runtime.InteropServices
                public static int SizeOf<T> (T structure) {
                        return SizeOf (structure.GetType ());
                }
-#endif
+
+               internal static uint SizeOfType (Type type)
+               {
+                       return (uint) SizeOf (type);
+               }
+
+               internal static uint AlignedSizeOf<T> () where T : struct
+               {
+                       uint size = SizeOfType (typeof (T));
+                       if (size == 1 || size == 2)
+                               return size;
+                       if (IntPtr.Size == 8 && size == 4)
+                               return size;
+                       return (size + 3) & (~((uint)3));
+               }
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr StringToBSTR (string s);
 
-               //
-               // I believe this is wrong, because in Mono and in P/Invoke
-               // we treat "Ansi" conversions as UTF-8 conversions, while
-               // this one does not do this
-               //
                public static IntPtr StringToCoTaskMemAnsi (string s)
                {
-                       int length = s.Length + 1;
-                       IntPtr ctm = AllocCoTaskMem (length);
-
-                       byte[] asBytes = new byte[length];
-                       for (int i = 0; i < s.Length; i++)
-                               asBytes[i] = (byte)s[i];
-                       asBytes[s.Length] = 0;
-
-                       copy_to_unmanaged (asBytes, 0, ctm, length);
-                       return ctm;
+                       return StringToAllocatedMemoryUTF8 (s);
                }
 
                public static IntPtr StringToCoTaskMemAuto (string s)
@@ -1041,6 +1100,29 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr StringToHGlobalAnsi (string s);
 
+               unsafe public static IntPtr StringToAllocatedMemoryUTF8(String s)
+               {
+                       const int MAX_UTF8_CHAR_SIZE = 3;
+                       if (s == null)
+                               return IntPtr.Zero;
+
+                       int nb = (s.Length + 1) * MAX_UTF8_CHAR_SIZE;
+
+                       // Overflow checking
+                       if (nb < s.Length)
+                               throw new ArgumentOutOfRangeException("s");
+                       
+                       IntPtr pMem = AllocCoTaskMemSize(new UIntPtr((uint)nb +1));
+                       
+                       if (pMem == IntPtr.Zero)
+                               throw new OutOfMemoryException();
+
+                       byte* pbMem = (byte*)pMem;
+                       int nbWritten = s.GetBytesFromEncoding(pbMem, nb, Encoding.UTF8);
+                       pbMem[nbWritten] = 0;
+                       return pMem;
+               }
+               
                public static IntPtr StringToHGlobalAuto (string s)
                {
                        return SystemDefaultCharSize == 2
@@ -1054,25 +1136,21 @@ namespace System.Runtime.InteropServices
                {
                        if (s == null)
                                throw new ArgumentNullException ("s");
-                       int len = s.Length;
-                       IntPtr ctm = AllocCoTaskMem ((len+1) * 2 + 4);
-                       byte [] buffer = null;
-                       WriteInt32 (ctm, 0, len*2);
-                       try {
-                               buffer = s.GetBuffer ();
 
-                               for (int i = 0; i < len; i++)
-                                       WriteInt16 (ctm, 4 + (i * 2), (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
-                               WriteInt16 (ctm, 4 + buffer.Length, 0);
-                       } finally {
-                               if (buffer != null)
-                                       for (int i = buffer.Length; i > 0; ){
-                                               i--;
-                                               buffer [i] = 0;
-                                       }
+                       byte[] buffer = s.GetBuffer ();
+                       int len = s.Length;
+                       
+                       // SecureString doesn't take endian-ness into account. 
+                       // Therefore swap bytes here before we send it to c-side if little-endian.
+                       if (BitConverter.IsLittleEndian) {
+                               for (int i = 0; i < buffer.Length; i += 2) {
+                                       byte b = buffer[i];
+                                       buffer[i] = buffer[i + 1];
+                                       buffer[i + 1] = b;
+                               }
                        }
-                       return (IntPtr) ((long)ctm + 4);
-               }
+                       return BufferToBSTR (buffer, len);
+        }
 
                public static IntPtr SecureStringToCoTaskMemAnsi (SecureString s)
                {
@@ -1143,11 +1221,9 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
 
-#if NET_4_5
                public static void StructureToPtr<T> (T structure, IntPtr ptr, bool fDeleteOld) {
                        StructureToPtr ((object)structure, ptr, fDeleteOld);
                }
-#endif
 
                public static void ThrowExceptionForHR (int errorCode) {
                        Exception ex = GetExceptionForHR (errorCode);
@@ -1161,14 +1237,16 @@ namespace System.Runtime.InteropServices
                                throw ex;
                }
 
+
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               public extern static IntPtr BufferToBSTR (Array ptr, int slen);
+
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr UnsafeAddrOfPinnedArrayElement (Array arr, int index);
 
-#if NET_4_5
                public static IntPtr UnsafeAddrOfPinnedArrayElement<T> (T[] arr, int index) {
                        return UnsafeAddrOfPinnedArrayElement ((Array)arr, index);
                }
-#endif
 
                public static void WriteByte (IntPtr ptr, byte val)
                {
@@ -1197,7 +1275,7 @@ namespace System.Runtime.InteropServices
                        if (((uint)addr & 1) == 0)
                                *(short*)addr = val;
                        else
-                               String.memcpy (addr, (byte*)&val, 2);
+                               Buffer.Memcpy (addr, (byte*)&val, 2);
                }
 
                public static unsafe void WriteInt16 (IntPtr ptr, int ofs, short val)
@@ -1207,7 +1285,7 @@ namespace System.Runtime.InteropServices
                        if (((uint)addr & 1) == 0)
                                *(short*)addr = val;
                        else {
-                               String.memcpy (addr, (byte*)&val, 2);
+                               Buffer.Memcpy (addr, (byte*)&val, 2);
                        }
                }
 
@@ -1241,7 +1319,7 @@ namespace System.Runtime.InteropServices
                        if (((uint)addr & 3) == 0) 
                                *(int*)addr = val;
                        else {
-                               String.memcpy (addr, (byte*)&val, 4);
+                               Buffer.Memcpy (addr, (byte*)&val, 4);
                        }
                }
 
@@ -1252,7 +1330,7 @@ namespace System.Runtime.InteropServices
                        if (((uint)addr & 3) == 0) 
                                *(int*)addr = val;
                        else {
-                               String.memcpy (addr, (byte*)&val, 4);
+                               Buffer.Memcpy (addr, (byte*)&val, 4);
                        }
                }
 
@@ -1272,7 +1350,7 @@ namespace System.Runtime.InteropServices
                        if (((uint)addr & 7) == 0) 
                                *(long*)addr = val;
                        else 
-                               String.memcpy (addr, (byte*)&val, 8);
+                               Buffer.Memcpy (addr, (byte*)&val, 8);
                }
 
                public static unsafe void WriteInt64 (IntPtr ptr, int ofs, long val)
@@ -1284,7 +1362,7 @@ namespace System.Runtime.InteropServices
                        if (((uint)addr & 7) == 0) 
                                *(long*)addr = val;
                        else 
-                               String.memcpy (addr, (byte*)&val, 8);
+                               Buffer.Memcpy (addr, (byte*)&val, 8);
                }
 
                [MonoTODO]
@@ -1377,10 +1455,10 @@ namespace System.Runtime.InteropServices
                        const int COR_E_TARGET = unchecked ((int)0x80131603L);
                        const int COR_E_TARGETINVOCATION = unchecked ((int)0x80131604L);
                        const int COR_E_TARGETPARAMCOUNT = unchecked ((int)0x8002000EL);
-                       const int COR_E_THREADABORTED = unchecked ((int)0x80131530L);
+                       //const int COR_E_THREADABORTED = unchecked ((int)0x80131530L);
                        const int COR_E_THREADINTERRUPTED = unchecked ((int)0x80131519L);
                        const int COR_E_THREADSTATE = unchecked ((int)0x80131520L);
-                       const int COR_E_THREADSTOP = unchecked ((int)0x80131521L);
+                       //const int COR_E_THREADSTOP = unchecked ((int)0x80131521L);
                        const int COR_E_TYPEINITIALIZATION = unchecked ((int)0x80131534L);
                        const int COR_E_VERIFICATION = unchecked ((int)0x8013150DL);
                        //const int COR_E_WEAKREFERENCE = unchecked ((int)?);
@@ -1529,6 +1607,7 @@ namespace System.Runtime.InteropServices
                        return null;
                }
 
+#if FEATURE_COMINTEROP
                [DllImport ("oleaut32.dll", CharSet=CharSet.Unicode, EntryPoint = "SetErrorInfo")]
                static extern int _SetErrorInfo (int dwReserved,
                        [MarshalAs(UnmanagedType.Interface)] IErrorInfo pIErrorInfo);
@@ -1577,7 +1656,7 @@ namespace System.Runtime.InteropServices
                        }
                        return retVal;
                }
-
+#endif
                public static Exception GetExceptionForHR (int errorCode)
                {
                        return GetExceptionForHR (errorCode, IntPtr.Zero);
@@ -1585,6 +1664,7 @@ namespace System.Runtime.InteropServices
 
                public static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo)
                {
+#if FEATURE_COMINTEROP
                        IErrorInfo info = null;
                        if (errorInfo != (IntPtr)(-1)) {
                                if (errorInfo == IntPtr.Zero) {
@@ -1596,7 +1676,7 @@ namespace System.Runtime.InteropServices
                                }
                        }
 
-                       if (info is ManagedErrorInfo && ((ManagedErrorInfo) info).Exception.hresult == errorCode) {
+                       if (info is ManagedErrorInfo && ((ManagedErrorInfo) info).Exception._HResult == errorCode) {
                                return ((ManagedErrorInfo) info).Exception;
                        }
 
@@ -1618,15 +1698,16 @@ namespace System.Runtime.InteropServices
                                }
                        }
                        return e;
+#else
+                       return ConvertHrToException (errorCode);
+#endif
                }
 
-#if !FULL_AOT_RUNTIME
                public static int FinalReleaseComObject (object o)
                {
                        while (ReleaseComObject (o) != 0);
                        return 0;
                }
-#endif
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private static extern Delegate GetDelegateForFunctionPointerInternal (IntPtr ptr, Type t);
@@ -1645,11 +1726,9 @@ namespace System.Runtime.InteropServices
                        return GetDelegateForFunctionPointerInternal (ptr, t);
                }
 
-#if NET_4_5
                public static TDelegate GetDelegateForFunctionPointer<TDelegate> (IntPtr ptr) {
                        return (TDelegate) (object) GetDelegateForFunctionPointer (ptr, typeof (TDelegate));
                }
-#endif
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private static extern IntPtr GetFunctionPointerForDelegateInternal (Delegate d);
@@ -1662,13 +1741,41 @@ namespace System.Runtime.InteropServices
                        return GetFunctionPointerForDelegateInternal (d);
                }
 
-#if NET_4_5
                public static IntPtr GetFunctionPointerForDelegate<TDelegate> (TDelegate d) {
                        if (d == null)
                                throw new ArgumentNullException ("d");
                        
                        return GetFunctionPointerForDelegateInternal ((Delegate)(object)d);
                }
-#endif
+
+               internal static void SetLastWin32Error (int error)
+               {
+               }
+
+               // Copied from referencesource/mscorlib/system/runtime/interopservices/marshal.cs
+               //====================================================================
+               // return the raw IUnknown* for a COM Object not related to current 
+               // context
+               // Does not call AddRef
+               //====================================================================
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               internal static extern IntPtr /* IUnknown* */ GetRawIUnknownForComObjectNoAddRef(Object o);
+               
+               // Copied from referencesource/mscorlib/system/runtime/interopservices/marshal.cs
+               //====================================================================
+               // Converts the CLR exception to an HRESULT. This function also sets
+               // up an IErrorInfo for the exception.
+               // This function is only used in WinRT and converts ObjectDisposedException
+               // to RO_E_CLOSED
+               //====================================================================
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               internal static extern int GetHRForException_WinRT(Exception e);
+
+               // Copied from referencesource/mscorlib/system/runtime/interopservices/marshal.cs
+               //========================================================================
+               // Create activation factory and wraps it with a unique RCW
+               //========================================================================
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               internal static extern object GetNativeActivationFactory(Type type);
        }
 }