Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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>

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.
C#
/*
      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();

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:
C#
qemuproc.StartInfo.CreateNoWindow = true;

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
Posted

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. 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[^].


Good luck.
—SA
 
Share this answer
 
v2
Comments
sid2x 17-Apr-14 6:35am    
Thanks for the quality answer and the time taken to write it.
I wonder if it's possible to create a Console Application in C# which calls QEMU, and then redirect the output of that console application. Messy but I think it should be able to do that.
Sergey Alexandrovich Kryukov 17-Apr-14 12:59pm    
I an not sure, but the facts you tell us in your question tell me that this is a problem, or you won't be able to do so, I tried to explain why. At the same time, my items 1-3 present some sure options, with #2 being most feasible (#3 could be more feasible if you have good experience with C++/CLI in particular).

Are you going to accept my answer formally now (green "Accept" button)?

—SA
sid2x 18-Apr-14 10:22am    
Thanks. I found out that it was a bug (not exactly) with SDL, it doesn't use the standard output.
Sergey Alexandrovich Kryukov 18-Apr-14 10:38am    
Great. You are very welcome.
Good luck, call again.
—SA
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"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900