Sign in to follow this  
Endurion

[.net] MFC to .NET, interfacing with C++ classes

Recommended Posts

Hi, Over the years i've built several editors (a paint app, MDI), 3d editor (SDI), etc. All of them are based on MFC. I've directly included my codebase files in C++. Now i've grown fond of C#. It's awesome to work with. Logically i'd like to port my apps over to C# but still interface with the C++ code. One of the problems is the 3d app. I've built DLLs which contain a getter function. The function returns a C++ interface. There's several sub-interfaces which can be returned by thet main class. A logical step would be to build managed C++ wrappers. But that sounds like quite a lot of tedious work since you'd have to wrap every interface there is. Is there not an easier way? I probably can't directly access the C++ classes? The other main problem is the paint app. All the C++ code is included directly, but the same problem applies. I'd have yet to write lots and lots of wrappers in managed C++. And i really want to use the optimized C++ code, GDI is quite slow with per pixel functions. Has anyone found a satisfying solution for this? Or is there just no way around?

Share this post


Link to post
Share on other sites
As far as I know the only way to do this is PInvoke.
Read these

I guess you didn't use COM, if you did then you could use tlbimp.exe to create an interop wrapper.

There is also another way: use Managed C++ to write the interop yourself.

Depending on how much work it is you could write a translator...

Share this post


Link to post
Share on other sites
P/Invoke could work if you add in a C interface to your C++ code wrapped in an extern "C" block. You'd have to do your own custom naming for any functions that are overloaded, and then for this to be usable in a ".NET sense" you'd want to write a managed wrapper (perhaps in C#) for the C++ objects which accesses them through the C interfaces. It probably would be about as much work as writing a C++/CLI wrapper for the C libraries.

You could use a tool like Swig to generate a C# interface to the C++ code. I have never tried it so I don't know how well it work.

Share this post


Link to post
Share on other sites
I've actually started implementing the wrapper with managed C++ now. I got it to work faster than i thought.

Part of the problem is that the class i need to wrap gives out other classes (which themselves need wrapping). I've got a DLL system with a API agnostic interface. It uses texture and vertex buffer interfaces.

Basically i think the wrapping was the easier method. Putting a C API around a C++ API always feels kinda dirty to me.

Thanks for all the input!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this