Skip to content

Commit

Permalink
Add RequiresAssemblyFiles onto Marshal.GetHINSTANCE. (#71531)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
vitek-karas committed Jul 2, 2022
1 parent 70cf817 commit 6b766cd
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
[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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,8 @@ public static void PtrToStructure<T>(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>(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; }
Expand Down

0 comments on commit 6b766cd

Please sign in to comment.