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

Started by
3 comments, last by Endurion 15 years, 3 months ago
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?

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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...

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!

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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.
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!

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement