Click here to Skip to main content
Licence 
First Posted 11 Jun 2001
Views 167,218
Bookmarked 52 times

Writing Windows Services - Made easy

By | 11 Jun 2001 | Article
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

About the Author

Ganesh Ramaswamy



India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberAshish Tyagi 405:11 28 Jan '11  
GeneralLicensing Question PinmemberMember 16332506:39 27 Jul '10  
GeneralRe: Licensing Question PinmemberGanesh Ramaswamy23:40 27 Jul '10  
GeneralRe: Licensing Question PinmemberMember 16332506:59 29 Jul '10  
GeneralRe: Licensing Question PinmemberMember 16332507:27 29 Jul '10  
GeneralRe: Licensing Question PinmemberGanesh Ramaswamy8:09 29 Jul '10  
GeneralRe: Licensing Question PinmemberMember 163325011:29 29 Jul '10  
QuestionRun external application using windows service Pinmemberlaith_omar3:03 7 Feb '07  
Generalimplementation of windows services Pinmemberkalaleela19:45 6 Dec '06  
GeneralDon't use this code... PinmemberGrimoire14:25 20 Jul '06  
It is full of mistakes, many of which will cause memory leaks and crashes.
 
The XCString class doesn't even have a destructor, despite allocating memory. The function DWORD CXService::Dispatch() only returns an error code upon failure, and returns nothing upon success. The Windows API ControlService() is called in 4 different places with an invalid pointer to a SERVICE_STATUS structure.
 
And those were just the bugs I found at a quick glance.
 
Nice idea, but you really need to test the code before you post it.
GeneralAccess Denied Error PinmemberSreekanth Muralidharan18:34 4 Dec '05  
GeneralNeeds more support PinmemberSreekanth Muralidharan0:30 3 Dec '05  
GeneralSupport System.Windows.Forms Pinmemberpraetorion23:12 18 Jul '05  
GeneralService crashes in winxp Pinmembersjacob23:07 22 Mar '05  
GeneralNice Idea but needs debugging... PinPopularmemberfuzzy2logic19:51 18 May '04  
GeneralService failure on Win 2k Server os PinsussDeepak Bhatia20:34 6 Apr '04  
GeneralRe: Service failure on Win 2k Server os Pinmemberverinder_bindra18:36 17 Sep '04  
GeneralArticle needs a demo project PinmemberDan Clark5:43 17 Dec '03  
GeneralInterprocess communications with NT Services Pinmembermstraub7:45 18 Nov '03  
GeneralGood idea Pinmembervc199416:27 17 Dec '02  
GeneralNice article PinmemberKumar Rangarajan8:03 16 Sep '02  
GeneralNT Service PinmemberMuneet Bakshi3:09 26 Oct '01  
GeneralRe: NT Service PinmemberCarlos Antollini3:49 26 Oct '01  
QuestionMFC Service App that includea a task icon? PinmemberAnonymous6:55 11 Aug '01  
AnswerRe: MFC Service App that includea a task icon? PinmemberSardaukar19:45 11 Jul '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 12 Jun 2001
Article Copyright 2001 by Ganesh Ramaswamy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid