1

Is it possible to launch a web browser from a windows service? I have created a basic service in C# and installed it under the "LocalSystem" security profile.

The code for the service looks as follows:

namespace Bootloader
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            string target = "http://www.microsoft.com";
            System.Diagnostics.Process.Start(target);
        }

        protected override void OnStop()
        {
        }
    }
}

When the service runs, nothing happens. The documentation on windows service say that they do not have any UI, but does that mean launching a web browser is not possible.

mon4goos
  • 1,569
  • 13
  • 24
  • 2
    I am curious what you are trying to accomplish. Do you need your windows service to interact with the currently logged on user? If so, why is it a windows service? What happens when there is no user logged on? Why does a background service need to interact with a user? etc. ad inf. – jrista Feb 24 '10 at 23:43
  • What are you expecting to happen? Depending on the version of Windows you're running on, the browser window may, or may not, not appear. Having a service provide UI is a **bad thing** ... Google "shatter attack" – Rob Feb 24 '10 at 23:57
  • I'm working on a program that will run in the background listening on a port that will pop up an internet browser and other UI when it receives certain commands. It sounds like an application with a hidden windows is the way to go. – mon4goos Apr 06 '10 at 00:09
  • possible duplicate of http://stackoverflow.com/questions/267838/how-can-a-windows-service-execute-a-gui-application – Rowland Shaw Apr 12 '10 at 12:25

4 Answers4

5

It's possible only in XP and lower. In Vista, Windows Services run on a separate desktop completely. You'll have to have something running in the user's desktop to accomplish this.

Write an app with a hidden window that starts at startup as a workaround.

David Morton
  • 16,338
  • 3
  • 63
  • 73
0

I dont think this is possible. I know that if you want to run Watin (functional tests that run in a browser instance) cannot be run from my CI environment, if this is running as a service, but only if it runs as an app.

Luhmann
  • 3,860
  • 26
  • 33
0

I believe it can be done, but you'll need to do extra work in order to deal with the process isolation model (window stations and desktops). Take a look at this page: Process Connection to a Window Station. Since you can't modify the browser, you may need to write a shim that changes the context and then invokes the browser.

A workaround is to run your service as an interactive service, but this is deprecated and won't work in newer versions of Windows.

jdigital
  • 11,926
  • 4
  • 34
  • 51
0

Services are explicitly forbidden from interacting with the user. Since Vista this is enforced, see Interactive Services:

Important 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.

The solution is to separate the inteactive part into a normal process that is launched when the user session starts (ie. a Start-Up program). This process can then communicate with the service via its IPC of choice (shared memeory, net pipes, TCP etc). The service can direct this process to start programs when needed.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569