How to provide a callback from a C++ application to a C# DLL

HankSloka
HankSloka used Ask the Experts™
on
Hi,

I'm hoping someone can help.  I've found a lot of articles on calling C# DLL's from C++ but haven't had any sucess in finding how to pass a c++ call back to a C# DLL

I can implement an Interface in a C# dll which creates a tlb file.  In the C# application, I import this tlb and compile.  This creates a tlh file and I have a pointer to the interface which enables me to create an Instnace of the DLL class and call the functions within it.  

I understand about delegates but I can't see how to get the C++ to subscribe to a C# delegate.  Or is there another way to do this?

A link to an article would be perfect if someone has one handy.

Thanks in anticipation
Hank
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Thanks James

But unfortunately not no.

The links you gave me were very much just in C++.  I need to be able to invoke a callback of some C++ code from within a C# dll.

Cheers
Hank
1. I propose, you need to put c++/cli as an intermediate dll (if you cant port your c++ code to c++/cli)
2. C++/CLI is again a c++, but clr enabled. (check out:http://www.functionx.com/cppcli/index.htm)
3. Call that dll from C++ dll and declare this callback as a delegate which can be called back then.
(http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/1cf2b822-91a6-4cc6-9987-afefee23a390

and

http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/a76063f0-2500-4187-833f-6294871171c7)

Hope it helps.
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:
Ah,

I forgot to mention that the C++ project is in Stuio V6, which doesn't have cli capabilities.  That would mean a lot of extra work in creating a C++ mixed mode dll in a later version of studio and uning that in the V6.  Really don't think that the company I'm working for will go for it.

My only other option at the moment is to use windows messegeing.  It's messy but it works.

Thanks again James. Cheers
Hank
yup, that can be the viable option.
Let me know if you need any help further (for windows messaging as well).

Author

Commented:
Thanks for your help

Cheers
Hank

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial