Writing a Managed Windows Service with Visual C++
- Defining the Windows Service Project
- Adding Some Simple Test Code
- Registering the Service
- Creating the Test Program
- Bottom Line
A Windows service is an application that sits in the background, waiting for you to call it or for some system event to happen. The Services console, found in the Administrative Tools folder of the Control Panel, is packed with examples of these applications. A quick look will show you that they perform a wide range of taskseverything from keeping track of system events to providing plug-and-play support. You probably have a Windows service monitoring your uninterruptible power supply (UPS), and another just waiting to create a dial-up connection should you need it.
In short, Windows services are very useful applications. Unfortunately, until now, writing a Windows service using any language was difficult (perhaps impossible) for many people. Visual Studio .NET takes a lot of the pain out of writing a Windows service by providing an object model for the code and wizards to create the project. This article gets you going with a basic Windows service and an application that calls it.
Although you'll find services in every version of Windows, I tested the example in this article to run on NT-based systems, including Windows 2000 and Windows XP. You can use any version of Visual C++ .NET to write the code in this example.
Defining the Windows Service Project
The first task you need to accomplish is creating a Windows service project. This project relies on a wizard that creates the basic infrastructure for you. The following steps describe how to create the Windows service. I'm assuming that you've already started Visual Studio .NET and opened the New Project dialog box.
Select the Windows Service (.NET) project in the New Project dialog box. Then type a name for the Windows service (the example uses SimpleService) and click OK. In the resulting Designer window, you can drag components from Server Explorer to the Windows service. For example, if you want to monitor the current processor usage with your new Windows service, you could drag a performance-monitoring component from Server Explorer to perform the task. For this example, we won't use any special components.
Select the Designer window to change the IDE focus. The Properties dialog box now contains all of the properties for the Windows service, as shown in Figure 1. Notice the Add Installer link at the bottom of the Properties dialog box. You'll use this link to create an installer after you configure the Windows service.
Perform any required configuration. The only property that you need to change for this example is ServiceName. The example uses TrackAccess.
Click the Add Installer link at the bottom of the Properties dialog box. Visual Studio displays another Designer windowthis one for the installer. The installer requires the use of two components that Visual Studio adds for you automatically: serviceProcessInstaller1 and serviceInstaller1. These components help you to manage the Windows service installation.
Select the serviceProcessInstaller1 control and choose an account property that matches the Windows service requirements. The account determines the security level of the Windows service; you can change it later, but it's best to set it now. Many Windows services use the LocalSystem account, but the User account is more secure because the Windows service can't be tricked into performing tasks the user isn't allowed to perform. For this example to work properly, though, you must select the LocalSystem account.
Select the serviceInstaller1 control. As a minimum, set the DisplayName property to the name you want to see in the Services console and the StartType property to the method you want to use for starting the Windows service. Generally, you should use a DisplayName value that's easy to read and understand; the example uses Simple Windows Service. Depending on what task your Windows service performs, you'll likely need to specify manual or automatic starting.
Because a Windows service doesn't have a user interface, you can't use controls with it.
Figure 1 Notice the Add Installer link in the Properties dialog box; click it only after you configure the Windows service.
Always configure the Windows service before you add the installer. It's a lot of work to perform this task later.