Skip to content

API: Native Library Resolve Event #27647

Closed
@swaroop-sridhar

Description

@swaroop-sridhar

This proposal adds a Native library resolving event, to be raised when the runtime cannot resolve a native library load.

Proposed API

namespace System.Runtime.Loader
{
    public abstract class AssemblyLoadContext
    {
        /// Event handler for resolving native libraries
        /// Inputs: Invoking assembly, and library name to resolve
        /// Returns: A handle to the loaded native library
        public event Func<Assembly, string, IntPtr> ResolvingUnmanagedDll;
    }
}

Rationale

In the case of Loading assemblies, the runtime provides the application to customize the load at various phases:

This proposal creates a matching facility for unmanaged libraries

In particular, the NativeLibraryResolve event is expected to provide flexibility for running custom native library resolution logic from plugins. Hence the motivation to have the event per AssemblyLoadContext, rather than globally.

DllImport sequence

DllImport load library works in the following order, stop at any step the library is successfully loaded.

  • If the invoking-assembly has a DllImportResolver callback registered, invoke it.
  • If the invoking-assembly is not in the default load context, call AssemblyLoadContext.LoadUnmanagedDll()
  • Run the default load logic, try loading from:
    • AppDomain cache
    • NATIVE_DLL_SEARCH_DIRECTORIES
    • Invoking-assembly directory, System32, etc. based on DllImportSearchPaths
  • Raise the ResolvingUnmanagedDll event

Discussion

Alternate notation

In this proposal ResolvingUnmanagedDll uses Func notation in order to be consistent with the
Loading/Unloading events in AssemblyLoadContext.

An alternative is to use EventHandler style recommended by these guidelines, and similar to certain other events in the same class.

namespace System
{
    public delegate IntPtr UnmanagedDllResolveEventHandler(object sender, ResolveEventArgs args);
}

namespace System.Runtime.Loader
{
    public abstract class AssemblyLoadContext
    {
        public event UnmanagedDllResolveEventHandler ResolvingUnmanagedDll;
    }
}

Related Topics

dotnet/corefx#32015 Native Library Loader API

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions