• Advertisement
Sign in to follow this  

C# Interop question

This topic is 4490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I've got a C# class library that is used by native C++ code via COM. I'd like to allow the C# COM objects to call functions in my C++ application. Something like a callback function or event would be great but I don’t see any straight forward way of doing this. The C++ app can not use ATL or managed code, it’s a vanilla application and must stay that way. How can I do this?

Share this post


Link to post
Share on other sites
Advertisement
Any reason you can't export plain vanilla C functions from your C++ application and have C# call them through P/Invoke?

Magius

Share this post


Link to post
Share on other sites
You can write Managed C++ classes that wrap your regular C++ classes. This will allow you to use your C++ code in C# code.

In .NET 2005 I understand that they've drastically overhauled Managed C++. I think it's called C++/CLI now, or something like that. You might want to look into that since Managed C++ is kind of awkward and now mostly obsolete. But, at the end of the day, you should be able to do the same thing.

-John

Share this post


Link to post
Share on other sites
Quote:
Original post by Magius
Any reason you can't export plain vanilla C functions from your C++ application and have C# call them through P/Invoke?Magius


I guess I could do that. It doesn’t fit nicely with the system I've set up but it could work. I was hoping there would be a way the callback functions or events would be able to be bound at runtime. Also, wouldn’t the C# code need to import the functions explicitly?

Quote:
Original post by Teknofreek
You can write Managed C++ classes that wrap your regular C++ classes. This will allow you to use your C++ code in C# code


Can a single file be compiled as managed? That could work too and I'll have to try it out. If individual files can be compiled as managed code then this would probably be the easiest and most effective way to deal with the problem.

The perfect solution would be some way to define an event handler in native C++. What about COM connection points?

Share this post


Link to post
Share on other sites
Your C++ app can implement a COM interface without having to use ATL.

Declare the interface in the C# code so its visible to the C++ app. Write a very rudimentary C++ class that inherits the interface, roll a very rudimentary QueryInterface, AddRef, and Release. Then supply a pointer to your class back to C# via a "Notify" function that requires as input a pointer to this interface.

Now you can call into C++ all you like through the interface.

Robert

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement