How to create a trigger-start Windows service in Windows 7


Windows Server 2008 R2 and Windows 7 introduce a new feature that is known as the trigger-start service. A Windows service can be registered as a trigger-start service by using the following methods:
  • Use the Sc.exe utility together with the Triggerinfo option
  • Use the ChangeServiceConfig2 API programmatically.
This article describes an All-In-One Code Framework sample that is available for download. This sample includes a step-by-step guide that shows how to programmatically create a trigger-start service in Windows 7.

Note This code sample that is described in this article is not meant for use in a production environment. It is provided only for illustration. This code sample is released under the terms of the Microsoft Public License (MS-PL).

Difficulty level

Download information

To download this code sample, click the following link:

Technical overview

Services and background processes have great influence on the overall performance of the system. If you reduce the total number of services, you reduce the total power consumption and increase the overall stability of the system.

In Windows Server 2008 R2 and in Windows 7, the Service Control Manager is extended so that a service can be automatically started and stopped when a specific system event or a specific trigger occurs on the system. This mechanism is called a service trigger event. A service can register to start or to stop when a service trigger event occurs. This feature eliminates the need for services to start when the operating system starts. Therefore, this feature prevents a situation in which a service is started even though there is no work for the service to perform. For example, you can define a service trigger event that occurs when a device is enabled by a specified device interface class. Or, you can define a service trigger event that occurs when a particular firewall port is available. A service can also register for a custom service trigger event that is generated by an Event Tracing for Windows (ETW) provider.

This code sample shows how to create a trigger-start service that starts when a generic USB disk becomes available. The sample also shows how to create a trigger-start service that starts a service when the first IP address on the TCP/IP networking stack becomes available. The sample also shows how to create a trigger-stop service that stops a service when the last IP address on the TCP/IP networking stack becomes unavailable. These start and stop events are reported in the Application log.

Note For more information about how to create the sample application and how to deploy the sample application, see the Readme.txt file that is included in the download package.

Technology category

  • Windows 7


This sample contains code samples in the following programming languages:
LanguageProject Name
Visual C++CppWin7TriggerStartService
Visual C#CSWin7TriggerStartService
Visual Basic .NETVBWin7TriggerStartService


  • You must run this code sample on a Windows Server 2008 R2 or Windows 7-based computer.

    Note Service trigger events are not supported in versions of Microsoft Windows earlier than Windows Server 2008 R2 and Windows 7.
  • This sample application was created by using Microsoft Visual Studio 2008 with Service Pack 1 installed.
  • To build the CppWin7TriggerStartService project, you must have Microsoft Windows SDK for Windows 7 and the .NET Framework 3.5 Service Pack 1 installed.


  • Windows 7, Service Trigger Events, Windows Service

More Information

What is All-In-One Code Framework?

All-In-One Code Framework shows most Microsoft development techniques by using code samples in different programming languages. Each example is carefully selected, composed, and documented to show one common code scenario. For more information about All-In-One Code Framework, visit the following Web site:

How to find more All-In-One Code Framework samples

To find more All-In-One Code Framework samples, you can search for kbcodefx together with related keywords on Or, you can simply click the following link:


For more information about service trigger events, visit the following Microsoft Developer Network (MDSN) Web site:
For more information about Windows SDK for Windows 7, visit the following Microsoft Web site:
For more information about the Microsoft Public License, vist the following Web site:

Artikel-id: 975425 - Laatst bijgewerkt: 30 dec. 2009 - Revisie: 1