Merge pull request #1222 from LogosBible/uri-trycreate
[mono.git] / mcs / class / corlib / System.Runtime.InteropServices / Marshal.cs
index 95df86a7c8f0bf6a13854cc50d73a28be57654b9..1b650da569d25bda48ab02c01ab63dadb89c4d9e 100644 (file)
@@ -187,11 +187,9 @@ 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)
@@ -211,22 +209,18 @@ namespace System.Runtime.InteropServices
                        return ComInteropProxy.GetProxy (co.IUnknown, t).GetTransparentProxy ();
                }
 
-#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);
@@ -347,11 +341,9 @@ namespace System.Runtime.InteropServices
 #endif
                }
 
-#if NET_4_5
                public static IntPtr GetComInterfaceForObject<T, TInterface> (T o) {
                        return GetComInterfaceForObject ((object)o, typeof (T));
                }
-#endif                 
 
                [MonoTODO]
                public static IntPtr GetComInterfaceForObjectInContext (object o, Type t)
@@ -416,10 +408,14 @@ namespace System.Runtime.InteropServices
 #if !FULL_AOT_RUNTIME
                public static int GetHRForException (Exception e)
                {
+#if FEATURE_COMINTEROP
                        var errorInfo = new ManagedErrorInfo(e);
                        SetErrorInfo (0, errorInfo);
 
                        return e.hresult;
+#else                  
+                       return -1;
+#endif
                }
 
                [MonoTODO]
@@ -489,11 +485,9 @@ namespace System.Runtime.InteropServices
                        Marshal.StructureToPtr(vt, pDstNativeVariant, false);
                }
 
-#if NET_4_5
                public static void GetNativeVariantForObject<T> (T obj, IntPtr pDstNativeVariant) {
                        GetNativeVariantForObject ((object)obj, pDstNativeVariant);
                }
-#endif
 
 #if !MOBILE
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
@@ -521,12 +515,10 @@ namespace System.Runtime.InteropServices
                        return vt.GetValue();
                }
 
-#if NET_4_5
                public static T GetObjectForNativeVariant<T> (IntPtr pSrcNativeVariant) {
                        Variant vt = (Variant)Marshal.PtrToStructure(pSrcNativeVariant, typeof(Variant));
                        return (T)vt.GetValue();
                }
-#endif
 
                public static object[] GetObjectsForNativeVariants (IntPtr aSrcNativeVariant, int cVars)
                {
@@ -539,7 +531,6 @@ namespace System.Runtime.InteropServices
                        return objects;
                }
 
-#if NET_4_5
                public static T[] GetObjectsForNativeVariants<T> (IntPtr aSrcNativeVariant, int cVars) {
                        if (cVars < 0)
                                throw new ArgumentOutOfRangeException ("cVars", "cVars cannot be a negative number.");
@@ -549,7 +540,6 @@ namespace System.Runtime.InteropServices
                                        i * SizeOf (typeof(Variant))));
                        return objects;
                }
-#endif
 
                [MonoTODO]
                public static int GetStartComSlot (Type t)
@@ -583,7 +573,12 @@ namespace System.Runtime.InteropServices
                        throw new NotImplementedException ();
                }
 
-#if !FULL_AOT_RUNTIME
+               public static Type GetTypeFromCLSID (Guid clsid)
+               {
+                       throw new NotImplementedException ();                   
+               }
+
+#if !MOBILE
                [Obsolete]
                [MonoTODO]
                public static string GetTypeInfoName (UCOMITypeInfo pTI)
@@ -690,11 +685,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);
@@ -744,7 +737,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);
                }
@@ -752,7 +744,6 @@ namespace System.Runtime.InteropServices
                public static T PtrToStructure<T> (IntPtr ptr) {
                        return (T) PtrToStructure (ptr, typeof (T));
                }
-#endif
 
 #if !MOBILE
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
@@ -801,7 +792,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;
                }
 
@@ -813,7 +804,7 @@ namespace System.Runtime.InteropServices
                                return *(short*)addr;
 
                        short s;
-                       String.memcpy ((byte*)&s, addr, 2);
+                       Buffer.Memcpy ((byte*)&s, addr, 2);
                        return s;
                }
 
@@ -833,7 +824,7 @@ namespace System.Runtime.InteropServices
                                return *(int*)addr;
 
                        int s;
-                       String.memcpy ((byte*)&s, addr, 4);
+                       Buffer.Memcpy ((byte*)&s, addr, 4);
                        return s;
                }
 
@@ -846,7 +837,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;
                        }
                }
@@ -870,7 +861,7 @@ namespace System.Runtime.InteropServices
                                return *(long*)ptr;
 
                        long s;
-                       String.memcpy ((byte*)&s, addr, 8);
+                       Buffer.Memcpy ((byte*)&s, addr, 8);
                        return s;
                }
 
@@ -882,7 +873,7 @@ namespace System.Runtime.InteropServices
                                return *(long*)addr;
                        
                        long s;
-                       String.memcpy ((byte*)&s, addr, 8);
+                       Buffer.Memcpy ((byte*)&s, addr, 8);
                        return s;
                }
 
@@ -980,7 +971,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));
                }
@@ -988,7 +978,6 @@ namespace System.Runtime.InteropServices
                public static int SizeOf<T> (T structure) {
                        return SizeOf (structure.GetType ());
                }
-#endif
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                public extern static IntPtr StringToBSTR (string s);
@@ -1136,11 +1125,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);
@@ -1157,11 +1144,9 @@ namespace System.Runtime.InteropServices
                [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)
                {
@@ -1190,7 +1175,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)
@@ -1200,7 +1185,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);
                        }
                }
 
@@ -1234,7 +1219,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);
                        }
                }
 
@@ -1245,7 +1230,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);
                        }
                }
 
@@ -1265,7 +1250,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)
@@ -1277,7 +1262,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]
@@ -1522,6 +1507,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);
@@ -1570,39 +1556,40 @@ namespace System.Runtime.InteropServices
                        }
                        return retVal;
                }
-
+#endif
                public static Exception GetExceptionForHR (int errorCode)
                {
                        return GetExceptionForHR (errorCode, IntPtr.Zero);
                }
 
-               public static Exception GetExceptionForHR (int errorCode, IntPtr errorInfoPtr)
+               public static Exception GetExceptionForHR (int errorCode, IntPtr errorInfo)
                {
-                       IErrorInfo errorInfo = null;
-                       if (errorInfoPtr != (IntPtr)(-1)) {
-                               if (errorInfoPtr == IntPtr.Zero) {
-                                       if (GetErrorInfo (0, out errorInfo) != 0) {
-                                               errorInfo = null;
+#if FEATURE_COMINTEROP
+                       IErrorInfo info = null;
+                       if (errorInfo != (IntPtr)(-1)) {
+                               if (errorInfo == IntPtr.Zero) {
+                                       if (GetErrorInfo (0, out info) != 0) {
+                                               info  = null;
                                        }
                                } else {
-                                       errorInfo = Marshal.GetObjectForIUnknown (errorInfoPtr) as IErrorInfo;
+                                       info  = Marshal.GetObjectForIUnknown (errorInfo) as IErrorInfo;
                                }
                        }
 
-                       if (errorInfo is ManagedErrorInfo && ((ManagedErrorInfo)errorInfo).Exception.hresult == errorCode) {
-                               return ((ManagedErrorInfo)errorInfo).Exception;
+                       if (info is ManagedErrorInfo && ((ManagedErrorInfo) info).Exception.hresult == errorCode) {
+                               return ((ManagedErrorInfo) info).Exception;
                        }
 
                        Exception e = ConvertHrToException (errorCode);
-                       if (errorInfo != null && e != null) {
+                       if (info != null && e != null) {
                                uint helpContext;
-                               errorInfo.GetHelpContext (out helpContext);
+                               info.GetHelpContext (out helpContext);
                                string str;
-                               errorInfo.GetSource (out str);
+                               info.GetSource (out str);
                                e.Source = str;
-                               errorInfo.GetDescription (out str);
+                               info.GetDescription (out str);
                                e.SetMessage (str);
-                               errorInfo.GetHelpFile (out str);
+                               info.GetHelpFile (out str);
 
                                if (helpContext == 0) {
                                        e.HelpLink = str;
@@ -1611,6 +1598,9 @@ namespace System.Runtime.InteropServices
                                }
                        }
                        return e;
+#else
+                       return ConvertHrToException (errorCode);
+#endif
                }
 
 #if !FULL_AOT_RUNTIME
@@ -1638,11 +1628,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);
@@ -1655,13 +1643,11 @@ 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
        }
 }