无法调用DLL导入条目,C#-> C ++,EntryPointNotFoundException [英] Cannot call DLL import entry, C# -> C++, EntryPointNotFoundException
问题描述
我正在尝试从C#调用用C ++编写的自定义DLL中的函数.但是我在代码分析过程中收到警告,并在运行时收到错误:
I'm trying to call from C# a function in a custom DLL written in C++. However I'm getting the warning during code analysis and the error at runtime:
警告:CA1400: Microsoft.Interoperability:正确 的声明 "SafeNativeMethods.SetHook()",以便 它正确地指向一个现有的 wi.dll中的入口点.不受管 当前链接到的入口点名称 是SetHook.
Warning: CA1400 : Microsoft.Interoperability : Correct the declaration of 'SafeNativeMethods.SetHook()' so that it correctly points to an existing entry point in 'wi.dll'. The unmanaged entry point name currently linked to is SetHook.
错误: System.EntryPointNotFoundException原为 未处理.找不到条目 DLL"wi.dll"中名为"SetHook"的点.
Error: System.EntryPointNotFoundException was unhandled. Unable to find an entry point named 'SetHook' in DLL 'wi.dll'.
两个项目wi.dll和C#exe均已编译到同一个DEBUG文件夹中,两个文件均位于此处.整个文件系统中只有一个名为wi.dll的文件.
Both projects wi.dll and C# exe has been compiled in to the same DEBUG folder, both files reside here. There is only one file with the name wi.dll in the whole file system.
C ++函数定义如下:
C++ function definition looks like:
#define WI_API __declspec(dllexport)
bool WI_API SetHook();
我可以使用Dependency Walker看到导出的功能:
I can see exported function using Dependency Walker:
as decorated: bool SetHook(void)
as undecorated: ?SetHook@@YA_NXZ
C#DLL导入看起来像(我已经使用MSDN杂志中的CLRInsideOut定义了这些行):
C# DLL import looks like (I've defined these lines using CLRInsideOut from MSDN magazine):
[DllImport("wi.dll", EntryPoint = "SetHook", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAsAttribute(UnmanagedType.I1)]
internal static extern bool SetHook();
我也尝试过不使用EntryPoint和CallingConvention定义.
I've tried without EntryPoint and CallingConvention definitions as well.
两个项目都是32位,我使用的是W7 64位,VS 2010 RC.
Both projects are 32-bits, I'm using W7 64 bits, VS 2010 RC.
我相信我只是忽略了一些东西....
I believe that I simply have overlooked something....
提前谢谢.
推荐答案
好,您知道入口点名称,请使用[DllImport]属性中的EntryPoint =?SetHook @@ YA_NXZ"属性.或者在您的C ++代码中的声明前加上extern"C",这样名称就不会被弄乱.
Well, you know the entry point name, use the EntryPoint = "?SetHook@@YA_NXZ" property in the [DllImport] attribute. Or put extern "C" before the declaration in your C++ code so the name doesn't get mangled.
[DllImport("wi.dll", EntryPoint = "?SetHook@@YA_NXZ", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAsAttribute(UnmanagedType.I1)]
internal static extern bool SetHook();
这篇关于无法调用DLL导入条目,C#-> C ++,EntryPointNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!