Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Writing Windows Services - Made easy

0.00/5 (No votes)
11 Jun 2001 1  
A framework for developing Windows services.

Introduction

A Windows Service program is one that gets registered to the service manager and starts as soon as the system boots and remains running as long as the system is up. These are special kind of programs that has to do some task for them to be treated as Services.

Presented here is a couple of classes that eases the process of creating a Windows Service and controlling them.

Description

The CXService class

This class is the one that actually helps in creating the service program. This class takes care of all the internals needed viz., registering a service, handling the control events, service main, setting the service status etc.,

It has a set of macros - something like MFC message map, called as service maps, that are used for handling and processing the events of the service. It also has got a pure virtual initialization function, called during the service initialization process.

The main advantage of using this class for creating a service program is that it uses MFC like message-map functionality and so its easy to use. Moreover, because of the service maps, it has only 2 virtual functions and so the overhead is also very less.

The CXServiceManager class

This is a service control class. This provides all the functionality that is needed for creating and working with a service program or services that exists. The class methods are pretty straight forward and self-explanatory.

Usage

For creating a new Windows service program

Derive a class from CXService and implement the pure virtual function - service initialization function. Then declare the service maps and implement the necessary maps for handling the events, providing handler functions for them.

In the main() - create an object for the derived class with the service name and then call the base class initialization function. After this, make a call to the base class dispatcher function. That's it. U have created a Windows service program. Add your functionality where ever needed.

Please make sure that the dispatcher function is called as early as possible or else the program will fail. If you have lots of initialization code, then handle them in a separate thread, so that dispatcher is called ASAP.

Making the service program to service and running it

After the service program is completed you can use the CXServiceManager class to create a service and start the service. This can be done in a separate program called as the service control program. Please refer the class documentation, because its simple and straight-forward.

This is the first version of the program and Unicode support is not added. Please refer the source for further documentation. Please read the ReadMe.txt in the downloads for file description.

That's it!!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here