Merge pull request #487 from mayerwin/patch-1
[mono.git] / mcs / class / corlib / System.Runtime.InteropServices / Marshal.cs
index 8411cd94763b863792d944ac1ad66adeddae37f0..4cfb7c2b5ebe0b0e7beab6047b350981997e03d5 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using Mono.Interop;
+using System.Collections;
+using System.Collections.Generic;
 using System.Runtime.CompilerServices;
 using System;
 using System.Security;
 using System.Reflection;
 using System.Threading;
 
-#if NET_2_0
 using System.Runtime.ConstrainedExecution;
+#if !MOONLIGHT
+#if !FULL_AOT_RUNTIME
 using System.Runtime.InteropServices.ComTypes;
+using Mono.Interop;
+#endif
 #endif
 
 namespace System.Runtime.InteropServices
 {
-       [SuppressUnmanagedCodeSecurity ()]
-       public
-#if NET_2_0
-       static
-#else
-       sealed
-#endif
-       class Marshal
+       public static class Marshal
        {
                /* fields */
                public static readonly int SystemMaxDBCSCharSize = 2; // don't know what this is
-               public static readonly int SystemDefaultCharSize;
-
-               static Marshal ()
-               {
-                       SystemDefaultCharSize = Environment.OSVersion.Platform == PlatformID.Win32NT ? 2 : 1;
-               }
+               public static readonly int SystemDefaultCharSize = Environment.OSVersion.Platform == PlatformID.Win32NT ? 2 : 1;
 
-#if !NET_2_0
-               private Marshal () {}
+#if !MOBILE
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               private extern static int AddRefInternal (IntPtr pUnk);
 #endif
 
-               [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               public extern static int AddRef (IntPtr pUnk);
+               public static int AddRef (IntPtr pUnk)
+               {
+#if !MOBILE
+                       if (pUnk == IntPtr.Zero)
+                               throw new ArgumentException ("Value cannot be null.", "pUnk");
+                       return AddRefInternal (pUnk);
+#else
+                       throw new NotImplementedException ();
+#endif
+               }
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr AllocCoTaskMem (int cb);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
-#endif
                public extern static IntPtr AllocHGlobal (IntPtr cb);
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
-#endif
                public static IntPtr AllocHGlobal (int cb)
                {
                        return AllocHGlobal ((IntPtr)cb);
@@ -99,12 +96,12 @@ namespace System.Runtime.InteropServices
                }
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               extern static void copy_to_unmanaged (Array source, int startIndex,
-                                                     IntPtr destination, int length);
+               internal extern static void copy_to_unmanaged (Array source, int startIndex,
+                                                              IntPtr destination, int length);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               extern static void copy_from_unmanaged (IntPtr source, int startIndex,
-                                                       Array destination, int length);
+               internal extern static void copy_from_unmanaged (IntPtr source, int startIndex,
+                                                                Array destination, int length);
 
                public static void Copy (byte[] source, int startIndex, IntPtr destination, int length)
                {
@@ -141,6 +138,11 @@ namespace System.Runtime.InteropServices
                        copy_to_unmanaged (source, startIndex, destination, length);
                }
 
+               public static void Copy (IntPtr[] source, int startIndex, IntPtr destination, int length)
+               {
+                       copy_to_unmanaged (source, startIndex, destination, length);
+               }
+
                public static void Copy (IntPtr source, byte[] destination, int startIndex, int length)
                {
                        copy_from_unmanaged (source, startIndex, destination, length);
@@ -176,6 +178,18 @@ namespace System.Runtime.InteropServices
                        copy_from_unmanaged (source, startIndex, destination, length);
                }
 
+               public static void Copy (IntPtr source, IntPtr[] destination, int startIndex, int length)
+               {
+                       copy_from_unmanaged (source, startIndex, destination, length);
+               }
+
+               public static IntPtr CreateAggregatedObject (IntPtr pOuter,
+                                                            object o)
+               {
+                       throw new NotImplementedException ();
+               }
+
+#if !FULL_AOT_RUNTIME && !MOONLIGHT
                public static object CreateWrapperOfType (object o, Type t)
                {
                        __ComObject co = o as __ComObject;
@@ -192,8 +206,10 @@ namespace System.Runtime.InteropServices
 
                        return ComInteropProxy.GetProxy (co.IUnknown, t).GetTransparentProxy ();
                }
+#endif
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               [ComVisible (true)]
                public extern static void DestroyStructure (IntPtr ptr, Type structuretype);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
@@ -203,53 +219,86 @@ namespace System.Runtime.InteropServices
                public extern static void FreeCoTaskMem (IntPtr ptr);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                public extern static void FreeHGlobal (IntPtr hglobal);
 
-#if NET_2_0
-               [MonoTODO]
-               public static void ZeroFreeBSTR (IntPtr ptr)
+               static void ClearBSTR (IntPtr ptr)
                {
-                       throw new NotImplementedException ();
+                       int len = ReadInt32 (ptr, -4);
+
+                       for (int i = 0; i < len; i++)
+                               WriteByte (ptr, i, 0);
+               }
+               
+               public static void ZeroFreeBSTR (IntPtr s)
+               {
+                       ClearBSTR (s);
+                       FreeBSTR (s);
                }
 
-               [MonoTODO]
-               public static void ZeroFreeCoTaskMemAnsi (IntPtr ptr)
+               static void ClearAnsi (IntPtr ptr)
                {
-                       throw new NotImplementedException ();
+                       for (int i = 0; ReadByte (ptr, i) != 0; i++)
+                               WriteByte (ptr, i, 0);
                }
 
-               [MonoTODO]
-               public static void ZeroFreeCoTaskMemUnicode (IntPtr ptr)
+               static void ClearUnicode (IntPtr ptr)
                {
-                       throw new NotImplementedException ();
+                       for (int i = 0; ReadInt16 (ptr, i) != 0; i += 2)
+                               WriteInt16 (ptr, i, 0);
+               }
+               
+               public static void ZeroFreeCoTaskMemAnsi (IntPtr s)
+               {
+                       ClearAnsi (s);
+                       FreeCoTaskMem (s);
                }
 
-               [MonoTODO]
-               public static void ZeroFreeGlobalAllocAnsi (IntPtr hglobal)
+               public static void ZeroFreeCoTaskMemUnicode (IntPtr s)
                {
-                       throw new NotImplementedException ();
+                       ClearUnicode (s);
+                       FreeCoTaskMem (s);
                }
 
-               [MonoTODO]
-               public static void ZeroFreeGlobalAllocUnicode (IntPtr hglobal)
+               public static void ZeroFreeGlobalAllocAnsi (IntPtr s)
                {
-                       throw new NotImplementedException ();
+                       ClearAnsi (s);
+                       FreeHGlobal (s);
                }
-#endif
 
-               [MonoTODO]
+               public static void ZeroFreeGlobalAllocUnicode (IntPtr s)
+               {
+                       ClearUnicode (s);
+                       FreeHGlobal (s);
+               }
+
+#if !FULL_AOT_RUNTIME && !MOONLIGHT
                public static Guid GenerateGuidForType (Type type)
                {
-                       throw new NotImplementedException ();
+                       return type.GUID;
                }
 
-               [MonoTODO]
                public static string GenerateProgIdForType (Type type)
                {
-                       throw new NotImplementedException ();
+                       IList<CustomAttributeData> attrs = CustomAttributeData.GetCustomAttributes (type);
+
+                       foreach (var a in attrs)
+                       {
+                               var dt = a.Constructor.DeclaringType;
+                               string name = dt.Name;
+                               if (name == "ProgIdAttribute")
+                               {
+                                       var args = a.ConstructorArguments;
+                                       string text = a.ConstructorArguments[0].Value as string;
+                                       if (text == null)
+                                       {
+                                               text = string.Empty;
+                                       }
+                                       return text;
+                               }
+                       }
+
+                       return type.FullName;
                }
 
                [MonoTODO]
@@ -258,52 +307,60 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
+#if !MOBILE
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               private extern static IntPtr GetCCW (object o, Type T);
+
+               private static IntPtr GetComInterfaceForObjectInternal (object o, Type T)
+               {
+                       if (IsComObject (o))
+                               return ((__ComObject)o).GetInterface (T);
+                       else
+                               return GetCCW (o, T);
+               }
+#endif
+
                public static IntPtr GetComInterfaceForObject (object o, Type T)
                {
-                       __ComObject co = o as __ComObject;
-                       if (co == null)
-                               throw new NotSupportedException ("Only RCWs are currently supported");
-                       
-                       IntPtr pUnk = co.GetInterface (T);
-                       AddRef (pUnk);
-                       return pUnk;
+#if !MOBILE
+                       IntPtr pItf = GetComInterfaceForObjectInternal (o, T);
+                       AddRef (pItf);
+                       return pItf;
+#else
+                       throw new NotImplementedException ();
+#endif
                }
 
-#if NET_2_0
                [MonoTODO]
                public static IntPtr GetComInterfaceForObjectInContext (object o, Type t)
                {
                        throw new NotImplementedException ();
                }
-#endif
 
+               [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
                public static object GetComObjectData (object obj, object key)
                {
-                       if (obj == null)
-                               throw new ArgumentNullException ("obj");
-                       if (key == null)
-                               throw new ArgumentNullException ("key");
-
-                       __ComObject com_object = obj as __ComObject;
-                       if (com_object == null)
-                               throw new ArgumentException ("obj is not a COM object", "obj");
-
-                       return com_object.Hashtable[key];
+                       throw new NotSupportedException ("MSDN states user code should never need to call this method.");
                }
 
+#if !MOBILE
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private extern static int GetComSlotForMethodInfoInternal (MemberInfo m);
+#endif
 
                public static int GetComSlotForMethodInfo (MemberInfo m)
                {
+#if !MOBILE
                        if (m == null)
-                               throw new ArgumentNullException("m", "Value cannot be null.");
+                               throw new ArgumentNullException ("m");
                        if (!(m is MethodInfo))
-                               throw new ArgumentException("m", "The MemberInfo must be an interface method.");
+                               throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
                        if (!m.DeclaringType.IsInterface)
-                               throw new ArgumentException("m", "The MemberInfo must be an interface method.");
+                               throw new ArgumentException ("The MemberInfo must be an interface method.", "m");
                        return GetComSlotForMethodInfoInternal (m);
+#else
+                       throw new NotImplementedException ();
+#endif
                }
 
                [MonoTODO]
@@ -319,17 +376,22 @@ namespace System.Runtime.InteropServices
                }
 
                [MonoTODO]
+               [ComVisible (true)]
                public static IntPtr GetExceptionPointers()
                {
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
                public static IntPtr GetHINSTANCE (Module m)
                {
-                       throw new NotImplementedException ();
+                       if (m == null)
+                               throw new ArgumentNullException ("m");
+
+                       return m.GetHINSTANCE ();
                }
+#endif // !MOONLIGHT
 
+#if !FULL_AOT_RUNTIME
                [MonoTODO ("SetErrorInfo")]
                public static int GetHRForException (Exception e)
                {
@@ -337,31 +399,28 @@ namespace System.Runtime.InteropServices
                }
 
                [MonoTODO]
+               [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
                public static int GetHRForLastWin32Error()
                {
                        throw new NotImplementedException ();
                }
+#if !MOONLIGHT
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               private extern static IntPtr GetIDispatchForObjectInternal (object o);
 
-               [MonoTODO]
                public static IntPtr GetIDispatchForObject (object o)
                {
-                       // only handle case of RCW objects for now
-                       __ComObject co = o as __ComObject;
-                       if (co != null) {
-                               IntPtr pUnk = co.IDispatch;
-                               AddRef (pUnk);
-                               return pUnk;
-                       }
-                       throw new NotImplementedException ();
+                       IntPtr pUnk = GetIDispatchForObjectInternal (o);
+                       // Internal method does not AddRef
+                       AddRef (pUnk);
+                       return pUnk;
                }
 
-#if NET_2_0
                [MonoTODO]
                public static IntPtr GetIDispatchForObjectInContext (object o)
                {
                        throw new NotImplementedException ();
                }
-#endif
 
                [MonoTODO]
                public static IntPtr GetITypeInfoForType (Type t)
@@ -369,34 +428,25 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               private extern static IntPtr GetIUnknownForObjectInternal (object o);
+
                public static IntPtr GetIUnknownForObject (object o)
                {
-                       // only handle case of RCW objects for now
-                       __ComObject co = o as __ComObject;
-                       if (co != null) {
-                               IntPtr pUnk = co.IUnknown;
-                               AddRef (pUnk);
-                               return pUnk;
-                       }
-                       throw new NotImplementedException ();
+                       IntPtr pUnk = GetIUnknownForObjectInternal (o);
+                       // Internal method does not AddRef
+                       AddRef (pUnk);
+                       return pUnk;
                }
 
-#if NET_2_0
                [MonoTODO]
                public static IntPtr GetIUnknownForObjectInContext (object o)
                {
                        throw new NotImplementedException ();
                }
-#endif
-
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-#if NET_2_0
-               [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
-               public static extern int GetLastWin32Error();
 
                [MonoTODO]
+               [Obsolete ("This method has been deprecated")]
                public static IntPtr GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
                {
                        throw new NotImplementedException ();
@@ -415,10 +465,24 @@ namespace System.Runtime.InteropServices
                        Marshal.StructureToPtr(vt, pDstNativeVariant, false);
                }
 
+#if !MOBILE
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               private static extern object GetObjectForCCW (IntPtr pUnk);
+#endif
+
                public static object GetObjectForIUnknown (IntPtr pUnk)
                {
-                       ComInteropProxy proxy = ComInteropProxy.GetProxy (pUnk, typeof(__ComObject));
-                       return proxy.GetTransparentProxy ();
+#if !MOBILE
+                       object obj = GetObjectForCCW (pUnk);
+                       // was not a CCW
+                       if (obj == null) {
+                               ComInteropProxy proxy = ComInteropProxy.GetProxy (pUnk, typeof (__ComObject));
+                               obj = proxy.GetTransparentProxy ();
+                       }
+                       return obj;
+#else
+                       throw new NotImplementedException ();
+#endif
                }
 
                public static object GetObjectForNativeVariant (IntPtr pSrcNativeVariant)
@@ -427,10 +491,15 @@ namespace System.Runtime.InteropServices
                        return vt.GetValue();
                }
 
-               [MonoTODO]
                public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars)
                {
-                       throw new NotImplementedException ();
+                       if (cVars < 0)
+                               throw new ArgumentOutOfRangeException ("cVars", "cVars cannot be a negative number.");
+                       object[] objects = new object[cVars];
+                       for (int i = 0; i < cVars; i++)
+                               objects[i] = GetObjectForNativeVariant ((IntPtr)(aSrcNativeVariant.ToInt64 () +
+                                       i * SizeOf (typeof(Variant))));
+                       return objects;
                }
 
                [MonoTODO]
@@ -440,6 +509,7 @@ namespace System.Runtime.InteropServices
                }
 
                [MonoTODO]
+               [Obsolete ("This method has been deprecated")]
                public static Thread GetThreadFromFiberCookie (int cookie)
                {
                        throw new NotImplementedException ();
@@ -464,31 +534,31 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
+#if !FULL_AOT_RUNTIME
                [Obsolete]
-#endif
                [MonoTODO]
                public static string GetTypeInfoName (UCOMITypeInfo pTI)
                {
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
+               public static string GetTypeInfoName (ITypeInfo typeInfo)
+               {
+                       throw new NotImplementedException ();
+               }
+
                [Obsolete]
-#endif
                [MonoTODO]
                public static Guid GetTypeLibGuid (UCOMITypeLib pTLB)
                {
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
                [MonoTODO]
                public static Guid GetTypeLibGuid (ITypeLib typelib)
                {
                        throw new NotImplementedException ();
                }
-#endif
 
                [MonoTODO]
                public static Guid GetTypeLibGuidForAssembly (Assembly asm)
@@ -496,33 +566,26 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
                [Obsolete]
-#endif
                [MonoTODO]
                public static int GetTypeLibLcid (UCOMITypeLib pTLB)
                {
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
                [MonoTODO]
                public static int GetTypeLibLcid (ITypeLib typelib)
                {
                        throw new NotImplementedException ();
                }
-#endif
 
-#if NET_2_0
                [Obsolete]
-#endif
                [MonoTODO]
                public static string GetTypeLibName (UCOMITypeLib pTLB)
                {
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
                [MonoTODO]
                public static string GetTypeLibName (ITypeLib typelib)
                {
@@ -534,20 +597,29 @@ 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)
                {
                        throw new NotImplementedException ();
                }
 
+#if !MOBILE
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               public extern static bool IsComObject (object o);
+#else
                public static bool IsComObject (object o)
                {
-                       Type t = o.GetType ();
-                       object[] attrs = t.GetCustomAttributes (typeof (ComImportAttribute), true);
-                       return (attrs != null && attrs.Length > 0);
+                       throw new NotImplementedException ();
                }
+#endif         
 
                [MonoTODO]
                public static bool IsTypeVisibleFromCom (Type t)
@@ -560,6 +632,12 @@ namespace System.Runtime.InteropServices
                {
                        throw new NotImplementedException ();
                }
+#endif // !NET_2_1
+#endif
+
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
+               public static extern int GetLastWin32Error();
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr OffsetOf (Type t, string fieldName);
@@ -594,17 +672,39 @@ 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)]
                public extern static void PtrToStructure (IntPtr ptr, object structure);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               [ComVisible (true)]
                public extern static object PtrToStructure (IntPtr ptr, Type structureType);
 
+#if !MOBILE
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               public extern static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv);
+               private extern static int QueryInterfaceInternal (IntPtr pUnk, ref Guid iid, out IntPtr ppv);
+#endif
+
+               public static int QueryInterface (IntPtr pUnk, ref Guid iid, out IntPtr ppv)
+               {
+#if !MOBILE
+                       if (pUnk == IntPtr.Zero)
+                               throw new ArgumentException ("Value cannot be null.", "pUnk");
+                       return QueryInterfaceInternal (pUnk, ref iid, out ppv);
+#else
+                       throw new NotImplementedException ();
+#endif
+               }
 
                public static byte ReadByte (IntPtr ptr)
                {
@@ -615,6 +715,7 @@ namespace System.Runtime.InteropServices
                public extern static byte ReadByte (IntPtr ptr, int ofs);
 
                [MonoTODO]
+               [SuppressUnmanagedCodeSecurity]
                public static byte ReadByte ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
                {
                        throw new NotImplementedException ();
@@ -629,129 +730,119 @@ namespace System.Runtime.InteropServices
                public extern static short ReadInt16 (IntPtr ptr, int ofs);
 
                [MonoTODO]
+               [SuppressUnmanagedCodeSecurity]
                public static short ReadInt16 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
                {
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                public static int ReadInt32 (IntPtr ptr)
                {
                        return ReadInt32 (ptr, 0);
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static int ReadInt32 (IntPtr ptr, int ofs);
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                [MonoTODO]
+               [SuppressUnmanagedCodeSecurity]
                public static int ReadInt32 ([In, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
                {
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                public static long ReadInt64 (IntPtr ptr)
                {
                        return ReadInt64 (ptr, 0);
                }
 
-#if NET_2_0
-               [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static long ReadInt64 (IntPtr ptr, int ofs);
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                [MonoTODO]
+               [SuppressUnmanagedCodeSecurity]
                public static long ReadInt64 ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
                {
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                public static IntPtr ReadIntPtr (IntPtr ptr)
                {
                        return ReadIntPtr (ptr, 0);
                }
                
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr ReadIntPtr (IntPtr ptr, int ofs);
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                [MonoTODO]
                public static IntPtr ReadIntPtr ([In, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs)
                {
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
-               public static IntPtr ReAllocCoTaskMem (IntPtr pv, int cb)
-               {
-                       throw new NotImplementedException ();
-               }
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               public extern static IntPtr ReAllocCoTaskMem (IntPtr pv, int cb);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
 
-#if NET_2_0
+#if !MOBILE
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               private extern static int ReleaseInternal (IntPtr pUnk);
+#endif
+
+               [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
+               public static int Release (IntPtr pUnk)
+               {
+#if !MOBILE
+                       if (pUnk == IntPtr.Zero)
+                               throw new ArgumentException ("Value cannot be null.", "pUnk");
+
+                       return ReleaseInternal (pUnk);
+#else
+                       throw new NotImplementedException ();
 #endif
+               }
+
+#if !FULL_AOT_RUNTIME
+#if !MOONLIGHT
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               public extern static int Release (IntPtr pUnk);
+               private extern static int ReleaseComObjectInternal (object co);
 
                public static int ReleaseComObject (object o)
                {
                        if (o == null)
-                               throw new ArgumentException ("o");
-                       __ComObject co = o as __ComObject;
-                       if (co == null)
-                               throw new ArgumentException ("o");
-                       return ComInteropProxy.ReleaseComObject (co);
+                               throw new ArgumentException ("Value cannot be null.", "o");
+                       if (!IsComObject (o))
+                               throw new ArgumentException ("Value must be a Com object.", "o");
+                       return ReleaseComObjectInternal (o);
                }
 
-#if NET_2_0
                [Obsolete]
-#endif
                [MonoTODO]
                public static void ReleaseThreadCache()
                {
                        throw new NotImplementedException ();
                }
 
+               [MonoNotSupportedAttribute ("MSDN states user code should never need to call this method.")]
                public static bool SetComObjectData (object obj, object key, object data)
                {
-                       if (obj == null)
-                               throw new ArgumentNullException ("obj");
-                       if (key == null)
-                               throw new ArgumentNullException ("key");
-
-                       __ComObject com_object = obj as __ComObject;
-                       if (com_object == null)
-                               throw new ArgumentException ("obj is not a COM object", "obj");
-
-                       com_object.Hashtable[key] = data;
-                       return true;
+                       throw new NotSupportedException ("MSDN states user code should never need to call this method.");
                }
+#endif // !NET_2_1
+#endif
 
+               [ComVisible (true)]
                public static int SizeOf (object structure)
                {
                        return SizeOf (structure.GetType ());
@@ -784,7 +875,8 @@ namespace System.Runtime.InteropServices
 
                public static IntPtr StringToCoTaskMemAuto (string s)
                {
-                       return StringToCoTaskMemAnsi (s);
+                       return SystemDefaultCharSize == 2
+                               ? StringToCoTaskMemUni (s) : StringToCoTaskMemAnsi (s);
                }
 
                public static IntPtr StringToCoTaskMemUni (string s)
@@ -812,64 +904,111 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr StringToHGlobalUni (string s);
 
-#if NET_2_0
-               [MonoTODO]
+#if !MOONLIGHT
                public static IntPtr SecureStringToBSTR (SecureString s)
                {
                        if (s == null)
                                throw new ArgumentNullException ("s");
-                       throw new NotSupportedException ();
+                       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;
+                                       }
+                       }
+                       return (IntPtr) ((long)ctm + 4);
                }
 
-               [MonoTODO]
                public static IntPtr SecureStringToCoTaskMemAnsi (SecureString s)
                {
                        if (s == null)
                                throw new ArgumentNullException ("s");
-                       throw new NotSupportedException ();
+                       int len = s.Length;
+                       IntPtr ctm = AllocCoTaskMem (len + 1);
+                       byte [] copy = new byte [len+1];
+
+                       try {
+                               byte [] buffer = s.GetBuffer ();
+                               int i = 0, j = 0;
+                               for (; i < len; i++, j += 2){
+                                       copy [i] = buffer [j+1];
+                                       buffer [j] = 0;
+                                       buffer [j+1] = 0;
+                               }
+                               copy [i] = 0;
+                               copy_to_unmanaged (copy, 0, ctm, len+1);
+                       } finally {
+                               // Ensure that we clear the buffer.
+                               for (int i = len; i > 0; ){
+                                       i--;
+                                       copy [i] = 0;
+                               }
+                       }
+                       return ctm;
                }
 
-               [MonoTODO]
                public static IntPtr SecureStringToCoTaskMemUnicode (SecureString s)
                {
                        if (s == null)
                                throw new ArgumentNullException ("s");
-                       throw new NotSupportedException ();
+                       int len = s.Length;
+                       IntPtr ctm = AllocCoTaskMem (len * 2 + 2);
+                       byte [] buffer = null;
+                       try {
+                               buffer = s.GetBuffer ();
+                               for (int i = 0; i < len; i++)
+                                       WriteInt16 (ctm, i * 2, (short) ((buffer [(i*2)] << 8) | (buffer [i*2+1])));
+                               WriteInt16 (ctm, buffer.Length, 0);
+                       } finally {
+                               if (buffer != null)
+                                       for (int i = buffer.Length; i > 0; ){
+                                               i--;
+                                               buffer [i] = 0;
+                                       }
+                       }
+                       return ctm;
                }
 
-               [MonoTODO]
                public static IntPtr SecureStringToGlobalAllocAnsi (SecureString s)
                {
                        if (s == null)
                                throw new ArgumentNullException ("s");
-                       throw new NotSupportedException ();
+                       return SecureStringToCoTaskMemAnsi (s);
                }
 
-               [MonoTODO]
                public static IntPtr SecureStringToGlobalAllocUnicode (SecureString s)
                {
                        if (s == null)
                                throw new ArgumentNullException ("s");
-                       throw new NotSupportedException ();
+                       return SecureStringToCoTaskMemUnicode (s);
                }
 #endif
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
-#endif
+               [ComVisible (true)]
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
 
-               public static void ThrowExceptionForHR (int errorCode)
-               {
-                       if (errorCode < 0)
-                               throw new COMException ("", errorCode);
+               public static void ThrowExceptionForHR (int errorCode) {
+                       Exception ex = GetExceptionForHR (errorCode);
+                       if (ex != null)
+                               throw ex;
                }
 
-               [MonoTODO]
-               public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo)
-               {
-                       throw new NotImplementedException ();
+               public static void ThrowExceptionForHR (int errorCode, IntPtr errorInfo) {
+                       Exception ex = GetExceptionForHR (errorCode, errorInfo);
+                       if (ex != null)
+                               throw ex;
                }
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
@@ -884,6 +1023,7 @@ namespace System.Runtime.InteropServices
                public extern static void WriteByte (IntPtr ptr, int ofs, byte val);
 
                [MonoTODO]
+               [SuppressUnmanagedCodeSecurity]
                public static void WriteByte ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, byte val)
                {
                        throw new NotImplementedException ();
@@ -898,6 +1038,7 @@ namespace System.Runtime.InteropServices
                public extern static void WriteInt16 (IntPtr ptr, int ofs, short val);
 
                [MonoTODO]
+               [SuppressUnmanagedCodeSecurity]
                public static void WriteInt16 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, short val)
                {
                        throw new NotImplementedException ();
@@ -927,6 +1068,7 @@ namespace System.Runtime.InteropServices
                public extern static void WriteInt32 (IntPtr ptr, int ofs, int val);
 
                [MonoTODO]
+               [SuppressUnmanagedCodeSecurity]
                public static void WriteInt32([In, Out, MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs, int val)
                {
                        throw new NotImplementedException ();
@@ -941,6 +1083,7 @@ namespace System.Runtime.InteropServices
                public extern static void WriteInt64 (IntPtr ptr, int ofs, long val);
 
                [MonoTODO]
+               [SuppressUnmanagedCodeSecurity]
                public static void WriteInt64 ([In, Out, MarshalAs (UnmanagedType.AsAny)] object ptr, int ofs, long val)
                {
                        throw new NotImplementedException ();
@@ -960,32 +1103,48 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
-               [MonoTODO]
-               public static int FinalReleaseComObject (object o)
-               {
-                       throw new NotImplementedException ();
+               public static Exception GetExceptionForHR (int errorCode) {
+                       return GetExceptionForHR (errorCode, IntPtr.Zero);
                }
 
-               [MonoTODO]
-               public static Exception GetExceptionForHR (int errorCode)
-               {
-                       throw new NotImplementedException ();
+               public static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo) {
+
+                       const int E_OUTOFMEMORY = unchecked ((int)0x8007000EL);
+                       const int E_INVALIDARG = unchecked ((int)0X80070057);
+                       
+                       switch (errorCode)
+                       {
+                       case E_OUTOFMEMORY:
+                               return new OutOfMemoryException ();
+                       case E_INVALIDARG:
+                               return new ArgumentException ();
+                       }
+                       if (errorCode < 0)
+                               return new COMException ("", errorCode);
+                       return null;
                }
 
-               [MonoTODO]
-               public static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo)
+#if !FULL_AOT_RUNTIME
+#if !MOONLIGHT
+               public static int FinalReleaseComObject (object o)
                {
-                       throw new NotImplementedException ();
+                       while (ReleaseComObject (o) != 0);
+                       return 0;
                }
+#endif
+#endif
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private static extern Delegate GetDelegateForFunctionPointerInternal (IntPtr ptr, Type t);
 
                public static Delegate GetDelegateForFunctionPointer (IntPtr ptr, Type t)
                {
+                       if (t == null)
+                               throw new ArgumentNullException ("t");
                        if (!t.IsSubclassOf (typeof (MulticastDelegate)) || (t == typeof (MulticastDelegate)))
                                throw new ArgumentException ("Type is not a delegate", "t");
+                       if (t.IsGenericType)
+                               throw new ArgumentException ("The specified Type must not be a generic type definition.");
                        if (ptr == IntPtr.Zero)
                                throw new ArgumentNullException ("ptr");
 
@@ -1002,6 +1161,5 @@ namespace System.Runtime.InteropServices
                        
                        return GetFunctionPointerForDelegateInternal (d);
                }
-#endif
        }
 }