Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read another process' stdout in C++

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.

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

like image 906
RobH Avatar asked Nov 04 '10 01:11

RobH


1 Answers

There's no easy way to do this.

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 in his The Code Project article presents his implementation of a CreateProcessElevated() function that solves this.

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

like image 127
Alex Jasmin Avatar answered Oct 06 '22 00:10

Alex Jasmin