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 lpExitCo开发者_如何学编程de parameter (with a WaitForSingleObject() call doing a 100 msec wait during each iteration).
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()
.
You should replace your use of ShellExecuteEx with CreateProcess. The lpStartupInfo argument lets the std in and std out handles of the process. Just Create an anonymous pipe using CreatePipe that you pass as the arguments. This MSDN article has an example of how to do this.
You need to create a named pipe to the child process. This MSDN article explains with and has code samples.
You should be able to get it going from that.
Can you not use CreateProcess/ShellExecuteEx to exec a cmd shell with stdout/stderr redirected that in turn invokes your process?
"cmd /c YourProcess.exe {parameters}" etc?
精彩评论