How to (if at all possible) bring the Vista/Win7 UAC consent dialog to the foreground, when it is minimized in the taskbar?
For instance, consider the following scenario:
My application checks for updates during startup, it downloads the new update file and it executes it by providing the administrator's password in the Process.StartInfo, after that my application closes.
At this point, if the user or Windows itself manages to lose focus from the MSI installer window (perhaps by clicking on the desktop or another window), UAC sees that installer window is not the foreground window and thus pops a blinking consent dialog into the taskbar.
Some not so brightminded customers don't understand that my application hasn't yet finished updating and try to relaunch the application. At this stage, I can enumerate the running processes and find the consent.exe which is blinking in the taskbar.
The problem is I can't bring it to the foreground. I tried to invoke ShowWindow() from user32.dll with different paramaters (restore, show, normal), but nothing happens. I did check the process MainWindowHandle and it seems ok (it isn't zero or negative). I'm guessing that the problem lies with UAC creating a different desktop session for the consent dialog (secure desktop).
If the user can click the blinking icon in the taskbar to bring the consent dialog to the foreground, then it should also be possible to simulate this via code?
PS! I'm using C#
Did you try SetForegroundWindow ? Also, in native win32 there is not really anything called a main window, a process can have 0 or any number of "main windows" but in the case of consent.exe, I'm guessing it has just one...
Edit:
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
char buf[50];
*buf=0;
GetClassName(hwnd,buf,50);
buf[sizeof("$$$Secure UAP")-1]=0;
if (!lstrcmp("$$$Secure UAP",buf))
{
SwitchToThisWindow(hwnd,true);
}
return true;
}
...
EnumWindows(EnumWindowsProc,0);
This will switch to the secure desktop, but I would not recommend using this because:
The problem is that the consent window is not a owned popup, if it was you could be sure you got the correct HWND. Hopefully Win8 will have consent act as a owned popup, or even better, a modal dialog.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With