Seb*_*n K 2 .net c# c++ dllimport
我有一个奇怪的问题我有C++ DLL,我使用DLL导入在C#库中导入.如果我指定入口点,一切都按预期工作,这是示例:
internal static class UnsafeMethods
{
[DllImport("GoodSchool.dll", EntryPoint = @"?AddNum@@YAHHH@Z")]
public static extern int AddNum(int num1, int num2);
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(UnsafeMethods.AddNum(4,5));
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我像这里一样使用simplfied导入:
[DllImport("GoodSchool.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int AddNum(int num1, int num2);
Run Code Online (Sandbox Code Playgroud)
我得到熟悉的错误消息:
未处理的异常:System.EntryPointNotFoundException:无法在DLL'GoodSchool.dll'中找到名为'AddNum'的入口点
我使用依赖验证方法是否正确暴露,我解码符号验证参数和命名约定 - 一切似乎都很好.
C++中的函数签名非常简单:
__declspec(dllexport) int AddNum(int num1, int num2);
Run Code Online (Sandbox Code Playgroud)
有关如何在C#中调用此方法而不提供装饰名称作为EntryPoint的任何建议?我做错了什么?我不想使用"C"导出,因为我的理解是装饰函数名称与DllImport一起使用时非常好.
C++破坏函数名称以解释函数名称重载.毕竟,如果DLL有
__declspec(dllexport) int AddNum(int num1);
__declspec(dllexport) int AddNum(int num1, int num2);
Run Code Online (Sandbox Code Playgroud)
哪个会AddNum指?
符号?AddNum@@YAHHH@Z是在非托管DLL中公开的受损(又称装饰)名称.
https://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_C.2B.2B
您的DLL不会导出调用的内容AddNum.
装饰的函数名称可以正常使用DllImport,正如您所看到的那样.但是这需要您在导入中指定修饰的函数名称.就链接器(静态或动态)而言,未修饰的名称不存在 - AddNum根本不是库暴露的符号.
如果你想要做你想要的事情:
在C#中调用此方法而不提供装饰名称作为EntryPoint?
那么你就不能让C++首先破坏这个名字.你可以任意在指定的装饰名称DllImport 或使用extern "C"上的C++代码链接.你必须选择其中一个.
| 归档时间: |
|
| 查看次数: |
5969 次 |
| 最近记录: |