Reputation: 1573
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!
Upvotes: 2
Views: 2750
Reputation: 1573
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?
Upvotes: 0
Reputation: 12880
Did you create a DEF file for your native DLL?
Dumpbin may also help. See what the DLL is exporting:
Upvotes: 1