【问题标题】:Load native library from application's runtimes directory in .NET 5 application从 .NET 5 应用程序中的应用程序运行时目录加载本机库
【发布时间】:2021-03-03 22:03:24
【问题描述】:

我在runtimes 目录中分布有 .NET 5 应用程序本机依赖项,​​如下所示:

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

从应用程序app.exe 我需要在运行时加载本机plugin.dll/so(使用NativeLibrary.Load 方法)。现在我需要构建候选路径以根据运行时标识符查找正确的本机库。我可以从System.Runtime.InteropServices.RuntimeInformation.RuntimeIdentifier 获取应用程序的当前 RID。但是它返回例如win7-x64 但本机库位于 win-x64。在理想情况下,我需要枚举所有compatible RIDs 并尝试从它们中的每一个加载库,从最具体到最少,直到找到库。有没有像公共 API 这样的机制?我知道这适用于 P/Invoke,但是我需要在运行时动态地执行此操作。我不想解析runtime.json.deps.json。有更好的主意吗?

【问题讨论】:

  • 它认为你解决了安装包的问题,​​而不是在运行时。安装包应该能识别机器类型,然后安装正确的dll。

标签: c# .net-core dll


【解决方案1】:

因此必须使用高级 API 方法NativeLibrary.Load(string libraryName, Assembly assembly, DllImportSearchPath? searchPath),它已经处理了运行时路径中的变化。低级方法NativeLibrary.Load(string libraryPath) 没有。

【讨论】:

猜你喜欢
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
  • 2010-12-13
  • 2013-07-14
相关资源
最近更新 更多