skip to Main Content

I have:

  1. Simple WPF app, with 1 window created by Visual studio
  2. Windows service (created with topshelf) that starts that app

If I start app manually, it appears in task manager and shows form as it should.

But when it is started by windows service, app appears in task manager, but form isnt shown. What should it the reason?

App launch code in win service

                    System.Diagnostics.Process process = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                startInfo.FileName = _app.MainAppDirectory;
                startInfo.Arguments = "";
                process.StartInfo = startInfo;
                process.Start();

2

Answers


  1. You can’t do it. Windows services are not running under current user session. When you login to your Windows, you will get an active session including (Desktop, Environment variables, ..) and whenever you run an application (like a Simple WindowsForm or WPF, …) it will runs under your active session. You can open up the Task manager and navigate to Detail tab to see information about the user who ran each Process (Check this for the WPF app when it’s ran by your windows service).

    You can type Services on the Start menu and open up Services window and find your installed service. You can check a column named Log on As there. It’s the user account who runs your Windows service (it might be Local System or Local Service).

    It’s an interesting fact, you can write an application who runs even with no logged-in user.

    I had the same task, 5 years ago, I tried to steal users session. Using Impersonation you are able to steal user token and run your application as an specific user. But still you are not able to see your Application on the desktop.

    Look for a different project type to fill your requirements. maybe mark your application as an startup app and hide it’s Forms from the user.

    Login or Signup to reply.
  2. Windows services run in session 0, which isolates services from user applications. This quote from the article gives more details on why they separated user applications from session 0.

    In Windows XP, Windows Server 2003, and earlier versions of the Windows operating system, all services run in the same session as the first user who logs on to the console. This session is called Session 0. Running services and user applications together in Session 0 poses a security risk because services run at elevated privilege and therefore are targets for malicious agents who are looking for a means to elevate their own privilege level.

    There used to be a means to do what you are trying to do by making a interactive service but the first important section in that document states.

    Services cannot directly interact with a user as of Windows Vista. Therefore, the techniques mentioned in the section titled Using an Interactive Service should not be used in new code.

    As far as I know, the checkbox is still there to make a service interactive but is only there for compatibility with installers that might have set it or read it.

    enter image description here

    That image is from my Windows 10 machine so the option to turn it on is there but like I said, I am under the impression it does nothing other than prevent applications which attempted to set it from breaking.

    Typically the way I have seen applications work around this limitation is by writing a corresponding desktop app that is installed to launch at start and then uses some form of inter-process communication, like a socket to communicate with the app.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search