![](/img/trans.png)
[英]"Unable to find an entry point named [function] in dll" (c++ to c# type conversion)
[英]Unable to find an entry point when calling C++ dll in C#
我正在尝试学习P / Invoke,所以我在C ++中创建了一个简单的dll
KingFucs.h:
namespace KingFuncs
{
class KingFuncs
{
public:
static __declspec(dllexport) int GiveMeNumber(int i);
};
}
KingFuns.cpp:
#include "KingFuncs.h"
#include <stdexcept>
using namespace std;
namespace KingFuncs
{
int KingFuncs::GiveMeNumber(int i)
{
return i;
}
}
所以它编译,然后我将这个DLL复制到我的WPF的调试文件夹中,代码如下:
[DllImport("KingFuncDll.dll", EntryPoint = "GiveMeNumber", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int GiveMeNumber(
int i
);
并在按钮单击中调用它:
private void Button_Click(object sender, RoutedEventArgs e)
{
int num = GiveMeNumber(123);
}
但它给了我例外:
无法在DLL“KingFuncDll.dll”中找到名为“GiveMeNumber”的入口点。
真的......我做错了什么......显然能找到DLL,否则会是另一个例外。 但我的方法名称完全一样......我想不出其他原因。
导出函数时需要使用extern "C"
,以便抑制C ++名称修改。 而且你也不应该尝试p / invoke给一个类的成员。 使用免费功能:
extern "C" {
__declspec(dllexport) int GiveMeNumber(int i)
{
return i;
}
}
在托管端,您的DllImport
属性都是错误的。 不要使用仅适用于Win32 API的SetLastError
。 如果没有文本参数,请不要打扰设置CharSet
。 不需要ExactSpelling
。 调用约定可能是Cdecl
。
[DllImport("KingFuncDll.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int GiveMeNumber(int i);
问题是您在C ++类中声明C ++“函数”并告诉P / Invoke使用StdCall。
尝试在类之外声明一个C ++函数,然后像你一样导出它。 然后你的代码应该工作。
如果你真的必须在一个类中有一个C ++函数,请看看CallingConvention.ThisCall。 但是,您负责创建非托管类实例并将其作为P / Invoke调用的第一个参数传递
dll文件的入口点名称在.exp文件中给出,该文件位于调试文件夹中,其中存在其他源文件。 如果dumpbin不起作用,你可以尝试这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.