Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / corlib / System.Runtime.InteropServices / Marshal.cs
index dc1c79736569f8f3e549d69247b14f2d8a3a5672..6a706c9da652471851b81cfcf11a7c2aedc2bd99 100644 (file)
@@ -30,7 +30,6 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using Mono.Interop;
 using System.Collections;
 using System.Runtime.CompilerServices;
 using System;
@@ -38,21 +37,18 @@ using System.Security;
 using System.Reflection;
 using System.Threading;
 
-#if NET_2_0
 using System.Runtime.ConstrainedExecution;
+#if !MOONLIGHT
 using System.Runtime.InteropServices.ComTypes;
 #endif
 
+#if !MOONLIGHT
+using Mono.Interop;
+#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
@@ -63,10 +59,6 @@ namespace System.Runtime.InteropServices
                        SystemDefaultCharSize = Environment.OSVersion.Platform == PlatformID.Win32NT ? 2 : 1;
                }
 
-#if !NET_2_0
-               private Marshal () {}
-#endif
-
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern static int AddRefInternal (IntPtr pUnk);
 
@@ -81,14 +73,10 @@ namespace System.Runtime.InteropServices
                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);
@@ -107,12 +95,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)
                {
@@ -149,12 +137,10 @@ namespace System.Runtime.InteropServices
                        copy_to_unmanaged (source, startIndex, destination, length);
                }
 
-#if NET_2_0
                public static void Copy (IntPtr[] source, int startIndex, IntPtr destination, int length)
                {
                        copy_to_unmanaged (source, startIndex, destination, length);
                }
-#endif
 
                public static void Copy (IntPtr source, byte[] destination, int startIndex, int length)
                {
@@ -191,7 +177,6 @@ namespace System.Runtime.InteropServices
                        copy_from_unmanaged (source, startIndex, destination, length);
                }
 
-#if NET_2_0
                public static void Copy (IntPtr source, IntPtr[] destination, int startIndex, int length)
                {
                        copy_from_unmanaged (source, startIndex, destination, length);
@@ -202,8 +187,8 @@ namespace System.Runtime.InteropServices
                {
                        throw new NotImplementedException ();
                }
-#endif
-               
+
+#if !MOONLIGHT
                public static object CreateWrapperOfType (object o, Type t)
                {
                        __ComObject co = o as __ComObject;
@@ -220,11 +205,10 @@ namespace System.Runtime.InteropServices
 
                        return ComInteropProxy.GetProxy (co.IUnknown, t).GetTransparentProxy ();
                }
+#endif
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-#if NET_2_0
                [ComVisible (true)]
-#endif
                public extern static void DestroyStructure (IntPtr ptr, Type structuretype);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
@@ -234,13 +218,9 @@ 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
-
                static void ClearBSTR (IntPtr ptr)
                {
                        int len = ReadInt32 (ptr, -4);
@@ -249,10 +229,10 @@ namespace System.Runtime.InteropServices
                                WriteByte (ptr, i, 0);
                }
                
-               public static void ZeroFreeBSTR (IntPtr ptr)
+               public static void ZeroFreeBSTR (IntPtr s)
                {
-                       ClearBSTR (ptr);
-                       FreeBSTR (ptr);
+                       ClearBSTR (s);
+                       FreeBSTR (s);
                }
 
                static void ClearAnsi (IntPtr ptr)
@@ -267,35 +247,34 @@ namespace System.Runtime.InteropServices
                                WriteInt16 (ptr, i, 0);
                }
                
-               public static void ZeroFreeCoTaskMemAnsi (IntPtr ptr)
+               public static void ZeroFreeCoTaskMemAnsi (IntPtr s)
                {
-                       ClearAnsi (ptr);
-                       FreeCoTaskMem (ptr);
+                       ClearAnsi (s);
+                       FreeCoTaskMem (s);
                }
 
-               public static void ZeroFreeCoTaskMemUnicode (IntPtr ptr)
+               public static void ZeroFreeCoTaskMemUnicode (IntPtr s)
                {
-                       ClearUnicode (ptr);
-                       FreeCoTaskMem (ptr);
+                       ClearUnicode (s);
+                       FreeCoTaskMem (s);
                }
 
-               public static void ZeroFreeGlobalAllocAnsi (IntPtr hglobal)
+               public static void ZeroFreeGlobalAllocAnsi (IntPtr s)
                {
-                       ClearAnsi (hglobal);
-                       FreeHGlobal (hglobal);
+                       ClearAnsi (s);
+                       FreeHGlobal (s);
                }
 
-               public static void ZeroFreeGlobalAllocUnicode (IntPtr hglobal)
+               public static void ZeroFreeGlobalAllocUnicode (IntPtr s)
                {
-                       ClearUnicode (hglobal);
-                       FreeHGlobal (hglobal);
+                       ClearUnicode (s);
+                       FreeHGlobal (s);
                }
-#endif
 
-               [MonoTODO]
+#if !MOONLIGHT
                public static Guid GenerateGuidForType (Type type)
                {
-                       throw new NotImplementedException ();
+                       return type.GUID;
                }
 
                [MonoTODO]
@@ -328,13 +307,11 @@ namespace System.Runtime.InteropServices
                        return pItf;
                }
 
-#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)
@@ -348,11 +325,11 @@ namespace System.Runtime.InteropServices
                public static int GetComSlotForMethodInfo (MemberInfo m)
                {
                        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);
                }
 
@@ -369,19 +346,20 @@ namespace System.Runtime.InteropServices
                }
 
                [MonoTODO]
-#if NET_2_0
                [ComVisible (true)]
-#endif
                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 // !NET_2_1
 
                [MonoTODO ("SetErrorInfo")]
                public static int GetHRForException (Exception e)
@@ -390,14 +368,12 @@ namespace System.Runtime.InteropServices
                }
 
                [MonoTODO]
-#if NET_2_0
                [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                public static int GetHRForLastWin32Error()
                {
                        throw new NotImplementedException ();
                }
-
+#if !MOONLIGHT
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern static IntPtr GetIDispatchForObjectInternal (object o);
 
@@ -409,13 +385,11 @@ namespace System.Runtime.InteropServices
                        return pUnk;
                }
 
-#if NET_2_0
                [MonoTODO]
                public static IntPtr GetIDispatchForObjectInContext (object o)
                {
                        throw new NotImplementedException ();
                }
-#endif
 
                [MonoTODO]
                public static IntPtr GetITypeInfoForType (Type t)
@@ -434,24 +408,14 @@ namespace System.Runtime.InteropServices
                        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]
-#if NET_2_0
                [Obsolete ("This method has been deprecated")]
-#endif
                public static IntPtr GetManagedThunkForUnmanagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
                {
                        throw new NotImplementedException ();
@@ -508,9 +472,7 @@ namespace System.Runtime.InteropServices
                }
 
                [MonoTODO]
-#if NET_2_0
                [Obsolete ("This method has been deprecated")]
-#endif
                public static Thread GetThreadFromFiberCookie (int cookie)
                {
                        throw new NotImplementedException ();
@@ -535,38 +497,30 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
                [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 ();
                }
-#endif
 
-#if NET_2_0
                [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)
@@ -574,33 +528,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)
                {
@@ -617,12 +564,9 @@ namespace System.Runtime.InteropServices
                {
                        throw new NotImplementedException ();
                }
-#endif
 
                [MonoTODO]
-#if NET_2_0
                [Obsolete ("This method has been deprecated")]
-#endif
                public static IntPtr GetUnmanagedThunkForManagedMethodPtr (IntPtr pfnMethodToWrap, IntPtr pbSignature, int cbSignature)
                {
                        throw new NotImplementedException ();
@@ -642,6 +586,11 @@ namespace System.Runtime.InteropServices
                {
                        throw new NotImplementedException ();
                }
+#endif // !NET_2_1
+
+               [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);
@@ -680,15 +629,11 @@ namespace System.Runtime.InteropServices
                public extern static string PtrToStringBSTR (IntPtr ptr);
                
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-#if NET_2_0
                [ComVisible (true)]
-#endif
                public extern static void PtrToStructure (IntPtr ptr, object structure);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-#if NET_2_0
                [ComVisible (true)]
-#endif
                public extern static object PtrToStructure (IntPtr ptr, Type structureType);
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
@@ -710,6 +655,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 ();
@@ -724,74 +670,58 @@ 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)
                {
@@ -804,15 +734,11 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr ReAllocHGlobal (IntPtr pv, IntPtr cb);
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern static int ReleaseInternal (IntPtr pUnk);
 
-#if NET_2_0
                [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                public static int Release (IntPtr pUnk)
                {
                        if (pUnk == IntPtr.Zero)
@@ -820,6 +746,7 @@ namespace System.Runtime.InteropServices
                        return ReleaseInternal (pUnk);
                }
 
+#if !MOONLIGHT
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                private extern static int ReleaseComObjectInternal (object co);
 
@@ -832,9 +759,7 @@ namespace System.Runtime.InteropServices
                        return ReleaseComObjectInternal (o);
                }
 
-#if NET_2_0
                [Obsolete]
-#endif
                [MonoTODO]
                public static void ReleaseThreadCache()
                {
@@ -846,10 +771,9 @@ namespace System.Runtime.InteropServices
                {
                        throw new NotSupportedException ("MSDN states user code should never need to call this method.");
                }
+#endif // !NET_2_1
 
-#if NET_2_0
                [ComVisible (true)]
-#endif
                public static int SizeOf (object structure)
                {
                        return SizeOf (structure.GetType ());
@@ -911,7 +835,7 @@ namespace System.Runtime.InteropServices
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr StringToHGlobalUni (string s);
 
-#if NET_2_0
+#if !MOONLIGHT
                public static IntPtr SecureStringToBSTR (SecureString s)
                {
                        if (s == null)
@@ -1001,10 +925,8 @@ namespace System.Runtime.InteropServices
                }
 #endif
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
                [ComVisible (true)]
-#endif
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static void StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld);
 
@@ -1032,6 +954,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 ();
@@ -1046,6 +969,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 ();
@@ -1075,6 +999,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 ();
@@ -1089,6 +1014,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 ();
@@ -1108,40 +1034,46 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-#if NET_2_0
-               public
-#else
-               internal
-#endif
-               static Exception GetExceptionForHR (int errorCode) {
+               public static Exception GetExceptionForHR (int errorCode) {
                        return GetExceptionForHR (errorCode, IntPtr.Zero);
                }
 
-#if NET_2_0
-               public
-#else
-               internal
-#endif
-               static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo) {
+               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;
                }
 
-#if NET_2_0
+#if !MOONLIGHT
                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);
 
                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");
 
@@ -1158,6 +1090,5 @@ namespace System.Runtime.InteropServices
                        
                        return GetFunctionPointerForDelegateInternal (d);
                }
-#endif
        }
 }