Description
Background and motivation
As mentioned in #7267, there is a strong desire for enabling using the "__Internal" name in a DllImport to point to methods in the hosting executable module of the process in both CoreCLR and Mono. Mono has had this support for many years, but CoreCLR doesn't have built-in support, and we don't have a desire to provide built-in support for this "hidden" name.
However, we understand the need for a way to resolve this name to the correct handle. We propose adding an API that gets the native module handle of the entry-point module.
API Proposal
namespace System.Runtime.InteropServices
{
public static class NativeLibrary
{
public static IntPtr GetMainProgramHandle();
}
}
Alternatively, we considered allowing null
as a parameter to NativeLibrary.Load(string)
to match how the implementation on non-Windows is dlopen(NULL)
. The interop team decided that design was an abstraction leak and undesirable since the Windows implementation doesn't follow the same model. Additionally, the other NativeLibrary.Load
overloads have additional behaviors and aren't thin wrappers, which makes the null
option less desirable.
API Usage
NativeLibrary.SetDllImportResolver(assembly, (name, _, paths) =>
{
if (name == "__Internal")
{
return NativeLibrary.GetMainProgramHandle();
}
return IntPtr.Zero;
});
Risks
None