views:

956

answers:

2

Basically I have a bunch of unmanaged VC++ static libraries. And the VC++ GUI application which uses those and it is based on MFC. The goal is to replace the GUI app with the one done in C# instead but using all the same static libraries. The question is if this even possible, and if yes, then what is the right way to accomplish that?

Suggestions, anybody? Thanks.

+2  A: 

Yes, it is possible using C++/CLI for managed C++ code. You would write a C++/CLI WinForms app and simply link in your static lib as per normal.

However, if there is a lot of tight coupling between the GUI code and the libraries then this can get a bit messy. You will need to worry about converting some data types between the managed and unmanaged world, particularly strings. If you need to pass managed objects/arrays

There is a good introduction on Wikipedia and lots of documentation on MSDN.

Rob Walker
The trick is also that GUI app has to extend some objects from those static libraries, to be able to react on some callback methods called from the libraries. Will that work?
Ma99uS
Thanks. I will check that out.
Ma99uS
A managed class can not inherit from an unmanaged class directly, but you could implement unmanaged classes in your app that inherit from the library classes and act as a proxy for your managed classes
Rob Walker
+1  A: 

Rob is correct - you can do it in C++/CLI entirely, but we found it most useful to wrap some native classes in a managed WinForms User Control class. This managed class contained an instance of the native class, and not only marshalled data like strings in method calls, but also converted native callbacks (implemented with boost::signal) to .NET events. The whole solution for this signal-event translation is spelled out in this question. The .NET WinForms User Control also trapped native exceptions and re-threw them as managed exceptions, and also did some translation of non-.NET interface (methods returning iterators) to a more .NET-styled interface, which you can read about in this question. We were then able to use the .NET class directly in a WPF application. Note that if you wrap it as a .NET class it will have to go in a DLL to be used from C#.

Brian Stewart