Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process.Start never returns when UAC denied

I have an updater exe that is meant to close the primary exe, replace it with an updated exe, and then launch that updated exe. When the updater attempts to start the updated exe, if the UAC permissions dialog is denied by the user, the updater will hang. This is because the Process.Start() function never returns. My CPU cycles meter indicates practically no usage btw.

I would hope all my users just say "yes" to the UAC, but since I'm here I'd like to handle this case with some kind of error message at least. Assume my users will have at least Windows 7. The exes themselves are 32 bit Winforms applications. Targeted .Net Framework is 4.0. Using Visual Studio 2010 Ultimate.

Any ideas on how to detect for when my user declines the UAC dialog?

I'm guessing all I can do is make the Process.Start() run on a separate thread that will timeout after a while. For more code:

private void RestartProcess()
{
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = @"C:\Users\Me\Documents\Visual Studio 2010\Projects\updated.exe";
    MessageBox.Show("Attempting to start process");
    Process newProc = Process.Start(startInfo);
    MessageBox.Show("If this shows, the user has clicked YES in the UAC.");
}

Solution:

Process.Start()exits silently with a Win32Exception unless one uses a Try{}Catch{} block to catch the error.

like image 321
Christopher Caldwell Avatar asked Apr 28 '14 20:04

Christopher Caldwell


1 Answers

   Process newProc = Process.Start(startInfo);
   MessageBox.Show("If this shows, the user has clicked YES in the UAC.");

This is normal, the exception that's raised by Process.Start() will bypass the MessageBox.Show() call. It is a Win32Exception for Windows error code 1223, ERROR_CANCELLED, "The operation was cancelled by the user".

Clearly you'll want to avoid swallowing exceptions here.

like image 141
Hans Passant Avatar answered Oct 31 '22 23:10

Hans Passant