Open default browser as standard user (C++)

做~自己de王妃 提交于 2019-12-03 20:16:24

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:

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.

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.

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