From 6b766cde5a05efe008b869913730e8265caa9746 Mon Sep 17 00:00:00 2001 From: Vitek Karas <10670590+vitek-karas@users.noreply.github.com> Date: Fri, 1 Jul 2022 20:52:19 -0700 Subject: [PATCH] Add `RequiresAssemblyFiles` onto `Marshal.GetHINSTANCE`. (#71531) * Add `RequiresAssemblyFiles` onto `Marshal.GetHINSTANCE`. The `GetHINSTANCE` only works on modules/assemblies which are loaded from a file on disk (must be loaded via `LoadLibrary` really), in single-file like scenarios lot of assemblies are loaded directly from memory. For such assemblies the API returns -1 which is not a valid handle. * Add the attribute to ref assembly * Add it on the non-implemented version --- .../src/System/Runtime/InteropServices/Marshal.CoreCLR.cs | 2 ++ .../src/System/Runtime/InteropServices/Marshal.cs | 2 ++ .../ref/System.Runtime.InteropServices.cs | 1 + 3 files changed, 5 insertions(+) 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 4ee3f10c2d40d..927504b3bdc18 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 @@ -268,6 +268,8 @@ internal static unsafe bool IsPinnable(object? obj) /// 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. /// + [RequiresAssemblyFiles("Windows only assigns HINSTANCE to assemblies loaded from disk. " + + "This API will return -1 for modules without a file on disk.")] public static IntPtr GetHINSTANCE(Module m) { ArgumentNullException.ThrowIfNull(m); 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 c95c068a679f6..267cc8e3baa40 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 @@ -625,6 +625,8 @@ public static void PtrToStructure(IntPtr ptr, [DisallowNull] T structure) // CoreCLR has a different implementation for Windows only #if !CORECLR || !TARGET_WINDOWS + [RequiresAssemblyFiles("Windows only assigns HINSTANCE to assemblies loaded from disk. " + + "This API will return -1 for modules without a file on disk.")] public static IntPtr GetHINSTANCE(Module m) { ArgumentNullException.ThrowIfNull(m); diff --git a/src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs b/src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs index 36f42feb6ec58..0a3c15e195581 100644 --- a/src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs +++ b/src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs @@ -576,6 +576,7 @@ public static void FreeHGlobal(System.IntPtr hglobal) { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public static System.IntPtr GetFunctionPointerForDelegate(System.Delegate d) { throw null; } public static System.IntPtr GetFunctionPointerForDelegate(TDelegate d) where TDelegate : notnull { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresAssemblyFiles("Windows only assigns HINSTANCE to assemblies loaded from disk. This API will return -1 for modules without a file on disk.")] public static System.IntPtr GetHINSTANCE(System.Reflection.Module m) { throw null; } public static int GetHRForException(System.Exception? e) { throw null; } public static int GetHRForLastWin32Error() { throw null; }