NET Core 2.1托管的C ++ [英] Managed C++ with .NET Core 2.1
问题描述
我们有一个用C ++编写的库。为了使其与我们更现代的.NET项目更加兼容,我们将此C ++库包装在另一个.NET项目中。从完整的.NET Framework项目(4.5、4.6等)中引用它时,它工作正常。
We have a library written in C++. To make it more compatible with our more modern .NET projects, we wrapped this C++ library in another .NET project. It works fine when referencing it from full .NET Framework projects (4.5, 4.6, etc.).
我正在使用.NET Core 2.1创建一个新应用程序,并且试图引用此 .NET C ++库。在我的第一次尝试中,它失败说无法加载程序集。我通过安装.NET Core SDK x86并强制应用程序使用 x86 而不是任何CPU 来解决此问题。
I am creating a new application using .NET Core 2.1 and I am trying to reference this "wrapped-in-.NET C++ library". On my first attempt, it failed saying the assembly couldn't be loaded. I fixed this problem by installing .NET Core SDK x86 and forcing my application to use x86, not Any CPU.
没有任何构建错误,但是当我尝试在该库中实例化一个类时,出现以下异常:
I get no build errors, but when I try to instantiate a class within this library, I get the following exception:
<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()
.NET Core 2.1完全支持这种情况吗?
Does .NET Core 2.1 support this scenario at all?
推荐答案
正如其他人指出的那样,.NET Core 当前不支持C ++ / CLI (也称为托管C ++)。如果要在.NET Core中调用本机程序集,则必须使用 PInvoke (如您所发现的)。
As others pointed out, .NET Core does not currently support C++/CLI (aka "managed C++"). If you want to call into native assemblies in .NET Core, you must use PInvoke (as you discovered).
您还可以在AnyCPU中编译.NET Core项目,只要您同时保持32-和& 64位版本的本机库,并在PInvoke调用周围添加特殊的分支逻辑:
You can also compile your .NET Core project in AnyCPU, as long as you keep around both 32- & 64-bit versions your native library and add special branching logic around your PInvoke calls:
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);
}
}
有MyLibrary.amd64.dll和MyLibrary的位置.x86.dll程序集在同一目录中。如果您可以将相对路径放入DllImport并具有x86 / amd64子目录,那很好,但是我还没有弄清楚该怎么做。
Where you have your MyLibrary.amd64.dll and MyLibrary.x86.dll assemblies in the same directory. It would be nice if you could put relative paths into DllImport and have x86/amd64 subdirectories, but I haven't figured out how to do that.
这篇关于NET Core 2.1托管的C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!