Link to home
Start Free TrialLog in
Avatar of HankSloka
HankSloka

asked on

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

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
Avatar of lucky_james
lucky_james
Flag of India image

Avatar of HankSloka
HankSloka

ASKER

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.
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
ASKER CERTIFIED SOLUTION
Avatar of lucky_james
lucky_james
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your help

Cheers
Hank