Windows Services (C++) HOWTO using .NET


A brief discription of Windows Services and installation procedures is presented here. This HOWTO is adopted from John Mueller's description which can be found here. This article is adapted specifically to CMS DCS requirements and specifications.

Introduction to Windows Services

A Windows service is an application that sits in the background, waiting for you to call it or for some system event to happen much like a Daemon in *NIX based systems. Services generally control operations that the user does not have to be immideatly concerned with like system schedules, DHCP service, or servers in general. In order to see the services that your Windows operating system is currently running, you may go to the command prompt and type in 'start services.msc' to pull up the services window. A quick look will show you that services perform a wide range of tasks—everything 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.


This HOWTO assumes you have Microsoft Visual Studio Professional edition since this is necessary for the following template system. Note that at least the professional edition is required for the use of the windows service template which the IDE supplies. Otherwise you can attempt to write the service with notepad using another template I have created. You can attain the template by contacting me at This HOWTO covers the easier method as it is less time consuming and Visual Studio .NET can be installed through CMF.

First Steps

  • First of all, you should fire up Visual Studio .NET and go to File -> New -> Project

You should now see the New Project Dialouge window. On the left hand side you should see a sub category called Visual C++. Once that category is selected, notice there is a project template that reads "Windows Service." Select that project and give a name to the project. In this case, let us use the name FIUService. 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.

Select the Designer window to change the IDE focus. The Properties dialog box now contains all of the properties for the Windows service. 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 FIUServiceCom.

  • NOTE You should always configure your service before you click on the "add installer" link.

Now in the main field of the Visual Studio IDE (the center for the screen) you should notice a grey area that reads "To add components to your class, drag them from the Toolbox.." For this simple example let us not add any special components. Just click on the blue text that reads "click here to switch to code view."

In this view you will notice some usual C++ code that even novice users should be familiar with. The using using namespace ... lines are usual declarations that tell the system what special libraries to use. The namespaces that are added are the bare essentials for a windows service.

Upon scrolling down, you will notice a few methods. Instead of running into the usual main function that we are all familiar with, there are a few odd named methods which define how the windows operating system implements the service you are writing. The main methods which are of most importance in this scope are the OnStart and OnStop methods. These methods essentially tell the operating system what to do in the case that the service starts and stops as is controlled by windows.

For all practical applications, whatever you have accustomed yourself to writing in your main function will now be added to the OnStart method. The OnStop method tells the operating system what to do in the case that the windows service is cleanly asked to stop.

The default template code for the Visual Studio .NET 2005 Professional edition should look like this:

virtual void OnStart() override


// TODO: Add code here to perform any tear-down necessary to stop your service.


Notice the TODO section. You can treat this method as your usual main function in regular C/C++ programs.

In this example we will simply tell the windows service to write an entry to the Event Viewer, so let us continue.

The Event Viewer

In the case of writing windows services, there is no user interface for the user to interact or even know what is going on with the program. Windows services run in the background and therefore cannot have a simple cout << "Hello World!" << endl; program test as most of us are accustomed to. To this end, we should use the windows event viewer to know the status of our windows service.

The windows event viewer is a logging system that windows uses which is comparable to the /var/log/ directory in *NIX based systems. Every NT based windows operating system has one and if you are viewing this article through such a system, you can view your own if you type in the following command in the start -> run prompt:


This will bring up the "Computer Management" admistration console. This componet holds most necessary tools for this project. Notice that in the menu on the left you can view the Event Viewer aswell as the current system services. Click on the Event Viewer. There are three sections to the logging system that the event viewer supplies: the Application, Security, and the System section. For this example will be writing to the application section.

More can be read about the Event Viewer on the Wikipedia article about the event viewer.

DCS Links

Wiki Tools:

Search options

Index | Preferences | Recent changes | E-mail notification | Access statistics

-- Richard Galvez - 30 May 2007

Edit | Attach | Watch | Print version | History: r1 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r1 - 2007-05-30 - unknown
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    CMS/HCAL/DCS All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2023 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
or Ideas, requests, problems regarding TWiki? use Discourse or Send feedback