我努力学习的P/Invoke,所以我创建了C++无法调用C++ DLL在C#中,当找到一个切入点
KingFucs.h一个简单的DLL:
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的debug文件夹中,用代码:
[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,否则会是另一个例外。但我的方法名称完全一样....我想不出其他原因。
请在这里看到我的问题:http://stackoverflow.com/questions/9849541/writing-c-intended-to-be-called-from-c – 2012-04-11 16:01:48
我解决了同样的问题,并描述在[这篇文章] (https://stackoverflow.com/a/45263176/1817569)。 – Hamid 2017-07-23 08:45:52