C++/CLI - 调用 DLL 中定义的非托管函数
我有一个 DLL,其中包含带有方法的非托管类。我正在尝试从 C++/CLI 调用这些方法。
我的阅读使我发现 P/Invoke 用于执行此操作。但是,我似乎无法找出如何使其发挥作用。
在 DLL 中,定义了以下类:
//Header:
namespace MathFuncs
{
class MyMathFuncs
{
public:
static __declspec(dllexport) double Add(double a, double b);
static __declspec(dllexport) double Subtract(double a, double b);
static __declspec(dllexport) double Multiply(double a, double b);
static __declspec(dllexport) double Divide(double a, double b);
};
}
//Source:
namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b) {
return a + b;
}
double MyMathFuncs::Subtract(double a, double b) {
return a - b;
}
double MyMathFuncs::Multiply(double a, double b) {
return a * b;
}
double MyMathFuncs::Divide(double a, double b) {
if (b == 0) {
throw new invalid_argument("b cannot be zero!");
}
return a / b;
}
}
根据我的阅读,以下内容应该在我的 C++/CLI 代码中声明一个方法,然后我可以在我的代码中调用该方法:
class MyMathFuncs {
public:
[DllImport("TestDLL.dll")]
static double Add(double a, double b);
};
其中“TestDLL.dll”是我的 DLL。
我得到的错误是在运行时。它显示“无法在 DLL 'TestDLL.dll' 中找到名为 'Add' 的入口点”。
我做错了什么?我已经阅读了 MSDN 上有关此的文章,但我不明白。
预先感谢您的帮助!
I have a DLL which contains unmanaged classes with methods. I'm trying to call those methods from C++/CLI.
My reading has lead me to find that P/Invoke is used to do this. However, I can't seem to find out how to make it work.
In the DLL, the following class is defined:
//Header:
namespace MathFuncs
{
class MyMathFuncs
{
public:
static __declspec(dllexport) double Add(double a, double b);
static __declspec(dllexport) double Subtract(double a, double b);
static __declspec(dllexport) double Multiply(double a, double b);
static __declspec(dllexport) double Divide(double a, double b);
};
}
//Source:
namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b) {
return a + b;
}
double MyMathFuncs::Subtract(double a, double b) {
return a - b;
}
double MyMathFuncs::Multiply(double a, double b) {
return a * b;
}
double MyMathFuncs::Divide(double a, double b) {
if (b == 0) {
throw new invalid_argument("b cannot be zero!");
}
return a / b;
}
}
From what I've read, the following should declare a method in my C++/CLI code, which I can then call in my code:
class MyMathFuncs {
public:
[DllImport("TestDLL.dll")]
static double Add(double a, double b);
};
Where "TestDLL.dll" is my DLL.
The error I get is at runtime. It says "Unable to find an entry point named 'Add' in DLL 'TestDLL.dll'".
What am I doing wrong? I've read through the MSDN articles on this, but I don't understand it.
Thanks in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否为本机 DLL 创建了 DEF 文件?
Dumpbin 也可能有帮助。查看 DLL 导出的内容:
Did you create a DEF file for your native DLL?
Dumpbin may also help. See what the DLL is exporting:
解决了。我需要在 DllImport 属性中包含 DLL 中函数的 EntryPoint。然而,MSDN 文档建议 EntryPoint 可以是 DLL 中的函数名称,但这对我不起作用 - 我必须声明函数的序号。谁能告诉我这是为什么吗?
Solved. I need to include the EntryPoint of the function in the DLL in the DllImport attribute. However, the MSDN documentation suggests that the EntryPoint can be the function name in the DLL but this doesn't work for me - I have to state the ordinal number of the function. Can anyone let me know why this is?