0

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);
}
  • Or https://stackoverflow.com/questions/315051/using-a-class-defined-in-a-c-dll-in-c-sharp-code – Alex K. Nov 20 '17 at 12:10
  • Add tag [pinvoke] (https://stackoverflow.com/questions/tagged/pinvoke) to your post - I can't because you already have 5 tags – Richard Critten Nov 20 '17 at 12:10
  • 1
    I read all related questions. Unfortunately all these solutions does not work for me – Peter Kolos Nov 20 '17 at 12:20
  • Then you should first, explain in your question what have you already tried, and second, explain what problems do you have with that solutions. Read [ask] to see how to make a good question – Pikoh Nov 20 '17 at 12:23
  • Described solutions does not works for me because: 1) I need to have only that two libraries (no additional wrappers); 2) PInvoke doesn't work here. Call initiates from native code. LibCPP call LibCS and not vice versa. LibCPP make call of LibCS exported function and pass pointer to native C++ class object as parameter of this function. LibCS have to call methods of C++ class object to which this pointer points – Peter Kolos Nov 20 '17 at 12:41
  • I think you need to go via Managed C++ (C++ CLI) to bridge between native c++ and c#, then use data marshalling when communicating between the two – rcs Nov 20 '17 at 14:02
  • check this out. Calling C# from unmanaged C++ https://social.msdn.microsoft.com/Forums/vstudio/en-US/5345a27a-a614-4a74-9f6d-ea7a999ddf83/calling-c-from-unmanaged-c?forum=vclanguage – Jake Nov 21 '17 at 08:27
  • Thank you for answers, but unfortunately all that does not help me. I need to access the methods of native C++ object that is already in memory. I have a pointer that points where in the memory this object is located. Is there a way to call methods of this object through this pointer? – Peter Kolos Nov 21 '17 at 11:43

0 Answers0