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!
Did you create a DEF file for your native DLL?
Dumpbin may also help. See what the DLL is exporting: