Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ensuring a windows service will run before logon

I built an application that is running as a windows service and is installed through my code.

All is fine except at logon.

When at the first windows xp/2003 server logon screen, I am not sure if the service is running at all. If it is, then it does work as it's not functional (the service IS USING WINPCAP so that could be an issue).

The service settings are set to "interact with desktop" and run as SYSTEM.

How can I ensure the service will start before windows logon? Also how can I make sure it is running even after I log off?

like image 776
EricBenDavid Avatar asked Oct 26 '10 18:10

EricBenDavid


People also ask

Do Windows Services run when logged out?

Definition of Windows Services Unlike regular software that is launched by the end user and only runs when the user is logged on, Windows Services can start without user intervention and may continue to run long after the user has logged off.

How do I assign a Log on to a service to the service account on this computer?

Go to Administrative Tools, click Local Security Policy. Expand Local Policy, click User Rights Assignment. In the right pane, right-click Log on as a service and select Properties. Click Add User or Group option to add the new user.


1 Answers

There are a couple of issues to consider.

  1. First, you can check if your service really is running before login and after logout by logging events to the Windows Event Log. Pretty much all services do this whenever they start and stop and yours should do the same.

  2. It may be that WinPcap is part of the problem. There are a couple of golden rules for using WinPcap in a service.

    1a) Your service must not do anything that might cause the WinPcap service to try to start up while your own service is starting up because this will cause a deadlock in the Windows Service Control Manager. That means that if the WinPcap service is not already SERVICE_RUNNING when your service begins startup, you must not do anything that might cause it to start until after your service is SERVICE_RUNNING.

    There are two ways to ensure this. Either make your service dependent on npf, the Network Packet Filter service. Or do not call any WinPcap function until after your service is SERVICE_RUNNING. I've not tried this latter method. I presume then the WinPcap function will block until npf is SERVICE_RUNNING.

    1b) If you make your service dependent on npf, you will also have to make it dependent on nm (Network Monitor Driver) - if and only if nm is installed on the system. nm provides WinPcap with PPP/VPN support, and WinPcap always tries to use it if installed. Obviously, if you make nm a dependency of your service and nm isn't installed then your service will fail to start.

  3. I don't think there is a guaranteed way to ensure that your service starts up before the desktop appears. But you might be able to help things along by creating a Service Control Group, adding it to the end of the existing list of Service Control Groups, and putting your service into this group. I'm not entirely convinced that this is an 'approved' way to get your service to start sooner, because if there was an approved way then everyone would do it and it wouldn't work any more. But there is a suggestion that services in a group are started before services not in a group.

    Look at HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Control\GroupOrderList" and HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Control\ServiceGroupOrder" and do a bit of Googling.

like image 121
Ian Goldby Avatar answered Sep 27 '22 20:09

Ian Goldby