Open default browser as standard user (C++)

纵然是瞬间 提交于 2019-12-21 05:42:10

问题


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?


回答1:


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:

  • LogonUser if you know the username and password
  • WTSQueryUserToken for any given session ID.
  • OpenProcessToken if you have another process handle



回答2:


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.

  1. read from "HKEY_CURRENT_USER\Software\Clients\StartMenuInternet"
  2. read command line from "HKEY_LOCAL_MACHINE\Software\Clients\StartMenuInternet\\shell\open\command"
  3. truncate the command line after ".exe"

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.




回答3:


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.



来源:https://stackoverflow.com/questions/2308439/open-default-browser-as-standard-user-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!