views:

200

answers:

4

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.

+3  A: 

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
A: 

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
A: 

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
A: 

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