5

I can easily start a process with it's STD I/O redirected but how can I redirect the STD I/O of an existing process.

Process process = Process.GetProcessById(_RunningApplication.AttachProcessId);

process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;

string text = process.StandardOutput.ReadToEnd(); //This line blows up.

Exception:

StandardOut has not been redirected or the process hasn't started yet.


Side note: If you know how to do this in C/C++ I'd be happy to re-tag and accept. I just need to know if it's even possible.

NTDLS
  • 4,757
  • 4
  • 44
  • 70
  • 1
    I'm not sure if you can do this without the cooperation of the other process, but if you can arrange such cooperation, then http://stackoverflow.com/questions/1692987/redirect-stdout-to-an-edit-control-win32 may help (since you said C/C++ was okay!). Problem is that SetStdHandle applies only to the executing process... – itowlson Jul 07 '11 at 22:49

1 Answers1

2

It doesn't look like it's possible in C#. Looking at possibility in C++.

EDIT: You can use named pipes in C# and that may be a better way to get IPC if that is what you are trying to accomplish.

Nate Zaugg
  • 4,202
  • 2
  • 36
  • 53
  • 1
    I'm going to have to agree with you. It doesn't look like its possible. I was already using names pipes for a few of the other IPC streams anyway so I just adopted them for this as well. Many thanks! – NTDLS Jul 08 '11 at 23:00