And*_*ijo 8 .net c# c++ .net-core asp.net-core
我们有一个用C ++编写的库。为了使其与我们更现代的.NET项目更加兼容,我们将此C ++库包装在另一个.NET项目中。从完整的.NET Framework项目(4.5、4.6等)中引用它时,它工作正常。
我正在使用.NET Core 2.1创建一个新应用程序,并且试图引用此“ .NET C ++库”。在我的第一次尝试中,它失败说无法加载程序集。我通过安装.NET Core SDK x86并强制我的应用程序使用x86(而不是任何CPU)来解决此问题。
我没有任何构建错误,但是当我尝试实例化该库中的类时,出现以下异常:
<CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load.
---> System.EntryPointNotFoundException: A library name must be specified in a DllImport attribute applied to non-IJW methods.
at _getFiberPtrId()
at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* )
at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
--- End of inner exception stack trace ---
at <CrtImplementationDetails>.ThrowModuleLoadException(String errorMessage, Exception innerException)
at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
at .cctor()
Run Code Online (Sandbox Code Playgroud)
.NET Core 2.1完全支持这种情况吗?
正如其他人指出的那样,.NET Core 当前不支持C ++ / CLI(也称为“托管C ++”)。如果要在.NET Core中调用本机程序集,则必须使用PInvoke(如您所见)。
您还可以在AnyCPU中编译.NET Core项目,只要您保留32位和64位版本的本机库并在PInvoke调用周围添加特殊的分支逻辑即可:
using System;
public static class NativeMethods
{
public static Boolean ValidateAdminUser(String username, String password)
{
if (Environment.Is64BitProcess)
{
return NativeMethods64.ValidateAdminUser(String username, String password);
}
else
{
return NativeMethods32.ValidateAdminUser(String username, String password);
}
}
private static class NativeMethods64
{
[DllImport("MyLibrary.amd64.dll", EntryPoint = "ValidateAdminUser", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern Boolean ValidateAdminUser(String username, String password);
}
private static class NativeMethods32
{
[DllImport("MyLibrary.x86.dll", EntryPoint = "ValidateAdminUser", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern Boolean ValidateAdminUser(String username, String password);
}
}
Run Code Online (Sandbox Code Playgroud)
在同一目录中拥有MyLibrary.amd64.dll和MyLibrary.x86.dll程序集的位置。如果您可以将相对路径放入DllImport并具有x86 / amd64子目录,那就太好了,但是我还没有弄清楚该怎么做。
| 归档时间: |
|
| 查看次数: |
6325 次 |
| 最近记录: |