I have two libraries LibCPP.dll (native C++) and LibCS.dll (managed C#). LibCS.dll exports some functions by using of Robert Giesecke DllExport. LibCPP.dll loads LibCS.dll and use these functions.
I need to make the folowing: On the LibCPP.dll side creates native C++ class object and pointer to that object is sent to LibCS.dll as parameter of some function. How can I make call of C++ class methods on the LibCS.dll side via recieved pointer?
Example of code on the LibCS.dll side:
public static class ExportClass
{
[DllExport(ExportName = "SimpleFunction", CallingConvention = CallingConvention.StdCall)]
public static int SimpleFunction(int a, int b)
{
return a + b;
}
[DllExport(ExportName = "CPPObjectMethodCall", CallingConvention = CallingConvention.StdCall)]
public static bool CPPObjectMethodCall(IntPtr cppObject)
{
// How to make call of c++ object method here?
}
}
Example of code on the LibCPP.dll side:
class CPPClass
{
public :
CPPClass() {}
~CPPClass() {}
void CPPClassMethodCall(std :: string text)
{
MessageBoxA(NULL, text.c_str(), "Message", 0);
}
};
void TestFunction()
{
HMODULE hDLL = LoadLibraryA("LibCS.dll");
typedef int (*SIMPLEFUNCTION)(int a, int b);
typedef bool (*CPPOBJECTMETHODCALL)(void * cppObject);
SIMPLEFUNCTION SimpleFunction = (SIMPLEFUNCTION)GetProcAddress(hDLL, "SimpleFunction");
CPPOBJECTMETHODCALL CPPObjectMethodCall = (CPPOBJECTMETHODCALL)GetProcAddress(hDLL, "CPPObjectMethodCall");
int x = SimpleFunction(1, 2);
CPPClass * cppObject = new CPPClass();
// Pass pointer to C++ object
CPPObjectMethodCall(cppObject);
delete cppObject;
FreeLibrary(hDLL);
}