-1

I have two project, say named CPPDLLProject and CSharpProject.

Here in CPPDLLProject, there are few dllexport functions which can be called from CSharpProject and that calling is working fine.

But here I want to call CSharpProject's functions from CPPDLLProject so that can propagate data from C++ DLL to C# project.

Just for more explanation, Would like to add below code example. Below is the function in the C++ DLL from which would like to call C# functions.

void __stdcall CPPDLLApp::PumpMessageData(int aCode, int aType)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    if (aCode == PUMP_UPDATE_MSG && aData != NULL)
    {
        switch (aType)
        {
            case MSG_ADD:
            {
                // Call the C# function to send the added msg to C# project
                GetCPPDLLAppMsg("MSG added");
                break;
            }
            case MSG_DELETE:
            {
                // Call the C# function to send the deleted msg to C# project
                GetCPPDLLAppMsg("MSG deleted");
                break;
            }
            case MSG_UPDATE:
            {
                // Call the C# function to send the updated msg to C# project
                GetCPPDLLAppMsg("MSG updated");
                break;
            }
        }
    }

    if (aCode == PUMP_STOP_MSG)
    {
        // Call the C# function to send the stop msg to C# project
        break;
    }

    if (aCode == PUMP_START_MSG)
    {
        // Call the C# function to send the start msg to C# project
        break;
    }
}

Below is the C# project's function.

public void GetCPPDLLAppMsg(string aMessage)
{
    Console.WriteLine(aMessage);
}

Please suggest how to achieve this goal.

I have done few googling but no luck. My question might not be clear but any kind of help is appreciated.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
gsmaker
  • 533
  • 1
  • 4
  • 21

1 Answers1

0

I don't really understand your question, but you can use a intermediate dll and the following C++ method:

    void *p = (void *)wglGetProcAddress(name);
    if (p == 0 ||
        (p == (void*)0x1) || (p == (void*)0x2) || (p == (void*)0x3) ||
        (p == (void*)-1))
    {
        HMODULE module = LoadLibraryA("dllname.dll");
        p = (void *)GetProcAddress(module, name);
    }

name = name of the function

dllname = name of your dll

with this you will be able to load a dll and get the function pointer to wichever function you need, but you may need to compile a C# dll to use this, you can make a bridge or something

petacreepers23
  • 164
  • 1
  • 9
  • are you suggesting to create C# dll and load into C++ and call the desired function? If so then how to use the C# dll into another C# project? Would you please suggest? – gsmaker Jun 25 '18 at 09:07
  • https://stackoverflow.com/questions/18362368/loading-dlls-at-runtime-in-c-sharp there it is – petacreepers23 Jun 25 '18 at 09:53