Search code examples
c++windowsuser-interfaceserviceimpersonation

Give focus to a Window launched from a system service using CreateProcessAsUser


I have a System service that launch an application using the method CreateProcessAsUser.

This service uses impersonation to launch the app in the active session. The Application is launching as it should, I managed to make the window appears on the front by using :

SetWindowPos( &this->wndTopMost, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE );

But I don't know what to do so the app can have the keyboard focus. Even if the app is on top, as long as the user hasn't clic on the window, the keyboard input goes to the application below my app.


Solution

  • Let your application register a (carefully chosen) HotKey via RegisterHotKey and then simulate the key via SendInput

    In the processing of the WM_HOTKEY message, you should be able to "steal the focus".

    Remenber: It's BAD to steal focus, but I fully understand the "what my client is asking!" part.