manison
manison

Reputation: 659

Load native library from application's runtimes directory in .NET 5 application

I have .NET 5 application native dependencies distributed in runtimes directory as follows:

runtimes/
├── win-x64/
│   └── native/
│       └── plugin.dll
├── win-x86/
│   └── native/
│       └── plugin.dll
├── linux-x64/
│   └── native/
│       └── plugin.so
⋮
└── app.exe

From the application app.exe I need to load the native plugin.dll/so at runtime (using the NativeLibrary.Load method). Now I need to build candidate paths to look for the right native library based on the runtime identifier. I can get application's current RID from System.Runtime.InteropServices.RuntimeInformation.RuntimeIdentifier. However it returns e.g. win7-x64 but the native library is located in win-x64. In ideal case I would need to enumerate all compatible RIDs and try to load the library from each of them from the most specific to the least until I find the library. Is there such mechanism available as public API? I know this works with P/Invoke however I need to do this dynamically at runtime. I don't want to parse runtime.json or .deps.json. Any better idea?

Upvotes: 2

Views: 3370

Answers (1)

manison
manison

Reputation: 659

So one must use the high-level API method NativeLibrary.Load(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) which already handles the variations in runtime paths. The low-level method NativeLibrary.Load(string libraryPath) does not.

Upvotes: 2

Related Questions