Jump to content
  • Advertisement
Sign in to follow this  
Endurion

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

This topic is 3440 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, 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
Advertisement
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
I can't use PInvoke, that only works with C like code, not C++ classes (AFAIK).
I'm just reading up on the managed C++ stuff regarding this; it seems there a few issues with building the project.

World of pain, here i come!

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!