3

I'm trying to start an UI using CreateProcessAsUser but the window doesn't come as the foreground Window.

I wouldn't mind if it was a normal window, but this window triggers the UAC prompt and it isn't displayed in foreground neither.

Jader Dias
  • 88,211
  • 155
  • 421
  • 625
  • Are you calling `CreateProcessAsUser` in order to run the target process in elevated mode? – Billy ONeal Feb 21 '11 at 21:25
  • @Billy I'm running as `LocalSystem` and I'm starting a process as the logged user. – Jader Dias Feb 21 '11 at 21:31
  • 2
    Is this called from a service? – Hans Passant Feb 21 '11 at 21:32
  • Windows allows only Services to be run as LocalSystem. PsExec does start a system service and gives itself rights to WinSta0 to access you desktop. But normally you cannot escape session 0 isolation and you window will not be visible to your user. There is a UI dection service (http://www.alex-ionescu.com/?p=59) which notifies you when a service tried to display a windows. – Alois Kraus Feb 21 '11 at 21:46
  • @Jader: Ah, well if it's running from a service then you can't really do this at all, because services run on session 0, and no user facing desktop is allowed to use that session. Vista has a backwards compatibility shim which will allow the user to see the Session 0 desktop; this shim was removed in 7. – Billy ONeal Feb 25 '11 at 06:35
  • @Bill Yes I know, we circumvent this by creating the UI process in a session different from 0. I just have a little problem when it happens. – Jader Dias Feb 25 '11 at 11:32

1 Answers1

2

From your description, it sounds as if you are trying to launch the other process in elevated mode. If that's what you're trying to do, you cannot set it to the foreground window. In fact, non-elevated processes aren't allowed to touch elevated processes in any way/shape/form.

The created process has to be responsible for actually making its own window the foreground window. Try playing with the STARTUPINFO structure you pass to CreateProcessAsUser, setting the STARTF_USESHOWWINDOW flag, and specifying a value of SW_SHOWNORMAL. If that doesn't work, there's no real workaround other than elevating part of yourself first, such as creating an elevated COM component.

If that's not what you're trying to do, and you simply want to move the child process' window to the foreground, then you have to enumerate windows owned by the child process and then call SetForegroundWindow on one of them.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • Unfortunately the process I am trying to start triggers the UAC. And I can't bring the UAC to foreground, probably because it is protected, nor the Process' MainWindow, probably because it doesn't start until you accept the UAC. – Jader Dias Mar 03 '11 at 13:01