Launching Web Browser from Windows Service

你说的曾经没有我的故事 提交于 2019-12-11 09:20:03

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/2330519/launching-web-browser-from-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!