C#:重定向已经运行的进程的标准输出 [英] C#: Redirect Standard Output of a Process that is Already Running

查看:641
本文介绍了C#:重定向已经运行的进程的标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直很难获得子进程的输出(一个由黑箱进程内部启动,我通过c#System.Diagnostics.Process监视)



我采纳了我上一篇文章的答案给出的建议:
这里



在这一点上,虽然我能够找到由process1.exe生成的ssh进程,我正在监控。我不能将输出重定向到我的c#程序,因为它是一个已经运行的进程,并没有直接从C#启动。



您在System.Diagnostics.Process对象上设置的所有属性,只有在您从c#应用程序显式启动该进程时才会生效;如果一些其他的非托管进程启动进程,设置此重定向没有效果,因为该进程已经由没有指定我需要的重定向启动。



有没有办法重定向已经启动的进程的输出(程序启动的进程,我没有范围在启动此进程之前预先指定重定向)?

$ b $假设没有更直接的解决方案,你可以尝试通过CreateRemoteThread()在另一个进程中运行一段代码, //blogs.msdn.com/jmstall/archive/2006/09/28/managed-create-remote-thread.aspxrel =nofollow noreferrer>此处


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

解决方案

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

这篇关于C#:重定向已经运行的进程的标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆