本文介绍了将C ++控制台输出重定向到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试集成C ++和C#项目,以便将C ++控制台输出到C#。问题是只有在C ++ exe终止后,C ++控制台的输出才会显示在C#控制台中。我想在他们写入控制台之后很快得到它们。 目前我在C#模块中使用我的代码来调用C ++模块。 private void Form1_Load( object sender,EventArgs ex) { Process process = new Process(); process.StartInfo.UseShellExecute = false ; process.StartInfo.RedirectStandardOutput = true ; process.StartInfo.RedirectStandardError = true ; process.EnableRaisingEvents = true ; process.StartInfo.FileName = C:\\Users\\acer \\\ \\docs\\testapp.exe; process.Start(); process.BeginOutputReadLine(); process.OutputDataReceived + = new DataReceivedEventHandler(Process_OutputDataReceived); // 不使用C ++模块同时运行C#模块 // process.WaitForExit(); } void Process_OutputDataReceived( object sender,DataReceivedEventArgs e) { if (e.Data!= null ){ string s = e.Data.ToString(); Console.WriteLine(s); } } 任何人都可以让我知道我是什么在这里不见了? 谢谢。解决方案 请看我对你的问题的评论。有很多问题。这肯定是可能的,但是...... 这是我能想象到的最糟糕的整合。如果您没有C ++项目的源代码,并且C ++应用程序是非常简单的控制台应用程序,那么您仍然可以接受这一点,这样您就可以立即读取所有输出并等待子进程终止。顺便说一句,这是一个更好的子进程解决方案:同步运行它并等待终止(你怎么知道它终止?)但它是父进程的一个单独的线程。 然而,这场比赛很尴尬。如果您需要真正的集成,请在一个过程中完成所有操作(但您可以使用不同的线程)。将您的C ++项目修改为DLL。然后你有两个选择: 使你的C ++项目成为一个非托管DLL。使用P / Invoke在.NET项目中使用DLL。请参阅: http://en.wikipedia.org/wiki / PInvoke [ ^ ]; http://msdn.microsoft.com/en-us/library/Aa712982 [ ^ ]。 此CodeProject文章也可能有用: http:// www .codeproject.com / csharp / EssentialPInvoke.asp [ ^ ]。 从C ++应用程序中开发混合模式C ++ / CLI + C ++(托管+非托管)DLL。在托管(ref)C ++ / CLI类中包装C ++代码,并使它们和所需方法 public 。生成的DLL可以用作常规.NET程序集:在.NET项目中引用。 -SA cout.flush()而不是cout<< 从命令行读取c#代码,并且可能使用管道将输出链接为inpout到c#。然后,你将再有一个c#app用管道扩展这两个程序。 Hi,I'm trying to integrate C++ and C# projects in order to get C++ console output to C#. Problem is the output of the C++ exe is displayed in the C# console only after the C++ exe is terminated. I want to get them soon after they have been written to console.Currently I'm having my code in C# module as follows to call C++ module.private void Form1_Load(object sender, EventArgs ex) { Process process = new Process(); process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.EnableRaisingEvents = true; process.StartInfo.FileName = "C:\\Users\\acer\\docs\\testapp.exe"; process.Start(); process.BeginOutputReadLine(); process.OutputDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived); //not using as wants to run C# module at the same time with C++ module //process.WaitForExit(); } void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) { if(e.Data != null){ string s = e.Data.ToString(); Console.WriteLine(s); } }Can anyone let me know what's I'm missing here?Thank you. 解决方案 Please see my comment to your question. There are a number of questions. This is certainly possible, but…This is the worst kind of integration I could imagine. This is still acceptable if you don't have a source code of your C++ project, and if the C++ application is very simple console application so you could read all the output at once and wait for the child process to terminate. By the way, this is better solution for a child process: run it all synchronously and wait for termination (how else would you know it is terminated?) but it a separate thread of the parent process.However, this game is awkward. If you need real integration, do it all in one process (but you can use different threads). Modify your C++ project into DLL. Then you have two options:Make your C++ project an unmanaged DLL. Use P/Invoke to consume your DLL in a .NET project. Please see:http://en.wikipedia.org/wiki/PInvoke[^];http://msdn.microsoft.com/en-us/library/Aa712982[^].This CodeProject article could also be useful:http://www.codeproject.com/csharp/EssentialPInvoke.asp[^].Develop a mixed mode C++/CLI + C++ (managed+unmanaged) DLL out of your C++ application. Wrap you C++ code in managed ("ref") C++/CLI classes and make them and required method public. Resulting DLL can be used as a regular .NET assembly: referenced in your .NET project.—SAin your c++ console application use cout.flush() instead of cout<<Have your c# code read from comand line and perhaps use pipes to chain the output as inpout to c#. U will then have one more c# app exectuning both programs with pipe. 这篇关于将C ++控制台输出重定向到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-29 12:42