How to call C# callbacks from C++/CLI?

  • Thread starter Thread starter John Doe
  • Start date Start date
J

John Doe

Hi,
Tried searching Google on this topic but not many came up. Basically I
have a C# GUI client that wants to receive 'notifications' from a worker DLL
(which is implemented in C++/CLI). So far, from C# I am able to call any
method directly in C++/CLI DLL. But I would like to have the C++/CLI invoke
some callback methods from within C# (some event notifications so that the
GUI updates its UI).
In the old days using C/C++, I could do this easily by passing a class
object ptr to the DLL and have it invoke callback methods declared in that
class. Can I do the same thing in .NET? Since there is no header file, I
don't know how to 'tell' the C++/CLI component which C# callback methods to
use. Thanks.
 
John,

Basically, you would define a separate assembly where you define the
delegate that is used for the callback (or an interface, if you need a more
elaborate contract).

Then, you would reference the callback from the client and in your C++
code, and expose the a parameter type/property/field which would receive the
callback in the C++ code. Then, you can just execute the delegate when
needed (or the member on the interface, if that is the way you are going).
 
Back
Top