重定向标准输出。 (C#) [英] Redirecting stdout. (C#)

查看:110
本文介绍了重定向标准输出。 (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试将QEMU(快速模拟器)输出捕获到我的文本框中,基本上如果您使用以下命令执行模拟器:

Hi,
I am trying to capture QEMU's (Quick Emulator) output into my textbox, basically if you execute the emulator with this command in:

... -serial FILE:<STDOUT>



然后,这将把所有收到的来宾的串行数据发送到stdout(在主持人),现在如果我使用CON它将重定向到Windows控制台。我从我的C#程序执行QEMU,然后使用RedirectStandardOutput值。


This will then send all the received serial data of the guest to stdout (which is on the host), now if I use CON it'll redirect to the Windows Console. I execute QEMU from my C# program and then use the RedirectStandardOutput value.

/*
      Arguments for the application
*/
stringargs = stringargs + " " + medialoc.Text;
stringargs = stringargs + " -m " + membox.Text;
stringargs = stringargs + " -serial file:CON";
/*
       Define QEMU Process
*/
Process qemuproc = new Process();
qemuproc.StartInfo.FileName = "qemu.exe";
qemuproc.StartInfo.Arguments = stringargs;
qemuproc.StartInfo.UseShellExecute = false;
qemuproc.StartInfo.RedirectStandardOutput = true;
qemuproc.Start();
string time = DateTime.Now.ToShortTimeString();
/*
          Display text and the time
*/
textBox1.Text = textBox1.Text + Environment.NewLine + time + "> " +                  qemuproc.StandardOutput.ReadToEnd();
 /*
        Wait for process to exit
 */
 qemuproc.WaitForExit();



现在,如果我使用它,QEMU启动并生成一个控制台窗口(它应该正常),但输出不会重定向到文本框但是它是控制台。

如果我添加这个:


Now if I use this, QEMU starts and spawns a Console Window (which it should normally) but the output isn't redirected to the textbox but to it's Console.
If I add this:

qemuproc.StartInfo.CreateNoWindow = true;



它没有显示控制台,文本框中没有任何内容,除了时间。

有一点需要注意当从控制台应用程序重定向输出时,它确实工作正常。

我相信我做错了什么或丢失了什么。我试过搜索但是无法提出解决方案(直到现在)

-sid123


It doesn't show up the Console and there is nothing in the textbox except the time.
One thing to note is that it does work fine when redirecting output from a "Console" Application.
I believe I am doing something wrong or missing something. I have tried searching but couldn't come up with a solution (until now)
-sid123

推荐答案

看起来你正确的重定向输出并在主机应用程序中读取它。您在仅控制台应用程序上的观察证实了这一点。看起来QEMU.EXE不使用真实控制台和标准输出重定向。你无法以这种方式重定向。



一般来说,执行某些应用程序并尝试与之协作的整个想法的适用性非常有限。实际上,它仅适用于控制台应用程序,以及使用标准控制台的少量其他应用程序。理想情况下,您需要在自己的进程中拥有QEMU.EXE的功能代码。流程很好地隔离,在单独的地址空间中执行,并且难以协作,除非它们是专门为协作而设计的。



请参阅此开源项目:< a href =http://sourceforge.net/projects/qemuinterface> http://sourceforge.net/projects/qemuinterface [ ^ ]。



这不是你想要的,但是你可以看一下源码代码并了解它是如何工作的。



此外,您还可以下载QEMU.EXE的源代码: http://wiki.qemu.org/Download [ ^ ]。



你能用它做什么?选项包括:
It looks like you correctly redirect the output and read it in your host application. This is confirmed by your observation on a console-only application. It looks like QEMU.EXE does not use the "real" console and standard output to be redirected. You cannot redirect it this way.

Generally, the whole idea of executing some application and trying to collaborate with it has very limited applicability. In practice, it works only for console applications, and small number of other application which use standard console. Ideally, you need to have the code of the functionality of QEMU.EXE in your own process. Processes are well isolated, executed in separate address spaces and are hard to collaborate with, unless they are specially designed for collaboration.

Please see this open-source project: http://sourceforge.net/projects/qemuinterface[^].

This is not exactly what you want, but you can look at the source code and learn how it works.

Besides, you can download source code of QEMU.EXE: http://wiki.qemu.org/Download[^].

What can you do with it? The options are:
  1. 您可以编写它的.NET端口。 : - )
  2. 使用源代码创建DLL并在应用程序中使用它。一种方法是使用P / Invoke:

    http://en.wikipedia.org / wiki / P / invoke [ ^ ] ,

    http://msdn.microsoft.com/library /en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp [ ^ ]。



    此CodeProject文章也很有用:http://www.codeproject.com/csharp/EssentialPInvoke.asp [ ^ ]。
  3. 您也可以用其他方式使用非托管DLL。您可以使用C ++ / CLI并创建混合模式项目(托管+非托管)。这样的项目可以使用非托管代码,并且.NET程序集使用的完全与任何有效的.NET程序集一样。您可以在托管的ref类中包装非托管代码,将它们公开,并以这种方式将它们公开给引用的.NET程序集。



    请参阅:

    http://en.wikipedia.org/wiki/C%2B%2B/ CLI [ ^ ],

    http://www.ecma-international.org/publications/ standards / ecma-372.htm [ ^ ],

    http://www.gotw.ca /publications/C++CLIRationale.pdf [ ^ ],

    http://msdn.microsoft.com / EN-US / lib目录rary / xey702bw.aspx [ ^ ]。
  1. You can write a .NET port of it. :-)
  2. You use the source code to create a DLL and use it in your applications. One way of doing so is using P/Invoke:
    http://en.wikipedia.org/wiki/P/invoke[^],
    http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

    This CodeProject article can also be useful: http://www.codeproject.com/csharp/EssentialPInvoke.asp[^].
  3. You can also use unmanaged DLL in another way. You can use C++/CLI and create a mixed mode project (managed + unmanaged). Such project can use unmanaged code and used by .NET assemblies exactly as any valid .NET assembly. You can wrap unmanaged code in managed "ref" classes, make them public and, this way, expose them to the referencing .NET assembly.

    Please see:
    http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
    http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
    http://www.gotw.ca/publications/C++CLIRationale.pdf[^],
    http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^].





祝你好运。

-SA


我不是shure,但我认为你必须订阅处理' OutputDataReceived 事件并在那里阅读(重定向stdout之后) )。有关Process.OutputDataReceived Event
I'm not shure, but I think you have to subscribe to process' OutputDataReceived event and do your reading there (after you redirected stdout). See the MSDN sample for "Process.OutputDataReceived Event"


这篇关于重定向标准输出。 (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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