How to create an interface using .NET for C++ application?

我只是一个虾纸丫 提交于 2019-12-10 20:36:19

问题


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? Thank you!


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/6003074/how-to-create-an-interface-using-net-for-c-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!