如何以编程方式将UAC的consent.exe带到前台? [英] How to bring UAC's consent.exe to the foreground programmatically?

查看:23
本文介绍了如何以编程方式将UAC的consent.exe带到前台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 Vista/Win7 UAC 同意对话框在任务栏中最小化时,如何(如果可能)将其置于前台?

例如,考虑以下场景:
我的应用程序在启动期间检查更新,它下载新的更新文件并通过在 Process.StartInfo 中提供管理员密码来执行它,然后我的应用程序关闭.

此时,如果用户或 Windows 本身设法从 MSI 安装程序窗口失去焦点(可能是通过单击桌面或其他窗口),UAC 会看到该安装程序窗口不是前台窗口,因此会弹出一个闪烁的同意对话框进入任务栏.

一些不太聪明的客户不明白我的应用程序尚未完成更新并尝试重新启动应用程序.在这个阶段,我可以枚举正在运行的进程并找到在任务栏中闪烁的consent.exe.

问题是我不能把它带到前台.我尝试使用不同的参数(恢复、显示、正常)从 user32.dll 调用 ShowWindow(),但没有任何反应.我确实检查了 MainWindowHandle 进程,它看起来没问题(它不是零或负数).我猜问题在于 UAC 为同意对话框(安全桌面)创建了不同的桌面会话.

如果用户可以点击任务栏中闪烁的图标将同意对话框带到前台,那么应该也可以通过代码来模拟吗?

附上!我正在使用 C#

解决方案

您尝试了吗 SetForegroundWindow ?此外,在本机 win32 中,实际上并没有任何称为主窗口的东西,一个进程可以有 0 个或任意数量的主窗口",但在 Consent.exe 的情况下,我猜它只有一个......

BOOL 回调 EnumWindowsProc(HWND hwnd,LPARAM lParam){字符缓冲区[50];*缓冲=0;GetClassName(hwnd,buf,50);buf[sizeof("$$$Secure UAP")-1]=0;if (!lstrcmp("$$$Secure UAP",buf)){SwitchToThisWindow(hwnd,true);}返回真;}...EnumWindows(EnumWindowsProc,0);

这将切换到安全桌面,但我不建议使用它,因为:

  • 它使用未公开的 $$$Secure UAP... 窗口类
  • 使用 SwitchToThisWindow 是邪恶的,当用户不想切换焦点时,你不应该切换焦点
  • 如果有多个 UAC 确认对话框处于活动状态,您将无法确定将切换到哪个 Consent.exe

问题是同意窗口不是自有弹出窗口,如果是,您可以确定您获得了正确的 HWND.希望 Win8 将同意作为一个拥有的弹出窗口,甚至更好的模式对话框.

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:

  • 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.

这篇关于如何以编程方式将UAC的consent.exe带到前台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆