在C ++中阅读另一个进程的标准输出 [英] Read another process' stdout in C++

查看:63
本文介绍了在C ++中阅读另一个进程的标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows中,有没有一种方法可以用C ++启动进程,然后在完成后将其读出到stdout中?如有必要,必须使用提升的特权(在Vista或更高版本上)运行该过程.

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.

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

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

推荐答案

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

There's no easy way to do this.

使用 runas 动词调用 ShellExecuteEx()将RPC消息发送到 AppInfo NT Service 巫婆,然后从提升的会话中运行该应用程序.没有API可以轻松将提升流程的输入/输出连接到您的应用程序.

Calling ShellExecuteEx() with the runas verb sends an RPC message to the AppInfo NT Service witch 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.

Thomas Hruska在他的代码项目文章中介绍了他的实现解决此问题的 CreateProcessElevated()函数.

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

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

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

这篇关于在C ++中阅读另一个进程的标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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