以标准用户身份打开默认浏览器 (C++)
我目前正在使用 ShellExecute“打开”在用户浏览器中打开 URL,但在 Win7 和 Vista 中遇到了一些麻烦,因为该程序作为服务运行提升。
当 ShellExecute 打开浏览器时,它似乎读取“本地管理员”配置文件而不是用户的配置文件。例如,如果键盘上的用户将 Firefox 作为默认浏览器,则可能会打开 IE(这是管理员的默认浏览器)。
我知道“runas”动词可以用来提升,但是反过来怎么做呢?有没有办法通过服务在标准用户桌面上的默认浏览器中打开 URL?
I'm currently using ShellExecute "open" to open a URL in the user's browser, but running into a bit of trouble in Win7 and Vista because the program runs elevated as a service.
When ShellExecute opens the browser, it seems to read the "Local Admin" profile instead of the user's. So for example, if the user at the keyboard has Firefox as his default browser, it may open IE (which is the admin's default).
I know that the "runas" verb can be used to elevate, but how do you do it the other way around? Is there a way to open a URL in the default browser on a standard user's desktop from a service?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ShellExecute 将在与您正在运行的进程相同的会话和相同用户的上下文中执行程序。
如果您想使用不同的会话或用户令牌,可以使用 CreateProcessAsUser Win32 API。
有多种方法可以获取用户令牌,例如您可以调用 Win32 API:
ShellExecute will execute the program in the context of the same session and same user as the process you are running.
If you'd like to use a different session or user token you can use the CreateProcessAsUser Win32 API.
There are several ways to obtain a user token, for example you can call the Win32 API:
经过一段时间的测试,确定默认浏览器的最佳方法如下:
注意:这很奇怪,但确实如此......
没有什么可说的,应用程序是默认应用程序
某些文件类型或网络协议,例如“http”。确定默认值的重要因素
Web 浏览器正是在开始菜单条目中注册的内容(请参阅下面的 reg 键)。
所以忘记所有 HKCR\http、HKCU\Software\Classes\http、HKLM\Software\Classes\http 及其朋友。
当然你需要模拟登录用户至上。
如果此解决方案不起作用(如 w2k),请使用 http 协议的处理程序。
为了实际从服务启动默认浏览器,我们使用一个额外的进程,该进程位于服务内,使用登录的用户上下文。此过程启动上述命令行(在平台 >= Vista 上使用 ShellExecute)。确保使用与默认用户相同的完整性级别(中)(否则 IE 将无法工作,因为它使用 DDE)。
HTH。
After a while of testing, the best way to determine the default browser is the following:
NOTE: It is strange but it's true...
It has nothing to say that an application is the default application for
some file type or web protocol like 'http'. What matters to determine the default
web browser is just what is registered in the start menu entry (see reg key below).
So forget all the HKCR\http, HKCU\Software\Classes\http, HKLM\Software\Classes\http and their friends.
Of course you need to impersonate as the logged on user first.
If this solution does not work (like with w2k), use the handler for the http protocol.
To actually start the default browser from a service we use an extra process which is within the service using the logged on user-context. This process starts the above commandline (using ShellExecute on platforms >= Vista). Be sure to use same integrity level (medium) as a default user (else IE won't work because it uses DDE).
HTH.
Aaron Margosis 有一个七步本机代码示例,位于 http://blogs.msdn.com/aaron_margosis/archive/2009/06/06/faq-how-do -i-start-a-program-as-the-desktop-user-from-an-elevated-app.aspx。如果您拥有这样的服务,则不会为您提供帮助 - 我同意您的服务不应尝试以登录用户身份启动应用程序,特别是因为可能没有应用程序。
Aaron Margosis has a seven-step native code example at http://blogs.msdn.com/aaron_margosis/archive/2009/06/06/faq-how-do-i-start-a-program-as-the-desktop-user-from-an-elevated-app.aspx. Won't help you from your service if that is what you have - I agree your service shouldn't be trying to launch an app as the logged in user, especially since there might not be one.