How can I change my C++ code to use as a DLL in C#?
See the question and my original answer on StackOverflowP/Invoke is more suited when you have a lot of static functions. You could instead use C++/CLI, which is more appropriate when you have a set of C++ classes, or a structured domain model hierarchy of classes. This is how you could do it with your sample:
The .H:
namespace MathFuncs {
public ref class MyMathFuncs
{
public:
double Add(double a, double b);
};
}
The .CPP:
namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b) {
return a + b;
}
}
The .CS:
static class Program
{
static void Main()
{
MyMathFuncs abd = new MyMathFuncs();
abd.Add(1.2, 2.3);
}
}
As you see, you don't need the getClass function as with C++/CLI, your MathFuncs class becomes a full-blown .NET class.
EDIT: if you want the actual computation (a+b in the sample) to happen in unmanaged code, you could do it like this; for example:
The .H:
namespace MathFuncs {
public ref class MyMathFuncs
{
public:
double Add(double a, double b);
};
class MyMathFuncsImpl
{
public:
double Add(double a, double b);
};
}
The .CPP:
namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b) {
MyMathFuncsImpl *p = new MyMathFuncsImpl();
double sum = p->Add(a, b);
delete p;
return sum;
}
#pragma managed(push, off)
double MyMathFuncsImpl::Add(double a, double b) {
return a + b;
}
#pragma managed(pop)
}
In this case, MyMathFuncsImpl::Add is generated as native code, not as IL (calling from C# is the same). See here: managed, unmanaged for more on how to mix managed and unmanaged code.