From 1087bcaa1e1e63110ea5ef7eace171132c2ee686 Mon Sep 17 00:00:00 2001 From: Faizur Rahman Date: Mon, 24 Oct 2016 21:21:29 -0700 Subject: [PATCH] Add few Interop Marshal APIs (dotnet/coreclr#7742) Add the following interop Marshal APIs: M:System.Runtime.InteropServices.Marshal.GenerateGuidForType(System.Type) M:System.Runtime.InteropServices.Marshal.GenerateProgIdForType(System.Type) M:System.Runtime.InteropServices.Marshal.GetComObjectData(System.Object,System.Object) M:System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Module) M:System.Runtime.InteropServices.Marshal.GetIDispatchForObject(System.Object) M:System.Runtime.InteropServices.Marshal.GetTypedObjectForIUnknown(System.IntPtr,System.Type) M:System.Runtime.InteropServices.Marshal.PtrToStringAuto(System.IntPtr) M:System.Runtime.InteropServices.Marshal.SetComObjectData(System.Object,System.Object,System.Object) M:System.Runtime.InteropServices.Marshal.StringToCoTaskMemAuto(System.String) M:System.Runtime.InteropServices.Marshal.StringToHGlobalAuto(System.String) Commit migrated from https://github.com/dotnet/coreclr/commit/3103a70d1fbe733d2b4e7ed9e2ff795b888b8b79 --- src/coreclr/src/mscorlib/model.xml | 7 +++++ src/coreclr/src/mscorlib/ref/mscorlib.cs | 20 ++++++++++++++ .../src/System/Runtime/InteropServices/Marshal.cs | 31 +++++++++++++++++++++- .../System/Runtime/InteropServices/NonPortable.cs | 12 +++++++++ src/coreclr/src/vm/ecalllist.h | 4 +-- 5 files changed, 71 insertions(+), 3 deletions(-) diff --git a/src/coreclr/src/mscorlib/model.xml b/src/coreclr/src/mscorlib/model.xml index 178ed9d..c051360 100644 --- a/src/coreclr/src/mscorlib/model.xml +++ b/src/coreclr/src/mscorlib/model.xml @@ -6783,13 +6783,18 @@ + + + + + @@ -6825,12 +6830,14 @@ + + diff --git a/src/coreclr/src/mscorlib/ref/mscorlib.cs b/src/coreclr/src/mscorlib/ref/mscorlib.cs index 994a62f..6524f55 100644 --- a/src/coreclr/src/mscorlib/ref/mscorlib.cs +++ b/src/coreclr/src/mscorlib/ref/mscorlib.cs @@ -10802,11 +10802,17 @@ namespace System.Runtime.InteropServices [System.Security.SecurityCriticalAttribute] public static void FreeHGlobal(System.IntPtr hglobal) { } [System.Security.SecurityCriticalAttribute] + public static Guid GenerateGuidForType(System.Type type) { throw null; } + [System.Security.SecurityCritical] + public static string GenerateProgIdForType(System.Type type) { throw null; } + [System.Security.SecurityCriticalAttribute] public static System.IntPtr GetComInterfaceForObject(object o, System.Type T) { throw null; } [System.Security.SecurityCriticalAttribute] public static System.IntPtr GetComInterfaceForObject(object o, System.Type T, System.Runtime.InteropServices.CustomQueryInterfaceMode mode) { throw null; } [System.Security.SecurityCriticalAttribute] public static System.IntPtr GetComInterfaceForObject(T o) { throw null; } + [System.Security.SecurityCritical] + public static object GetComObjectData(object obj, object key) { throw null; } [System.Security.SecurityCriticalAttribute] public static System.Delegate GetDelegateForFunctionPointer(System.IntPtr ptr, System.Type t) { throw null; } [System.Security.SecurityCriticalAttribute] @@ -10821,10 +10827,14 @@ namespace System.Runtime.InteropServices public static System.IntPtr GetFunctionPointerForDelegate(System.Delegate d) { throw null; } [System.Security.SecurityCriticalAttribute] public static System.IntPtr GetFunctionPointerForDelegate(TDelegate d) { throw null; } + [System.Security.SecurityCritical] + public static System.IntPtr GetHINSTANCE(System.Reflection.Module m) { throw null; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)][System.Security.SecurityCriticalAttribute] public static int GetHRForException(System.Exception e) { throw null; } [System.Security.SecurityCriticalAttribute] public static int GetHRForLastWin32Error() { throw null; } + [System.Security.SecurityCritical] + public static System.IntPtr GetIDispatchForObject(object o) { throw null; } [System.Security.SecurityCriticalAttribute] public static System.IntPtr GetIUnknownForObject(object o) { throw null; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)][System.Security.SecurityCriticalAttribute] @@ -10845,6 +10855,8 @@ namespace System.Runtime.InteropServices public static T[] GetObjectsForNativeVariants(System.IntPtr aSrcNativeVariant, int cVars) { throw null; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)][System.Security.SecurityCriticalAttribute] public static int GetStartComSlot(System.Type t) { throw null; } + [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)][System.Security.SecurityCritical] + public static object GetTypedObjectForIUnknown(System.IntPtr pUnk, System.Type t) { throw null; } public static System.Type GetTypeFromCLSID(System.Guid clsid) { throw null; } [System.Security.SecurityCriticalAttribute] public static string GetTypeInfoName(System.Runtime.InteropServices.ComTypes.ITypeInfo typeInfo) { throw null; } @@ -10861,6 +10873,8 @@ namespace System.Runtime.InteropServices public static string PtrToStringAnsi(System.IntPtr ptr) { throw null; } [System.Security.SecurityCriticalAttribute] public static string PtrToStringAnsi(System.IntPtr ptr, int len) { throw null; } + [System.Security.SecurityCritical] + public static string PtrToStringAuto(System.IntPtr ptr) { throw null; } [System.Security.SecurityCriticalAttribute] public static string PtrToStringBSTR(System.IntPtr ptr) { throw null; } [System.Security.SecurityCriticalAttribute] @@ -10921,6 +10935,8 @@ namespace System.Runtime.InteropServices public static int Release(System.IntPtr pUnk) { throw null; } [System.Security.SecurityCriticalAttribute] public static int ReleaseComObject(object o) { throw null; } + [System.Security.SecurityCritical] + public static bool SetComObjectData(object obj, object key, object data) { throw null; } [System.Runtime.InteropServices.ComVisibleAttribute(true)] public static int SizeOf(object structure) { throw null; } public static int SizeOf(System.Type t) { throw null; } @@ -10930,12 +10946,16 @@ namespace System.Runtime.InteropServices public static System.IntPtr StringToBSTR(string s) { throw null; } [System.Security.SecurityCriticalAttribute] public static System.IntPtr StringToCoTaskMemAnsi(string s) { throw null; } + [System.Security.SecurityCritical] + public static System.IntPtr StringToCoTaskMemAuto(string s) { throw null; } [System.Security.SecurityCriticalAttribute] public static System.IntPtr StringToCoTaskMemUni(string s) { throw null; } [System.Security.SecurityCriticalAttribute] public static System.IntPtr StringToCoTaskMemUTF8(string s) { throw null; } [System.Security.SecurityCriticalAttribute] public static System.IntPtr StringToHGlobalAnsi(string s) { throw null; } + [System.Security.SecurityCritical] + public static System.IntPtr StringToHGlobalAuto(string s) { throw null; } [System.Security.SecurityCriticalAttribute] public static System.IntPtr StringToHGlobalUni(string s) { throw null; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)][System.Runtime.InteropServices.ComVisibleAttribute(true)] diff --git a/src/coreclr/src/mscorlib/src/System/Runtime/InteropServices/Marshal.cs b/src/coreclr/src/mscorlib/src/System/Runtime/InteropServices/Marshal.cs index 86e8830..7497c24 100644 --- a/src/coreclr/src/mscorlib/src/System/Runtime/InteropServices/Marshal.cs +++ b/src/coreclr/src/mscorlib/src/System/Runtime/InteropServices/Marshal.cs @@ -1072,6 +1072,7 @@ namespace System.Runtime.InteropServices DestroyStructure(ptr, typeof(T)); } +#if FEATURE_COMINTEROP //==================================================================== // Returns the HInstance for this module. Returns -1 if the module // doesn't have an HInstance. In Memory (Dynamic) Modules won't have @@ -1103,6 +1104,7 @@ namespace System.Runtime.InteropServices [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity] private extern static IntPtr GetHINSTANCE(RuntimeModule m); +#endif // FEATURE_COMINTEROP //==================================================================== // Throws a CLR exception based on the HRESULT. //==================================================================== @@ -1737,6 +1739,7 @@ namespace System.Runtime.InteropServices //==================================================================== [MethodImplAttribute(MethodImplOptions.InternalCall)] internal static extern IntPtr /* IUnknown* */ GetRawIUnknownForComObjectNoAddRef(Object o); +#endif // FEATURE_COMINTEROP //==================================================================== // return the IDispatch* for an Object @@ -1744,9 +1747,14 @@ namespace System.Runtime.InteropServices [System.Security.SecurityCritical] // auto-generated_required public static IntPtr /* IDispatch */ GetIDispatchForObject(Object o) { +#if FEATURE_CORECLR + throw new PlatformNotSupportedException(); +#else return GetIDispatchForObjectNative(o, false); +#endif // FEATURE_CORECLR } - + +#if FEATURE_COMINTEROP //==================================================================== // return the IDispatch* for an Object if the current context // is the one where the RCW was first seen. Will return null @@ -2083,6 +2091,7 @@ namespace System.Runtime.InteropServices [MethodImplAttribute(MethodImplOptions.InternalCall)] internal static extern void InternalFinalReleaseComObject(Object o); +#endif // FEATURE_COMINTEROP //==================================================================== // This method retrieves data from the COM object. @@ -2090,6 +2099,9 @@ namespace System.Runtime.InteropServices [System.Security.SecurityCritical] // auto-generated_required public static Object GetComObjectData(Object obj, Object key) { +#if FEATURE_CORECLR + throw new PlatformNotSupportedException(); +#else // Validate that the arguments aren't null. if (obj == null) throw new ArgumentNullException("obj"); @@ -2116,6 +2128,7 @@ namespace System.Runtime.InteropServices // Retrieve the data from the __ComObject. return comObj.GetData(key); +#endif // FEATURE_CORECLR } //==================================================================== @@ -2127,6 +2140,9 @@ namespace System.Runtime.InteropServices [System.Security.SecurityCritical] // auto-generated_required public static bool SetComObjectData(Object obj, Object key, Object data) { +#if FEATURE_CORECLR + throw new PlatformNotSupportedException(); +#else // Validate that the arguments aren't null. The data can validly be null. if (obj == null) throw new ArgumentNullException("obj"); @@ -2153,8 +2169,10 @@ namespace System.Runtime.InteropServices // Retrieve the data from the __ComObject. return comObj.SetData(key, data); +#endif // FEATURE_CORECLR } +#if FEATURE_COMINTEROP //==================================================================== // This method takes the given COM object and wraps it in an object // of the specified type. The type must be derived from __ComObject. @@ -2340,6 +2358,7 @@ namespace System.Runtime.InteropServices [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern int InternalGetComSlotForMethodInfo(IRuntimeMethodInfo m); +#endif // FEATURE_COMINTEROP //==================================================================== // This method generates a GUID for the specified type. If the type @@ -2350,6 +2369,10 @@ namespace System.Runtime.InteropServices [System.Security.SecurityCritical] // auto-generated_required public static Guid GenerateGuidForType(Type type) { +#if FEATURE_CORECLR + throw new PlatformNotSupportedException(); + } +#else Guid result = new Guid (); FCallGenerateGuidForType (ref result, type); return result; @@ -2358,6 +2381,7 @@ namespace System.Runtime.InteropServices // The full assembly name is used to compute the GUID, so this should be SxS-safe [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern void FCallGenerateGuidForType(ref Guid result, Type type); +#endif // FEATURE_CORECLR //==================================================================== // This method generates a PROGID for the specified type. If the type @@ -2368,6 +2392,9 @@ namespace System.Runtime.InteropServices [System.Security.SecurityCritical] // auto-generated_required public static String GenerateProgIdForType(Type type) { +#if FEATURE_CORECLR + throw new PlatformNotSupportedException(); +#else if (type == null) throw new ArgumentNullException("type"); if (type.IsImport) @@ -2402,8 +2429,10 @@ namespace System.Runtime.InteropServices // If there is no prog ID attribute then use the full name of the type as the prog id. return type.FullName; +#endif // FEATURE_CORECLR } +#if FEATURE_COMINTEROP //==================================================================== // This method binds to the specified moniker. //==================================================================== diff --git a/src/coreclr/src/mscorlib/src/System/Runtime/InteropServices/NonPortable.cs b/src/coreclr/src/mscorlib/src/System/Runtime/InteropServices/NonPortable.cs index 408f56c..e9ea076 100644 --- a/src/coreclr/src/mscorlib/src/System/Runtime/InteropServices/NonPortable.cs +++ b/src/coreclr/src/mscorlib/src/System/Runtime/InteropServices/NonPortable.cs @@ -31,6 +31,18 @@ namespace System.Runtime.InteropServices { throw new PlatformNotSupportedException(); } + + [System.Security.SecurityCriticalAttribute] + public static Object GetTypedObjectForIUnknown(System.IntPtr pUnk, System.Type t) + { + throw new PlatformNotSupportedException(); + } + + [System.Security.SecurityCritical] // auto-generated_required + public static System.IntPtr GetHINSTANCE(System.Reflection.Module m) + { + return (System.IntPtr) (-1); + } [System.Security.SecurityCriticalAttribute] public static System.IntPtr CreateAggregatedObject(System.IntPtr pOuter, T o) diff --git a/src/coreclr/src/vm/ecalllist.h b/src/coreclr/src/vm/ecalllist.h index 935d717..5c8834e 100644 --- a/src/coreclr/src/vm/ecalllist.h +++ b/src/coreclr/src/vm/ecalllist.h @@ -1603,10 +1603,10 @@ FCFuncStart(gInteropMarshalFuncs) FCFuncElement("DestroyStructure", MarshalNative::DestroyStructure) FCFuncElement("UnsafeAddrOfPinnedArrayElement", MarshalNative::FCUnsafeAddrOfPinnedArrayElement) FCFuncElement("GetExceptionCode", ExceptionNative::GetExceptionCode) + QCFuncElement("GetHINSTANCE", COMModule::GetHINSTANCE) #ifndef FEATURE_CORECLR QCFuncElement("InternalNumParamBytes", MarshalNative::NumParamBytes) FCFuncElement("GetExceptionPointers", ExceptionNative::GetExceptionPointers) - QCFuncElement("GetHINSTANCE", COMModule::GetHINSTANCE) FCFuncElement("GetUnmanagedThunkForManagedMethodPtr", MarshalNative::GetUnmanagedThunkForManagedMethodPtr) FCFuncElement("GetManagedThunkForUnmanagedMethodPtr", MarshalNative::GetManagedThunkForUnmanagedMethodPtr) FCFuncElement("InternalGetThreadFromFiberCookie", MarshalNative::GetThreadFromFiberCookie) @@ -1651,10 +1651,10 @@ FCFuncStart(gInteropMarshalFuncs) FCFuncElement("InternalReleaseComObject", MarshalNative::ReleaseComObject) FCFuncElement("Release", MarshalNative::Release) FCFuncElement("InitializeWrapperForWinRT", MarshalNative::InitializeWrapperForWinRT) + FCFuncElement("GetTypedObjectForIUnknown", MarshalNative::GetTypedObjectForIUnknown) #ifndef FEATURE_CORECLR FCFuncElement("GetLoadedTypeForGUID", MarshalNative::GetLoadedTypeForGUID) FCFuncElement("GetITypeInfoForType", MarshalNative::GetITypeInfoForType) - FCFuncElement("GetTypedObjectForIUnknown", MarshalNative::GetTypedObjectForIUnknown) FCFuncElement("CleanupUnusedObjectsInCurrentContext", MarshalNative::CleanupUnusedObjectsInCurrentContext) FCFuncElement("IsTypeVisibleFromCom", MarshalNative::IsTypeVisibleFromCom) FCFuncElement("FCallGenerateGuidForType", MarshalNative::DoGenerateGuidForType) -- 2.7.4