1

I need to start a subprocess with elevated privileges from C++ code. I'm not familiar with Windows APIs at all, and from cursory googling I found out that
a) to start an elevated process I need to use ShellExecute with the runas verb.
b) to communicate with subprocess via stdin/stdout I have to use CreateProcess

But I haven't found a way to do both at the same time.

Additionally, I need to

  • either get a callback when the process finishes, or poll it to see whether it's still running

  • check its return code

As I said, I'm completely unfamiliar with Windows API, so could someone point me to the right functions?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
user697683
  • 1,423
  • 13
  • 24
  • you need set `SEE_MASK_NOCLOSEPROCESS` in `SHELLEXECUTEINFO.fMask` and use `SHELLEXECUTEINFO.hProcess` with some wait functions and finally call `GetExitCodeProcess` and `CloseHandle` - about stdin/stdout - this is impossible by design with elevated process – RbMm Jul 12 '17 at 17:09
  • Time to graduate from "cursory googling" to actually studying the documentation. This website is not a replacement for that act. Start here: https://msdn.microsoft.com/en-us/library/windows/desktop/ff818516(v=vs.85).aspx – Lightness Races in Orbit Jul 12 '17 at 17:41
  • duplicate of: https://stackoverflow.com/q/30970718/103167 – Ben Voigt Jul 12 '17 at 17:51
  • I suggest to read up on [named pipes API](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365590(v=vs.85).aspx) which could be used quite similar to stdin/stdout. [Example code](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365799(v=vs.85).aspx). – zett42 Jul 12 '17 at 18:14
  • 2
    See [`CreateProcessElevated()`](https://www.codeproject.com/Articles/19165/Vista-UAC-The-Definitive-Guide), which supports starting an elevated process while redirecting std(in|out|err) using named pipes. – Remy Lebeau Jul 12 '17 at 18:25

0 Answers0