在命令提示符中获取已经运行的 Windows 进程的控制台输出?将 StreamReader 定向到命令提示符 [英] Get already running windows process' console output in a command prompt? Direct StreamReader to command prompt

查看:102
本文介绍了在命令提示符中获取已经运行的 Windows 进程的控制台输出?将 StreamReader 定向到命令提示符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定是否有办法通过 C# 在命令提示符下获取 Windows 环境中已运行进程的控制台输出.我已经通过 shell 看到了 一个答案,适用于基于 linux 的系统,也是一种方法检索 Process 对象.尽管两者都没有提供获取流程输出的解决方案.

I am trying to determine if there is a way to get the console output, in a command prompt, of an already running process in the windows environment via C#. I have seen an answer for linux based systems through the shell and also a way to retrieve a Process object. Though neither offer a solution to get the output of the process.

我的代码目前以这种方式找到一个进程(MongodDB 守护进程)

I my code I currently find a process (MongodDB daemon) this way

Process[] procs = Process.GetProcessesByName("mongod");
if (procs.Length > 0)
{
    MongoProcess = procs[0];
    Console.Out.WriteLine("Found mongod.exe, process id: " + MongoProcess.Id);
}

我还发现了 Process.StandardOutput 属性 提供可用于读取应用程序标准输出流的 StreamReader.".有没有办法将这个 StreamReader 输入定向到命令提示符输出?

I have also found the Process.StandardOutput property which supplies "A StreamReader that can be used to read the standard output stream of the application.". Is there a way to direct this StreamReader input to a command prompt output?

我也知道我可以启动一个进程并显示命令提示符(进程输出),但这只是在进程由我"启动时.

I also know that I can start a process and display the command prompt (process output), but this is only when the process is started "by me".

Process.Start(new ProcessStartInfo("notepad.exe") { CreateNoWindow = false })

我也知道我可以简单地从 StreamReader 读取并以我自己的方式显示输出,但我真的更喜欢只显示命令提示符.

I also know that I could simply read from the StreamReader and display the output in my own way, but I would really prefer to just display a command prompt.

推荐答案

Windows 不提供任何机制来追溯性地为另一个进程提供标准输出句柄(如果它是在没有标准输出句柄的情况下创建的).(此外,在大多数情况下,应用程序只会在启动时检查标准输出句柄.)

Windows does not provide any mechanism to retroactively give another process a standard output handle if it was created without one. (Besides, in most cases an application will check the standard output handle only during startup.)

如果您知道(或可以成功猜测)特定应用程序如何生成输出,则原则上可以通过将您自己的代码注入到进程中来开始捕获该输出.例如,如果您知道应用程序使用 C++ 并动态链接到 VS2015 运行时库,您可能会注入调用 freopen 如图所示.(在这种情况下,您必须知道应用程序使用哪个运行时库,因为您在查找 freopen 的地址时已指定它.)

If you know (or can successfully guess) how a specific application generates output, it may in principle be possible to start capturing that output by injecting your own code into the process. For example, if you know that the application uses C++ and is dynamically linked to the VS2015 runtime library, you might inject code that calls freopen as shown here. (In this scenario, you must know which runtime library the application uses because you have specify it when looking up the address for freopen.)

然而,代码注入并非微不足道,它会产生无意中导致进程出现故障的风险.此外,任何已经生成的输出几乎肯定会被丢弃并且不可撤销地丢失.

However, code injection is not trivial, and creates a risk of inadvertently causing the process to malfunction. Also, any output that has already been generated will almost certainly have been discarded and be irrevocably lost.

这篇关于在命令提示符中获取已经运行的 Windows 进程的控制台输出?将 StreamReader 定向到命令提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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