Good morning,
I am working on a project which combine C++ dll and C#. I have to make a kind of bridge between both. Now, I have to call C# functions inside C++ dll. for doing that, I made a C/ClI wrapper.
My project is divided in several parts :
- My C# code :
The file Class1.cs
namespace ManagedClasses
{
public class MessageShower
{
private string _message = "";
public MessageShower()
: this("TEST WRAPPER")
{
}
public MessageShower(string message)
{
_message = message;
}
public void CreateArea(Area areaTest, Point pt1 , Point pt2, string message)
{
message = "Creation of area";
Console.WriteLine(message);
pt1 = new Point(2, 3);
pt2 = new Point(4, 8);
areaTest = new Area(pt1, pt2);
Console.WriteLine("Area created");
Console.WriteLine("Details of area :");
Console.WriteLine(" -> BottomRight pt1({0},{1})", pt1.GetX(), pt1.GetY() );
Console.WriteLine(" -> BottomRight pt2({0},{1})", pt2.GetX(), pt2.GetY() );
}
public int AddXPoints(Point a, Point b)
{
return (a.GetX() + b.GetX());
}
public void Show()
{
Console.WriteLine("Message Test : {0}", _message);
}
public int Add(int first, int second)
{
return first + second;
}
}
}
The file Class2.cs
namespace WrapperTest
{
public class Area
{
public Area(Point pt1, Point Pt2)
{
BottomRight.SetX(pt1.GetX());
BottomRight.SetY(pt1.GetY());
TopLeft.SetX(Pt2.GetX());
TopLeft.SetY(Pt2.GetY());
}
private Point BottomRight;
private Point TopLeft;
}
public class Point
{
public Point() : this(0, 0) { }
public Point(int a , int b)
{
x = a;
y = b;
}
public void SetX(int a) { x = a; }
public void SetY(int b) { y = b; }
public int GetX() { return x; }
public int GetY() { return y; }
public void DisplayPoint()
{
Console.Write("Point : x({0}) , y({1})", x, y);
}
private int x;
private int y;
}
}
- My C\CLi wrapper use 3 C++ files :
The first one CLIWrapper.h :
using namespace ManagedClasses;
using namespace System::Runtime::InteropServices;
using namespace abstractWrapper;
namespace CliWrapper
{
public ref class Cli
{
Cli(void)
{
managedObject = gcnew MessageShower();
}
~Cli()
{
delete CliInstance;
}
public :
MessageShower^ managedObject;
static Cli^ CliInstance = gcnew Cli();
};
}
The .cpp files associated to the header : CLIWrapper.cpp
#include "CLIWrapper.h"
#include "NativeInterface.h"
namespace CliWrapper
{
#ifdef __cplusplus
extern "C"
{
#endif
__declspec(dllexport) void DisplayTest()
{
Cli::CliInstance->managedObject->Show();
}
__declspec(dllexport) int AddTest(int first, int second)
{
System::Int32 paramFirst = first;
System::Int32 paramSecond = second;
System::Int32 returnVal = Cli::CliInstance->managedObject->Add(paramFirst, paramSecond);
return returnVal;
}
#ifdef __cplusplus
}
#endif
}
The file concerning the declaration of the function which be called in C++ dll : NativeInterface.h :
#ifdef __cplusplus
extern "C"
{
#endif
__declspec(dllexport) void DisplayTest();
__declspec(dllexport) int AddTest(int first, int second);
#ifdef __cplusplus
}
#endif
I test it in a main program in C++ and I can call the function DisplayTest and AddTest.
Now I would like to do the same with the function CreateArea (in Class1.cs in C#). For doing the same thing, I have to transform C# object like Area , Point to unmanaged types which can be understood by C++ and I don't know how to do it.
So I would like to know if someone have an idea to do that.
Thanks in advance.