在Visual C ++中将控制台应用程序与Windows窗体应用程序合并 [英] Merging Console application with Windows Form Application in Visual C++

查看:115
本文介绍了在Visual C ++中将控制台应用程序与Windows窗体应用程序合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,
我正在为一个即将发生的事件设计一个项目,并且我需要设计一个基于手势识别的项目.我在前一部分使用OpenCV,但在下一步使用该手势时,我需要使用.NET框架.我正在考虑在Windows窗体应用程序项目文件中合并Visual C ++的win32控制台应用程序项目.可能吗?如果是,那么请告诉我有关此操作的信息.如果不是,请告诉我其他适合我的要求的方法.
谢谢.

Hello Friends,
I am working on a project for an upcoming event and I am required to design a gesture recognition based project. I am using OpenCV for the former part but for the next step of using that gesture I am required to use .NET framework. I am thinking of merging a win32 console application project of Visual C++ in A Windows Form Application Project File . Is it possible? If yes, then please tell me about doing it. If not, then tell me any other suitable method keeping in view my requirement.
Thank You.

推荐答案

您当然可以.首先,让我假设您拥有所有两个项目的源代码,然后可以自由合并它们.我建议您不要弄乱控制台.而是使用一些UI控件,通过.NET应用程序模拟控制台.我通常使用ListBox(每行文本项),它比TextBox更好,但是还有许多其他选择;例如,您还可以添加许多有用的功能(在输出中搜索,保存等).

您可以将Win32 C ++应用程序转换为C ++/CLI类库,这仅需要进行最小的修改.如果您确实使用了需要大量使用本机环境/库的任何内容,请创建一个混合模式的C ++/CLI +非托管C ++项目.将应用程序的main功能转换为C ++/CLI(ref)类.现在,抽象出所有写入控制台的方法.使它们具有完全相同的签名的抽象虚拟方法,并使此类成为公共和抽象.

您的.NET应用程序可以像其他任何.NET程序集一样引用此托管或混合模式的DLL.现在,在此.NET应用程序中,子类化上一段中提到的类,并重写那些输出方法以将字符串写入到第一段中提到的控件中.

这是我可以考虑的最简单且不太麻烦的方法.您无需更改现有Win32应用程序的任何逻辑,但可以轻松添加任何功能.

现在,如果您没有此Win32应用程序的源代码,这将变得更糟,因为您正在处理一个单独的进程,并且您没有支持该解决方案的方法.通过使用System.Diagnostics.Process.Start http作为子进程运行此应用程序,可以很容易地解决此问题. ://msdn.microsoft.com/zh-CN/library/system.diagnostics.process.aspx [ http://msdn.microsoft.com /en-us/library/system.diagnostics.process.standardoutput.aspx [
You certainly can. First, let me you assume you have source codes of all both projects, then you can freely merge them. I would advice you not to mess with console. Instead, simulate console with your .NET application using some UI control. I usually use ListBox (one item per text line) which is better than TextBox, but there are many other options; you can also add many useful features (search in output, save it, etc.)

You can turn your Win32 C++ application into C++/CLI class library, which will require only minimal modification. If you really use anything which require extensive use of native environment/libraries, make if a mixed-mode C++/CLI + unmanaged C++ project. Turn main function of your application into a C++/CLI (ref) class. Now, abstract out all methods writing to the console. Make them abstract virtual methods with exact same signature and make this class public and abstract.

This managed or mixed-mode DLL can be referenced by your .NET application as any other .NET assembly. Now, in this .NET application, subclass the class mentioned in the above paragraph and override those output method to write strings into your control mentioned on first paragraph.

This is the simplest and less intrusive method I can think off. You don''t need to change any logic of your existing Win32 application but can easily add any features.

Now, in case you don''t have the source code of this Win32 application, this is much worse as you are dealing with a separate process and you don''t have a way to support the solution. This can be resolved pretty easily by running this application as a child process using System.Diagnostics.Process.Start, http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^]. You would also need to redirect the console output of the child process, read the redirected stream(s) StandardOutput and StandardError and use the content of these streams in you UI the way you want. You will find the redirection code sample here: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^].

Please, pay attention that I had to give you too different variants of the answer because you haven''t been comprehensive in formulation of your question. Providing variants is somewhat unpleasant, because I know you would be using only one of them, so it''s not very satisfying to do a double job. So please, next time try to make sure you are 100% certain when you formulate a question — thank you.

—SA




您需要创建一个用于调用win32应用程序的批处理文件.您需要从
运行该批处理文件 Windows窗体应用程序项目文件.

谢谢
Hi,

You need to create a batch file for calling the win32 application.The u need to run this batch file from
Windows Form Application Project File.

Thanks


这篇关于在Visual C ++中将控制台应用程序与Windows窗体应用程序合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆