C# 托管C++;使用.NETCore2.1 我们有一个用C++编写的库。为了使它更符合我们的现代.NET项目,我们将这个C++库打包在另一个.NET项目中。当从完整的.NET Framework项目(4.5、4.6等)中引用它时,它工作良好 我正在使用.NETCype 2.1创建一个新的应用程序,我试图引用这个“包在.NET中的C++库”。在我的第一次尝试中,它失败了,说程序集无法加载。通过安装.NET Core SDK x86并强制我的应用程序使用x86,而不是任何CPU,我修复了这个问题
我没有得到生成错误,但是当我尝试在这个库中实例化一个类时,我得到以下异常:C# 托管C++;使用.NETCore2.1 我们有一个用C++编写的库。为了使它更符合我们的现代.NET项目,我们将这个C++库打包在另一个.NET项目中。当从完整的.NET Framework项目(4.5、4.6等)中引用它时,它工作良好 我正在使用.NETCype 2.1创建一个新的应用程序,我试图引用这个“包在.NET中的C++库”。在我的第一次尝试中,它失败了,说程序集无法加载。通过安装.NET Core SDK x86并强制我的应用程序使用x86,而不是任何CPU,我修复了这个问题,c#,c++,.net,asp.net-core,.net-core,C#,C++,.net,Asp.net Core,.net Core,我没有得到生成错误,但是当我尝试在这个库中实例化一个类时,我得到以下异常: <CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load. ---> System.EntryPointNotFoundException: A library name must be specified in a DllImport attribute applied to non-IJW metho
<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()
模块>模块:加载失败的C++模块。
--->System.EntryPointNotFoundException:必须在应用于非IJW方法的DllImport属性中指定库名称。
at_getfibertrid()
at.LanguageSupport.\u初始化(LanguageSupport*)
at.LanguageSupport.Initialize(LanguageSupport*)
---内部异常堆栈跟踪的结束---
at.ThrowModuleLoadException(String errorMessage,Exception innerException)
at.LanguageSupport.Initialize(LanguageSupport*)
at.cctor()
.NETCore2.1是否支持此方案 没有。Net Cype是跨平台的,但C++/CLI不是,微软C++编译器需要Windows。<> PInvoke > <强> >似乎是唯一的出路。
将库DLL放入解决方案文件夹(实际的C++ DLL,而不是.NET包装器)。< /P> 注意:不要在解决方案中引用DLL,只需将DLL放在同一文件夹中即可
然后使用DLL导入访问以下方法:static class NativeMethods
{
[DllImport("MyLibrary.dll", EntryPoint = "ValidateAdminUser", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern Boolean ValidateAdminUser(String username, String password);
}
注2:它仍然需要.NET核心项目在x86体系结构中运行 正如其他人指出的,.NET核心(又名“托管C++”)。如果要调用.NET Core中的本机程序集,必须使用(如您所发现的) 您也可以在任何CPU中编译.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(username, password);
}
else
{
return NativeMethods32.ValidateAdminUser(username, 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子目录,那就太好了,但我还不知道如何做到这一点。包装器项目的.NET版本是什么?您好@ahelwer,顺便说一句,它是.NET 4.0,我们还尝试使用.NET 4.6,但它返回了相同的错误。需要澄清的是,此包装器项目是使用PInvoke调用本机库的.NET项目,还是一个C++/CLI项目(也称为“托管C++”)?对,其他人回答说,.NET Core不支持C++/CLI,尽管PInvoke工作正常。有关更多信息,请参阅