We help IT Professionals succeed at work.

Callback from C# .dll to unmanaged C++

tomcalef
tomcalef asked
on
I have a c++ application successfully calling functions in a C# .dll using a COM interface.

How do I setup a callback in the c++ where a function in the C# can call it?
Comment
Watch Question

jkr
Top Expert 2012

Commented:
That's a lot easer, since you can just use P/Invoke - see http://msdn.microsoft.com/en-us/library/aa288468%28v=VS.71%29.aspx ("Platform Invoke Tutorial"). Right the 1st example should be sufficiant, just specify the function you want to call.

Author

Commented:
I guess the issue with my situation is that the .dll is c#, and it needs to call functions from the application which is in c++. I don't think i can do a dll import from that application can i?
jkr
Top Expert 2012

Commented:
Hm, not from an executable, but why not putting the callback inside a DLL that this executable loads?

Author

Commented:
The way our system architecture is done, this isn't allowed. What the ideal case i would think would be is to pass a function pointer to c# so that I can just call that function when the event happens.
jkr
Top Expert 2012

Commented:
Hmm, passing a function pointer to managed code won't work. I guess if you can't use a DLL, you'll also have to use a COM interface for that direction.
Top Expert 2016
Commented:
you could have a mixed-code dll in c++ which contains both managed and unmanaged code and therefore can call into both worlds. you can find samples by using the "it just works" (IJW) slogan ms has created for such assemblies.

Sara
evilrixSenior Software Engineer (Avast)

Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.