Elevating Privileges Doesn't Work with Useshellexecute=False

Elevating privileges doesn't work with UseShellExecute=false

ProcessStartInfo.Verb will only have an effect if the process is started by ShellExecuteEx(). Which requires UseShellExecute = true. Redirecting I/O and hiding the window can only work if the process is started by CreateProcess(). Which requires UseShellExecute = false.

Well, that's why it doesn't work. Not sure if forbidding to start a hidden process that bypasses UAC was intentional. Probably. Very probably.

Check this Q+A for the manifest you need to display the UAC elevation prompt.

Allow UAC window when CreateNoWindow is set

Instead of starting the 'target' app directly like this, instead create a small 'helper' app that does nothing but start the target app with CreateNoWindow.

Then you start your 'helper' app with 'runas' and without CreateNoWindow, since your 'helper' app won't create any windows that's not a problem, and since it was run with 'runas' it'll be able to run other elevated programs without 'runas' itself - and can therefore use CreateNoWindow.

C# Start Program with ADMIN rights from other program

Try adding this line

process.StartInfo.UseShellExecute = true;

on

using (var process = new Process())
{
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = $".\\ServiceControl.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Arguments = $"-s {ServiceName} -start";
process.StartInfo.Verb = "runas";
process.Start();
}

Redirect standard output and prompt for UAC with ProcessStartInfo

UseShellExecute must be set to false to redirect IO, and to true to use the Verb property. So you can't.

But this article seems do the magic, although I haven't tested it.

It's written in C++, but a wrapper API can easily be created to be called from C# by using DllImport.


Note: If you want to pass data between the two programs and have access to the target program's source code, you can easily re-design you application to use Named Pipes instead of redirecting standard I/O.

Verb = runas not run as elevated

Process.Start() can use the OS or Shell (Explorer.exe) to spawn a new process, but only the Shell will prompt for elevation.

Thus you have to specify UseShellExecute=true in ProcessStartInfo, according to this answer: processstartinfo-verb-runas-not-working

UseShellExecute=false will allow you to capture Standard Output and Standard Error messages.



Related Topics



Leave a reply



Submit