5

Recently I bumped into an issue where I need to open a webpage from an elevated application. I need the browser to be opened non elevated so I looked around and found this solution

This would definitely solve my issue except that CreateProcessAsUser does not accept URIs as a filename, the execution will fail with the error message being "The filename, directory name, or volume label syntax is incorrect"

Can someone tell me if there is a way to launch a website through CreateProcessAsUser or if there is a similar API that does the job.. Please note that my actual application is always elevated and thus I cannot uses the usual Process.Start(), CreateProcess() or ShellExecute()

Community
  • 1
  • 1
Zaid Amir
  • 4,727
  • 6
  • 52
  • 101

2 Answers2

3

There are two distinct families of functions that you will need to use to get this task done:

  1. CreateProcess and related functions. These can be used to start a new process. You must supply a filename of an executable image.
  2. ShellExecute and friends. These will apply verbs to files using the shell's rules for associating file classes to executables.

In order to open a URI you need to use one of the ShellExecute family. And in order to execute as a standard user from your elevated process, you need to use one of the CreateProcess family.

You cannot meet both your requirements with a single function. Your solution therefore is to use one of the CreateProcessXXX functions to create a process that runs as standard user. That process in turn will call ShellExecuteXXX. You will pass the URI as command line arguments to CreateProcessXXX.

You will therefore need to either create a distinct executable as your standard user launcher. Or re-use your existing executable but make it switch into launcher mode depending on the arguments that you pass it.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I am aware of that solution and was hoping for something else... will wait and see if someone can come up with something different – Zaid Amir Feb 21 '13 at 08:43
  • 1
    That's the only solution, unless you wish to re-implement `ShellExecuteXXX`. Lots of people do that, poking at the registry, but they always get it wrong in some detail or other! – David Heffernan Feb 21 '13 at 09:03
0

I used CreateProcessAsUser() with CommandLine like cmd /c start myurl:, then there is no need to create another executable just to call the shell execute.

kimmoli
  • 111
  • 1
  • 4