2

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?

manison
  • 649
  • 6
  • 17
  • It think you solve issue with installation package and not at runtime. The installation package should recognize the type of machine and then install the correct dll's. – jdweng Nov 20 '20 at 13:33

1 Answers1

2

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.

manison
  • 649
  • 6
  • 17
  • This answer helped me: https://stackoverflow.com/a/45384763/1121437 in terms of the searchPath parameter. – J Mills Feb 27 '21 at 12:45