UnmanagedDllResolveHelper 1.1.0
dotnet add package UnmanagedDllResolveHelper --version 1.1.0
NuGet\Install-Package UnmanagedDllResolveHelper -Version 1.1.0
<PackageReference Include="UnmanagedDllResolveHelper" Version="1.1.0" />
<PackageVersion Include="UnmanagedDllResolveHelper" Version="1.1.0" />
<PackageReference Include="UnmanagedDllResolveHelper" />
paket add UnmanagedDllResolveHelper --version 1.1.0
#r "nuget: UnmanagedDllResolveHelper, 1.1.0"
#:package UnmanagedDllResolveHelper@1.1.0
#addin nuget:?package=UnmanagedDllResolveHelper&version=1.1.0
#tool nuget:?package=UnmanagedDllResolveHelper&version=1.1.0
UnmanagedDllResolveHelper
UnmanagedDllResolveHelper is a .NET helper library for resolving unmanaged (native) library dependencies at runtime, based on the location of the calling native library.
This is especially useful for self-contained native plugins or AOT scenarios where dependent libraries are placed alongside the main native library.
Features
- Resolves dependent native libraries from the same directory as the calling library
- Supports Windows, Linux, and macOS
- No configuration or search path setup required
- Simple integration with AssemblyLoadContext
How to Use
1. Add Reference
Add a reference to the UnmanagedDllResolveHelper
project or NuGet package in your native library project.
2. Register the Resolver
In your native library's static constructor, register the resolver to AssemblyLoadContext.Default.ResolvingUnmanagedDll
:
using System.Runtime.Loader;
static Class1()
{
AssemblyLoadContext.Default.ResolvingUnmanagedDll += UnmanagedDllResolveHelper.UnmanagedDllCurrentLibraryLocationResolver.ResolveUnmanagedDll;
}
3. Use P/Invoke as Usual
Declare your P/Invoke as usual. For example:
[DllImport("SampleNativeLibraryDependency", EntryPoint = "Sum42", CallingConvention = CallingConvention.StdCall)]
public static extern int Sum42(int a);
4. Build and Place Native Libraries
Build your native libraries and place any dependencies in the same directory as the main native library.
For example, if your main library is SampleNativeLibraryWithLoader.dll
and it depends on SampleNativeLibraryDependency.dll
, place both in the same output folder.
5. Example
public static class Class1
{
static Class1()
{
AssemblyLoadContext.Default.ResolvingUnmanagedDll += UnmanagedDllResolveHelper.UnmanagedDllCurrentLibraryLocationResolver.ResolveUnmanagedDll;
}
[DllImport("SampleNativeLibraryDependency", EntryPoint = "Sum42", CallingConvention = CallingConvention.StdCall)]
public static extern int Sum42(int a);
[UnmanagedCallersOnly(EntryPoint = "Run", CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvStdcall) })]
public static int Run(int a)
{
return Sum42(a);
}
}
Testing
See UnmanagedDllResolveHelper.Tests/UnitTest1.cs
for a dynamic loading example using NativeLibrary.Load
and function pointer binding.
Supported Platforms
- Windows (.dll)
- Linux (.so)
- macOS (.dylib)
License
MIT License
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.