简体   繁体   English

C#:重定向已经运行的进程的标准输出

[英]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) 我一直很难获得“子流程”的输出(一个由我通过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. 此时,虽然我能够找到由process1.exe生成的ssh进程,但我正在监视。 I can't redirect the output to my c# program, because it is an "already running process", and wasn't launched directly from C#. 我无法将输出重定向到我的c#程序,因为它是一个“已在运行的进程”,并且不是直接从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; 看来,您在System.Diagnostics.Process对象上设置的所有属性只有在从c#应用程序显式启动该进程时才会生效; 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. 如果这没有帮助,您可以查看在重定向标准输出时应该使用的P / Invoking SetStdHandleGetStdHandle 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) (我有一个更好的解决方案,因为我可以访问输出到控制台的源代码)

I've got the same conundrum. 我有同样的难题。 It's not an option for me to invoke anything internal in the slave process. 在slave进程中调用内部任何东西都不是我的选择。 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. 我还没有找到.NET Process在这些条件下工作的方法。 Perhaps there is another way, maybe C++ is the way to go here? 也许有另一种方式,也许C ++是要走的路? This would be marginally acceptable, but I would like to approach it from a .NET C# perspective if possible. 这可能是可以接受的,但如果可能的话,我想从.NET C#的角度来看待它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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