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

Article translations Article translations
Article ID: 975425 - View products that this article applies to.
Expand all | Collapse all

On This Page

INTRODUCTION

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

Collapse this imageExpand this image
codefx Medium

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

Languages

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

Prerequisites

  • 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.

Tags

  • 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:
http://cfx.codeplex.com

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 support.microsoft.com. Or, you can simply click the following link:http://support.microsoft.com/search/default.aspx?query=kbcodefx

REFERENCES

For more information about service trigger events, visit the following Microsoft Developer Network (MDSN) Web site:
http://msdn.microsoft.com/en-us/library/dd405513(VS.85).aspx
For more information about Windows SDK for Windows 7, visit the following Microsoft Web site:
http://www.microsoft.com/downloads/details.aspx?familyid=71DEB800-C591-4F97-A900-BEA146E4FAE1&displaylang=en
For more information about the Microsoft Public License, vist the following Web site:
http://opensource.org/licenses/ms-pl.html

Properties

Article ID: 975425 - Last Review: October 15, 2009 - Revision: 1.6
APPLIES TO
  • Microsoft Visual Studio 2008 Academic Edition
  • Microsoft Visual Studio 2008 Professional Edition
  • Microsoft Visual Studio 2008 Standard Edition
  • Microsoft Visual Studio Team System 2008 Database Edition
  • Microsoft Visual Studio Team System 2008 Architecture Edition
  • Microsoft Visual Studio Team System 2008 Development Edition
  • Microsoft Visual Studio Team System 2008 Test Edition
  • Microsoft Visual Studio Team System 2008 Team Suite
Keywords: 
kbcodefx atdownload kbsurveynew kbinfo KB975425

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com