开发者

How to bring UAC's consent.exe to the foreground programmatically?

开发者 https://www.devze.com 2023-01-31 21:22 出处:网络
How to (if at all possible) bring the Vista/Win7 UAC consent dialog to the foregr开发者_如何学Goound, when it is minimized in the taskbar?

How to (if at all possible) bring the Vista/Win7 UAC consent dialog to the foregr开发者_如何学Goound, 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:

  • It uses the undocumented $$$Secure UAP... window class
  • Using SwitchToThisWindow is evil, you should never switch focus when the user does not want to
  • If more than one UAC confirmation dialog is active, you can't be sure which consent.exe you will be switching to

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.


There is a discussion here that may help you: How do you maximize User Account control(UAC) window?

The official documentation about that subject is here: Redesign for UAC Compatibility (UAC)

0

精彩评论

暂无评论...
验证码 换一张
取 消