windows 在 C++ 中读取另一个进程的标准输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4093252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:32:34  来源:igfitidea点击:

Read another process' stdout in C++

c++windowsstdout

提问by RobH

In Windows, is there a way to launch a process in C++ and then read what it spat out into stdout when it's done? The process must be run using elevated privileges (on Vista or later) if necessary.

在 Windows 中,有没有办法在 C++ 中启动一个进程,然后在它完成后将它吐出的内容读入 stdout?如有必要,必须使用提升的权限(在 Vista 或更高版本上)运行该进程。

I'm currently using ShellExecuteEx() to launch the process and running a while-loop until GetExitCodeProcess() no longer returns STILL_ACTIVE via the lpExitCode parameter (with a WaitForSingleObject() call doing a 100 msec wait during each iteration).

我目前正在使用 ShellExecuteEx() 来启动进程并运行一个 while 循环,直到 GetExitCodeProcess() 不再通过 lpExitCode 参数返回 STILL_ACTIVE(使用 WaitForSingleObject() 调用在每次迭代期间进行 100 毫秒的等待)。

采纳答案by Alex Jasmin

There's no easy way to do this.

没有简单的方法可以做到这一点。

Calling ShellExecuteEx()with the runasverb sends an RPC message to the AppInfo NT Servicewitch then run the application from an elevated session. There's no API to easily connect the input/output of the elevated process to your application.

ShellExecuteEx()使用runas动词调用会向AppInfo NT 服务发送一条 RPC 消息,然后从提升的会话中运行应用程序。没有 API 可以轻松地将提升进程的输入/输出连接到您的应用程序。

Thomas Hruska in his The Code Project articlepresents his implementation of a CreateProcessElevated()function that solves this.

Thomas Hruska 在他的The Code Project 文章中介绍了他对CreateProcessElevated()解决此问题的函数的实现。

Instead of running the elevated program directly CreateProcessElevated()relies on another executable that receive the name of the stdin,stdout,stderrnamed pipes and recreate their handles in the elevated session before calling CreateProcess().

不是直接运行提升的程序,而是CreateProcessElevated()依赖另一个可执行文件,该可执行文件接收stdin、stdout、stderr命名管道的名称,并在调用CreateProcess().

回答by zdan

You should replace your use of ShellExecuteEx with CreateProcess. The lpStartupInfoargument lets the std in and std out handles of the process. Just Create an anonymous pipe using CreatePipethat you pass as the arguments. This MSDN articlehas an example of how to do this.

您应该用CreateProcess替换您对 ShellExecuteEx 的使用。该lpStartupInfo参数,您在性病性病出来的过程中把手。只需使用您作为参数传递的CreatePipe创建一个匿名管道。这篇MSDN 文章有一个如何做到这一点的例子。

回答by JimR

Can you not use CreateProcess/ShellExecuteEx to exec a cmd shell with stdout/stderr redirected that in turn invokes your process?

你不能使用 CreateProcess/ShellExecuteEx 来执行一个带有 stdout/stderr 重定向的 cmd shell,反过来调用你的进程吗?

"cmd /c YourProcess.exe {parameters}" etc?

“cmd /c YourProcess.exe {parameters}”等?

回答by Byron Whitlock

You need to create a named pipe to the child process. This MSDN articleexplains with and has code samples.

您需要为子进程创建一个命名管道。这篇 MSDN 文章解释并提供了代码示例。

You should be able to get it going from that.

你应该能够从中得到它。