C++ function trigger C# function

117 Views Asked by At

Basically my code is looks like below

Managed.dll

Managed.cs

class MyManagedClass
{
public ShowMessage()
{
System.out.println("My Message");
}
}

Wrapper.dll

ref class Wrapper

    {

    };

Native.lib

class NativeClass
{
public:
void NativeMessage()
{
cout<<"Print Message";
}

}

Main

void main
{
NativeClass ob;
ob.NativeMessage();
}

my issue is whenever the "ob.NativeMessage();" called, somehow MyManagedClass::ShowMessage() has to be triggered.

And more impotent Native.lib linked in Wrapper.dll and Wrapper.dll referenced in Managed.dll.

Can any one help me on this.

1

There are 1 best solutions below

0
On

If you're running strictly from a C++ environment, you will want to host a .NET runtime in your C++ application. If you're running from a .NET environment, that part is already done and you will need to pass a delegate to the C++ code to be called later (this, by the way, is fraught with problems as the .NET runtime up to at least version 3.0 can and will garbage collect delegates out from under you).