Unmanaged (native) library loading algorithm
Unmanaged libraries are located and loaded with an algorithm involving various stages.
The following algorithm describes how native libraries are loaded through
PInvoke load library algorithm
PInvoke uses the following algorithm when attempting to load an unmanaged assembly:
activeAssemblyLoadContext. For an unmanaged load library, the
activeAssemblyLoadContext is the caller of the
activeAssemblyLoadContext, try to find the assembly in priority order by:
Checking its cache.
Calling the current System.Runtime.InteropServices.DllImportResolver delegate set by the NativeLibrary.SetDllImportResolver(Assembly, DllImportResolver) function.
Calling the AssemblyLoadContext.LoadUnmanagedDll function.
Raising the AssemblyLoadContext.ResolvingUnmanagedDll event for the
If the unmanaged library is newly loaded, the AppDomain.AssemblyLoad event is raised.