How to create an interface using .NET for C++ application?
I have written all the code for an application in C++. It is standard C++ written using Visual Studio 2010.
I want to create a GUI using .NET. Is it possible? Than开发者_如何学编程k you!
There a plenty of options, here's a few off the top of my head:
Wrap the c++ code in a Windows DLL, call the c++ code from .NET using p/invoke.
Wrap the c++ code in a com object, .NET can directly use com objects.
Wrap the c++ code inside some type of managed c++ wrapper (c++/CLI), load and use that new assembly in the .NET GUI.
Some more exotic ideas:
Use some type of inter-process communication, like sockets. Your c++ code would have to run in another process. I guess you could also make the c++ code into a web service.
Run the c++ code on the command line, launched by the .NET GUI. Pass parameters via command line parameters, capture and parse the standard-out text from the c++ program back in the .NET app.
Edit: I'd recommend the DLL and p/invoke. The COM object would have advantages over a plain-old dll for passing more complicated types and exposing an object model, but COM objects a pain to create and install/register (even w/ ATL, manifest files, etc). Using C++/CLI always seems more complicated than it's worth for a lot of projects - but it's worth learning a bit about it if the other options become too limiting.
Yes, that's exactly how Expression Studio works which is a GUI editing tool for WPF. You can use a similar approach and expose all your functionality through P/Invoke to the GUI app which would be in .NET.
Just keep your UI as thin as possible. In addition, if you find yourself with a ton of contact points between your C++ and C# applications, then you will want to look into using C++/CLI as a glue layer between them. C++/CLI is not exactly the best programming language to write your whole UI but it can act as a very good intermediate layer between C# frontend and C++ backend.
You could user interop to communicate with unmanaged code and have your GUI written in managed code but in general it won't be a great developer experience.
精彩评论