Search code examples
c++windowsservicedesktopaccount

How to launch application on non-logged on user's desktop from service on Windows


I am trying to create a service which does following:

  1. Logging in as a user with specified username/password
  2. Running an application on the desktop of logged in user of step 1

Note: before step 1, user is not logged on. (something like right after machine is rebooted)

The expected result is, the user should be able to see the UI of launched app at step 2 if user logged onto the desktop with the user account logged on at step


I found this article, but this code will launch the app on current desktop (let's say, currently logged on as userA. Using username/password of userB, the app is still be launched on userA desktop, but using userB's account).

https://support.microsoft.com/en-ca/help/165194/createprocessasuser-windowstations-and-desktops


Please let me know what's the correct way to achieve my goal.

IDE: Visual Studio 2015 C++ on Windows.


Solution

  • Let me answer to my question.

    The bottom line is:

    • I cannot launch a process on a desktop of not-logged-on user by programmatically logging in.

    Reason:

    • Calling certain function such as LogOnUser, I can login and access to resources related to user's account.
    • However, the logon session is different from a Session which will be created when user logs on from login screen.
    • Because of security reason, once a process is launched, we cannot move the process to another Session. So, UI will never be able to show on the desktop after the user logs on from login screen.

    Alternative solution:

    • Use auto-logon
    • Redesign app, and split UI and its data. So, a process with data can run as service, and UI can launch later on a desktop.

    The details explanation of how Windows Session, Window Station, and Desktop works: https://brianbondy.com/blog/100/understanding-windows-at-a-deeper-level-sessions-window-stations-and-desktops

    Thank you very much for Harry Johnston for detailed explanation in the comment.