3

I want to launch an external application, and capture its output. That's pretty easy, using Diagnostics.Process and its OutputStream.

However, the process I have generated binary data, which I need to capture as a Stream, not as the textual StreamWriter

Its there a way to get the underlying raw binary stream?

Currently, I hack it by launching a batch file which looks like:

myapplication | socat stdin tcp-connect:localhost:12345

And in my code I create and listen on a TCP socket. while this hack works, its a bit ugly, and I prefer to use the external process directly.

(One thing I can't do is use local files, as the data is real-time in its nature)

3 Answers 3

4

How about p.StandardOutput.BaseStream?

Sign up to request clarification or add additional context in comments.

2 Comments

How about to give a full answer?
@SerG: This is a full answer. The only question asked was "Its [sic] there a way to get the underlying raw binary stream?"
1

You could use a named pipe, similar to but more in-depth than the OutputStream functionality of Process (which is basically a listener for StandardOutput). Named pipes are accessible using Streams, without any assumption as to the content being Unicode character data. They also don't tie up network ports, unless you want to connect to a pipe remotely (which is also possible).

1 Comment

But you need to modify the child process to make it use the pipe, or else p/invoke CreateProcess to pass the pipe as the child's standard output handle.
1

I used the follwowing code to read binary data from stdout of a program

ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "yourProcess";
info.Arguments = "blah";
info.StandardOutputEncoding = System.Text.Encoding.GetEncoding("latin1");//important part
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
Process p = Process.Start(info);
FileStream fs = new FileStream("outputfile.bin", FileMode.Create);

while((val = p.StandardOutput.Read())!= -1){
    fs.WriteByte((byte)val);
}

p.WaitForExit();
fs.Close();

Comments

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.