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

后端 未结 4 1139
离开以前
离开以前 2020-12-10 03:52

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

相关标签:
4条回答
  • 2020-12-10 03:55

    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.

    0 讨论(0)
  • 2020-12-10 04:01

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

    0 讨论(0)
  • 2020-12-10 04:13

    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)

    0 讨论(0)
  • 2020-12-10 04:17

    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?

    0 讨论(0)
提交回复
热议问题