Freigeben über


Trigger-Start Services with Windows Server 2008 R2

As the name implies it, Trigger-Start Services are not constantly running on the system but rather start when a specific event in the system triggers the service to start and/or respond. The events that can be triggered can be any of the following:

· Device interface arrival: By subscribing to this trigger, any service can take action to, for example, starting the scanner service when a scanner-capable printer is connected to the machine.

· Domain join or leave: In Enterprise scenarios, this trigger will fire off when the machine is joins or leaves a domain.

· Firewall port opened or closed: If a service needs to run when a specific port is opened, then it can be triggered by that specific event. Likewise, when a port is closed a service would not be able to function properly, then it only makes sense to stop the service until the port is available again.

· Group policy updates: Another trigger that is targeted towards Enterprise scenarios. Whenever a group policy changes (for example, a policy that enforces Bit Locker technology) services that should be running based on a specific group policy could be started. Likewise, when a service does not need to run because the Group Policy does not apply, then it can be stopped to preserve resources.

· IP address: The arrival and departure notification of this trigger can start or stop the service when an IP address is acquired or when it is lost. In the case that someone carries a laptop with them on a place, those services that rely on Internet connectivity could be stopped; thus maximizing resource utilization and extending the battery life.

· Custom ETW event: In case that a service needs to be triggered by a custom event not previously covered, then developer can be the provider themselves from wherever in the system they want to provide this custom event. The service can then depend on that Event Tracing for Windows (ETW) event and be triggered to start or stop.

The Service Control Manager (SCM) registers for system events with the providers described above. Whenever and event is triggered, the SCM starts or stops the service.

Using the API to Set the Service to be Trigger Started

The code below shows the required steps to set a service to be trigger-started by using the ChangeServiceConfig2 API call.

//Initialize the Structure

SERVICE_TRIGGER trigger = {0};

//Set the Trigger Type to be of Type IP ADDRESS AVAILABLE

trigger.dwTriggerType =SERVICE_TRIGGER_TYPE_IP_ADDRESS_AVAILABILITY;

//The action that you want to do, in this case Start the Service

trigger.dwAction = SERVICE_TRIGGER_ACTION_SERVICE_START;

//Sub-Type that specifies that we want to be triggered when the machine aquires a IP

trigger.pTriggerSubtype = (GUID*)&NETWORK_MANAGER_FIRST_IP_ADDRESS_ARRIVAL_GUID;

SERVICE_TRIGGER_INFO info = {0};

info.cTriggers = 1;

info.pTriggers = &trigger;

//Change the Service Configuration

ChangeServiceConfig2(hService,SERVICE_CONFIG_TRIGGER_INFO, &info);

For more information on using the API, please check the Service Trigger Events page on MSDN.   Watch for video demonstrations at MSDN Channel9.