[英]How to deploy a C# library that uses C++/CLI libraries
我有一个使用AnyCPU构建的C#库,但是对一些C ++ / CLI库有依赖性。 我已经为x86和x64 Windows编译了C ++ / CLI。 看来我只能将C ++ / CLI库的单个引用添加到C#项目中(否则文件相互覆盖)。 我认为可能会有一个x86和x64文件夹,分别位于相应的库中。但是当我尝试这样做时,我会遇到无法找到该库的运行时异常。
有没有办法在我的AnyCpu库中同时包含x86和x64,以便在部署它时,调用应用程序可以决定是否要x86或x64?
基本上,您需要执行以下操作:
获取库的路径以根据流程体系结构进行加载:
public NativeLibraryLoader(string path32, string path64)
{
if (!File.Exists(path32))
throw new FileNotFoundException("32-bit library not found.", path32);
if (!File.Exists(path64))
throw new FileNotFoundException("64-bit library not found.", path64);
string path;
switch (RuntimeInformation.ProcessArchitecture)
{
case Architecture.X86:
path = path32;
break;
case Architecture.X64:
path = path64;
break;
default:
throw new PlatformNotSupportedException();
}
...
}
用LoadLibrary
加载本地库:
/// <summary>
/// https://msdn.microsoft.com/en-us/library/windows/desktop/ms684175(v=vs.85).aspx
/// </summary>
/// <param name="lpLibFileName"></param>
/// <returns></returns>
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr LoadLibrary(string lpLibFileName);
完整的例子:
您可以查看aubio.net ,这是我为aubio编写的.NET包装。 这是一个AnyCPU
程序集,它将根据运行的当前体系结构加载x86
或x64
库。
这些是您感兴趣的部分:
https://github.com/aybe/aubio.net/tree/master/Aubio/Interop
https://github.com/aybe/aubio.net/blob/master/Aubio/AubioNative.cs
警告:
此方法说明了如何加载本机库而不是托管库。
正如@ Flydog57指出的那样,要加载托管程序集,请改用Assembly.Load
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.