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

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top