4

I'm trying to list the supported interfaces on which dumpcap.exe can capture data.

The command i want to run is "dumpcap.exe -D"

On my system, it gives the output as :-

1. \Device\NPF_{1B627AA8-2A1D-4C90-B560-517CF71B33A5} (Intel(R) 825
Network Connection)
2. \Device\NPF_{7ECC4D31-DF17-49AB-960D-628D0580F3C6} (Microsoft)

I'm trying to read the above output by runing the same command from a WPF C# application. I have the following code to do so:-

        Process proc = new Process();

        //set the path to dumpcap.exe (verified by me)
        proc.StartInfo.FileName = "..\\..\\..\\..\\..\\Dumpcap\\dumpcap.exe";

        StringBuilder dumpcapArgs = new StringBuilder();

        dumpcapArgs.Append("-D");

        proc.StartInfo.Arguments = dumpcapArgs.ToString();
        proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.UseShellExecute = false;

        proc.Start();

        while (!proc.StandardOutput.EndOfStream)
        {
            string line = proc.StandardOutput.ReadLine();
            MessageBox.Show(line);

        }

When i debug, the execution does not go in my while loop at all. The StandardOutput is already at the end of stream. Digging further into StandardOutput base stream class members, i see a lot of System.NotSupportedExceptions in length, position etc.

What is possibly wrong with the above code?

Quick Observation

I tried another sample C# console application with the following code:

        Process proc = new Process();

        proc.StartInfo.FileName = "C:\\Karan\\Dumpcap\\dumpcap.exe";
        proc.StartInfo.Arguments = "-D";
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.Start();

        Console.WriteLine(proc.StandardOutput.ReadToEnd());

        proc.WaitForExit();

This prints the output as expected!

However, if i try to read the standardoutput in a string variable, i get an empty string.

For example, this returns me an empty string:-

       Process proc = new Process();

        proc.StartInfo.FileName = "C:\\Karan\\Dumpcap\\dumpcap.exe";
        proc.StartInfo.Arguments = "-D";
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.Start();


        String output = proc.StandardOutput.ReadToEnd();


        proc.WaitForExit();

3 Answers 3

3

Have you set EnableRaisingEvents to true?

I'm not sure if this will do the trick since I usually do the redirecting of the output in a different way:

private Process proc;
private string procOutput = string.Empty;

private void StartProc()
{
    this.procOutput = string.Empty;
    this.proc = new Process();
   ...
    this.proc.StartInfo.UseShellExecute = false;
    this.proc.StartInfo.CreateNoWindow = true;
    this.proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

    this.proc.EnableRaisingEvents = true;
    this.proc.OutputDataReceived += this.ProcOutputDataReceivedHandler;
    this.proc.Exited += this.ProcExitedHandler;

    this.proc.Start();

    this.proc.BeginOutputReadLine();
}

private void ProcOutputDataReceivedHandler(object sendingProcess, DataReceivedEventArgs e)
{
    if (!string.IsNullOrEmpty(e.Data))
    {
         this.procOutput += e.Data;
    }
}

private void ProcExitedHandler(object sender, EventArgs e)
{
    // Check the exitcode for possible errors happened during execution.
    MessageBox.Show(this.procOutput);
}
Sign up to request clarification or add additional context in comments.

12 Comments

I tried your method. I'm not able to get the ouput in a string varible (it always comes as empty), but using the standard synchronous method (readToEnd() and using Console.WriteLine()) prints the expected output.
That's strange. Can you set a breakpoint in the ProcOutputDataReceivedHandler to check if it actually hits and if so, what's the content of e.Data?
It's not hitting ProcOutputDataReceivedHandler. After BeginOutputReadLine(), my main method exists as usual.
Are you using a console app or a winforms app? If it's a console app, try adding Console.ReadLine() at the end of the Main method, to ensure that the process has finnished before your application is finnished. It's a bit trial and erro here, since I cannot reproduce your issue...
The initial code posted in the question is a WPF C# application. The code posted in my quick observation is a console application.
|
2

You never start the process. You should do so using proc.Start();

Length, Position and similar members of Stream are not supported, because the stream representing the standard out of a console application is unknown in length and a forward only stream. So that's normal and not an error in your code.

1 Comment

Sorry, i missed that line while copying the code from visual studio. my bad. Updated.
0

Try this:

string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();

foreach(var line in output.Split('\r'))
{
    MessageBox.Show(line);
}

Why does this work? Potentially there is nothing in the output stream when you first query EndOfStream. This is a race condition.

I consulted this article before posting. Give it a read http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx

3 Comments

I'm still getting an empty string "" in the message box.
I think you are very close to my problem. I'm not able to get the ouput in a string varible (it always comes as empty), but using the standard synchronous method (readToEnd() and using Console.WriteLine()) prints the expected output. I think it has something to do with waiting for the process to exit and filling the buffer of Console.Writeline()
I didn't do it exactly like that, but with your hint about "it has something to do with timing", I added another line that also reads the StandardError into a different string. Just doing that made it so that I now get the StandardOutput string as expected. Thx!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.