Reputation: 8404
I have a requirement to execute several commands with elevated rights, something like:
All of this dynamic, e. g. the first line could contain proxy settings, including user name and password, or any other modification to the settings file. (Settings file is in program folder, program to modify the settings file is externally provided. The actual info would be user entered through a GUI.)
I thought to create a very flexible solution, using a cmd.exe process with redirected stdin and stdout. But.. it looks like "runas" (needs ShellExecute) and redirected in-/output are exclusive.
(Also, I noticed that redirection and cmd.exe are quite hard to handle.. I had to write my own functions e. g. as a replacement for ReadLine, since the ReadLine expects a NewLine at the end and waits e. g. if there is a command prompt (meaning you have text in the line like c:\bla> but no NewLine. Still, not knowing what one gets as a result makes writing something generic difficult.)
There are 3 alternatives I can think of:
How to handle this?
Upvotes: 0
Views: 433
Reputation: 612954
I think you've already diagnosed the issue, and enumerated your available options. To my mind the best option is to use runas
to create a new elevated process, and get that process to do the work.
The other process need not be a different executable it could be your existing executable started with particular command line arguments. Once you have another process running elevated, you can certainly use cmd
with redirected stdin/stdout to do the work. However, why use net stop/start
rather than the service API? And can you do the .config
file modification without starting a separate process?
Upvotes: 1