views:

556

answers:

3

I've been having a hard time getting the output of a "sub-process" (one launched internally by a blackbox process that I'm monitoring via c# System.Diagnostics.Process)

I took the advice given by the answer of my previous post: here. And there you can find the details of what I've been going through.

At this point, although I'm able to locate the ssh process spawned by process1.exe, that I'm monitoring. I can't redirect the output to my c# program, because it is an "already running process", and wasn't launched directly from C#.

It seems that, all the properties that you set on a System.Diagnostics.Process object, only take effect if you are explicitly launching that process from your c# application; if some other "unmanaged process" has launched the process, setting this redirection has no effect, because the process has already been launched by something that didn't specify the redirection I need.

Is there any way to redirect output of a process that has already been launched (a process launched by a program for which I have no scope to pre-specify redirection before this process is launched)?

+1  A: 

Instead of redirecting the output directly from the running process, can you capture the output as it leaves the process A at the intended destination, the pass it into your new process?

Ian Jacobs
How is that possible? I'm already redirecting all output from process1.exe, to my C# application. At the point that process1 internally launches ssh.exe (process2), I receive able 3 more lines of output from process1, and then all output ceases, because process2 is the only thing outputting (I suspect), and I can't redirect process2's output, because it is launched internally by process1 (for which I can't control redirection).
LonnieBest
+1  A: 

Assuming there's no more straightforward solution, you could try to run a piece a code in another process through CreateRemoteThread(), explained here.

Ariel
+1  A: 

Perhaps you can look at this code. I found it when searching for a solution to do the same kind of thing; however, it was not really inter-process.

If that doesn't help you might be able to look at P/Invoking SetStdHandle and GetStdHandle which are supposed to be used when redirecting standard output. I think this is what the code sample linked to does to make the redirection happen.

Note: I just looked at this stuff and didn't actually get it to work properly. (I had a better solution available to me because I had access to the source code outputting to the console)

cmw
While not fully tested (or really working AFAIR): http://pastebin.com/m13ab7101 This is what I had developed based on the `SetStdHandle` stuff. HTH!
cmw