简体   繁体   中英

C#: Redirect Standard Output of a Process that is Already Running

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)?

假设没有更简单的解决方案,您可以尝试通过CreateRemoteThread()在另一个进程中运行一段代码, 这里解释。

而不是直接从正在运行的进程重定向输出,是否可以在输出离开进程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.

: 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)

I've got the same conundrum. It's not an option for me to invoke anything internal in the slave process. It's already running, which code is beyond my control. But I do know it spits out Standard Output , which I want to monitor, process, etc. It's one thing I kick off the process, I can configure the redirection, but in this instance, the process will be launched secondarily to my kicking off the primary slave process. So I do not have that option. I haven't found a way for .NET Process to work under these conditions. Perhaps there is another way, maybe C++ is the way to go here? This would be marginally acceptable, but I would like to approach it from a .NET C# perspective if possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM