From a668de0a1f619947c6fe164d9408d2fdbda86dc8 Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Mon, 19 Apr 2021 19:57:28 -0700 Subject: [PATCH] Put Marshal.GetHINSTANCE under ifdef for Windows instead of COM (#51523) --- .../Runtime/InteropServices/Marshal.CoreCLR.cs | 4 ++-- src/coreclr/vm/ecalllist.h | 2 ++ .../System/Runtime/InteropServices/Marshal.NoCom.cs | 10 ---------- .../src/System/Runtime/InteropServices/Marshal.cs | 13 +++++++++++++ 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs index afacc85d34c86..2b02d135931e1 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs @@ -221,7 +221,7 @@ private static object PtrToStructureHelper(IntPtr ptr, Type structureType) [MethodImpl(MethodImplOptions.InternalCall)] internal static extern bool IsPinnable(object? obj); -#if FEATURE_COMINTEROP +#if TARGET_WINDOWS /// /// Returns the HInstance for this module. Returns -1 if the module doesn't have /// an HInstance. In Memory (Dynamic) Modules won't have an HInstance. @@ -244,7 +244,7 @@ public static IntPtr GetHINSTANCE(Module m) [DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)] private static extern IntPtr GetHINSTANCE(QCallModule m); -#endif // FEATURE_COMINTEROP +#endif // TARGET_WINDOWS [MethodImpl(MethodImplOptions.InternalCall)] diff --git a/src/coreclr/vm/ecalllist.h b/src/coreclr/vm/ecalllist.h index 9b84c7a8dd6b1..432dc7b176822 100644 --- a/src/coreclr/vm/ecalllist.h +++ b/src/coreclr/vm/ecalllist.h @@ -751,7 +751,9 @@ FCFuncStart(gInteropMarshalFuncs) FCFuncElement("IsPinnable", MarshalNative::IsPinnable) FCFuncElement("GetExceptionCode", ExceptionNative::GetExceptionCode) FCFuncElement("GetExceptionPointers", ExceptionNative::GetExceptionPointers) +#ifdef TARGET_WINDOWS QCFuncElement("GetHINSTANCE", COMModule::GetHINSTANCE) +#endif // TARGET_WINDOWS FCFuncElement("OffsetOfHelper", MarshalNative::OffsetOfHelper) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.NoCom.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.NoCom.cs index 450b660185096..0a1117a767590 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.NoCom.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.NoCom.cs @@ -87,16 +87,6 @@ public static IntPtr GetComInterfaceForObject([DisallowNull] T o) throw new PlatformNotSupportedException(SR.PlatformNotSupported_ComInterop); } - public static IntPtr GetHINSTANCE(Module m) - { - if (m is null) - { - throw new ArgumentNullException(nameof(m)); - } - - return (IntPtr)(-1); - } - [SupportedOSPlatform("windows")] public static IntPtr GetIDispatchForObject(object o) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs index 542f3ba41b19e..089a447da407f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs @@ -605,6 +605,19 @@ public static void PtrToStructure(IntPtr ptr, [DisallowNull] T structure) public static void DestroyStructure(IntPtr ptr) => DestroyStructure(ptr, typeof(T)); +// CoreCLR has a different implementation for Windows only +#if !CORECLR || !TARGET_WINDOWS + public static IntPtr GetHINSTANCE(Module m) + { + if (m is null) + { + throw new ArgumentNullException(nameof(m)); + } + + return (IntPtr)(-1); + } +#endif + /// /// Converts the HRESULT to a CLR exception. ///