Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the system:
C# object ---> C++ CLI bridge ---> C++ native code.
some functionality of the C# object, uses C++ objects (via CLI)

I need to pass a delegate of a member C# function, all the layers down, to C++,
so that C++ can call it when needed.

Is this even possible?
thanks.
Posted

Yes, this is quite possible; and this possibility is quit often used, because big number of, for example, raw Windows API functions works through such callbacks.

In this approach, a callback signature is represented by a .NET delegate type, and the actual function parameter passed to the native function is represented by a delegate instance based on this delegate type (you should understand that the type of the delegate instance is System.MulticastDelegate, not the corresponding delegate type). Such actual parameter is automatically marshaled by the Platform Invocation Services to the native pointer to the callback function.

The examples of it can be found in many places, I just don't know what functions using callbacks do you want to call. I think you can just start writing code and ask another question if you get stuck. Of course, using P/Invoke for functions using native callbacks is somewhat more difficult than others. :-)

—SA
 
Share this answer
 
solved by using interfaces, thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900