How do I call C# managed callback methods from unmanaged C++ code?


I've got the following class definition defined in unmanaged C++:
class Listener
	virtual void onStarted() = 0;
	virtual void onStopped() = 0;
        virtual void onResults(std::vector<Foo> foo, Results results) = 0;

Open in new window

This class is consumed by an unmanaged C++ library (of which I can' t really change the implementation).

How do I wrap this class in C++/CLI so it can be:
1) Consumed by C# (managed client)
2) The C# implementation will provide definitions for the virtual functions.
     The unmanaged library needs to call the onStarted() & onStopped() callbacks
     I can't change the underlying unmanaged C++ implementation (as we have existing unmanaged C++ clients)
     How do I do this?

jxbmaSoftware ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

you have to make a mixed-code c++ wrapper dll which transfers the unmanaged class into a managed class. in a mixed-code dll you can mix unmanaged code with managed code and access unmanaged classes/functions from managed code this way.  the c# then would use the managed class provided by the dll.

you may search for "IJW" (it just works) to get samples of mixed code projects.

jxbmaSoftware ConsultantAuthor Commented:
sarabande>> Thanks for the quick response.

I created a C++ wrapper class for this.
The part I'm confused about is the callbacks themselves would work.
I've got some standard CLI/C++ wrappers defined, and they make sense.

It's not clear to me how to specifically write the wrapper for this abstract (interface).
Can you provide example based on my code snippet or point me to a similar

1) Consumed by C# (managed client)
this part is covered by the cli wrapper class.

2) The C# implementation will provide definitions for the virtual functions.
      The unmanaged library needs to call the onStarted() & onStopped() callbacks
perhaps some windows messages could do that task quicklier ...

but you could try:

derive from Listener class in mixed-code dll (in the unmanaged part), say NetListener.  say your wrapper class is called CliListener. when it is created, it will create an instance of NetListener and pass a unique id of its own instance to the NetListener constructor. then store the pointer as IntPtr in CliListener. then overload NetListener::OnStart (and OnStop)  functions would call global cli functions CliOnStart and CliOnStop which are implemented in the same mixed code assembly. those cli functions would search the CliListener instance by id and call the OnStart, OnStop of the CliListener instance.

#pragma managed
void CliOnStart(int id)
      CliListener^ listener = FromListenerDictionary(id);

#pragma unmanaged
class NetListener : public Listener
      NetListener(int id) : myid(id) {}
      void OnStart() { 

Open in new window

I don't know whether the above can work as I don't have used it myself, but you may learn from articles like how to call from native to cli and vice versa.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Maybe this article could indeed help to make things easier: ("Pass C# Delegate as Callback to C++") - if you just pass the delegates to the C++ callback interface to be invoked from there, it should be a piece of cake.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.