不能调用DLL进口报关,C# - > C ++,EntryPointNotFoundException
-
20-09-2019 - |
题
我想从C#中的函数用C ++编写自定义的DLL调用。但是我得到期间代码分析和在运行时警告该错误:
警告:CA1400: Microsoft.Interoperability:正确 声明 'SafeNativeMethods.SetHook()',使得 它正确地指向现有的 在“wi.dll”的切入点。非托管 切入点名目前挂 是SetHook。
错误: System.EntryPointNotFoundException是 未处理。无法找到一个条目 点命名为 'SetHook' 在DLL 'wi.dll'。
这两个项目wi.dll和C#exe文件已经在相同的debug文件夹被编译,这两个文件在这里驻留。仅存在一个与在整个文件系统的名称wi.dll文件。
C ++函数定义如下:
#define WI_API __declspec(dllexport)
bool WI_API SetHook();
我可以使用依赖步行者看到导出的函数:
as decorated: bool SetHook(void)
as undecorated: ?SetHook@@YA_NXZ
C#DLL导入看起来像(I所定义从MSDN杂志使用CLRInsideOut这些行):
[DllImport("wi.dll", EntryPoint = "SetHook", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAsAttribute(UnmanagedType.I1)]
internal static extern bool SetHook();
我没有入口点和CallingConvention定义试图为好。
这两个项目都32位,我使用W7 64位,VS 2010 RC。
我相信,我根本就忽略了一些...
预先感谢。
解决方案
那么,你知道的入口点的名称,使用入口点=“?SetHook @@ YA_NXZ”属性的函数[DllImport]属性。或者把外部的“C”在你的C ++代码的声明之前这样的名字没有得到错位。
[DllImport("wi.dll", EntryPoint = "?SetHook@@YA_NXZ", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAsAttribute(UnmanagedType.I1)]
internal static extern bool SetHook();
其他提示
CallingConvention.Cdecl
结构C不是C ++,所以当你有一个C ++修饰名的功能,你需要在C ++代码的声明使用装饰的名称作为您EntryPoint
或使用的extern“C”来关闭C ++名称装饰。
不隶属于 StackOverflow