Click here to Skip to main content
11,500,844 members (79,465 online)
Click here to Skip to main content

Writing Windows Services - Made easy

, 11 Jun 2001 177.5K 3.6K 55
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ashish Tyagi 4028-Jan-11 5:11
memberAshish Tyagi 4028-Jan-11 5:11 
good job. thanks
GeneralLicensing Question Pin
Member 163325027-Jul-10 6:39
memberMember 163325027-Jul-10 6:39 
GeneralRe: Licensing Question Pin
Ganesh Ramaswamy27-Jul-10 23:40
memberGanesh Ramaswamy27-Jul-10 23:40 
GeneralRe: Licensing Question Pin
Member 163325029-Jul-10 6:59
memberMember 163325029-Jul-10 6:59 
GeneralRe: Licensing Question Pin
Member 163325029-Jul-10 7:27
memberMember 163325029-Jul-10 7:27 
GeneralRe: Licensing Question Pin
Ganesh Ramaswamy29-Jul-10 8:09
memberGanesh Ramaswamy29-Jul-10 8:09 
GeneralRe: Licensing Question Pin
Member 163325029-Jul-10 11:29
memberMember 163325029-Jul-10 11:29 
QuestionRun external application using windows service Pin
laith_omar7-Feb-07 3:03
memberlaith_omar7-Feb-07 3:03 
Generalimplementation of windows services Pin
kalaleela6-Dec-06 19:45
memberkalaleela6-Dec-06 19:45 
GeneralDon't use this code... Pin
Grimoire20-Jul-06 14:25
memberGrimoire20-Jul-06 14:25 
GeneralAccess Denied Error Pin
Sreekanth Muralidharan4-Dec-05 18:34
memberSreekanth Muralidharan4-Dec-05 18:34 
GeneralNeeds more support Pin
Sreekanth Muralidharan3-Dec-05 0:30
memberSreekanth Muralidharan3-Dec-05 0:30 
GeneralSupport System.Windows.Forms Pin
praetorion18-Jul-05 23:12
memberpraetorion18-Jul-05 23:12 
GeneralService crashes in winxp Pin
sjacob22-Mar-05 23:07
membersjacob22-Mar-05 23:07 
GeneralNice Idea but needs debugging... Pin
fuzzy2logic18-May-04 19:51
memberfuzzy2logic18-May-04 19:51 
GeneralService failure on Win 2k Server os Pin
Deepak Bhatia6-Apr-04 20:34
sussDeepak Bhatia6-Apr-04 20:34 
GeneralRe: Service failure on Win 2k Server os Pin
verinder_bindra17-Sep-04 18:36
memberverinder_bindra17-Sep-04 18:36 
GeneralArticle needs a demo project Pin
Dan Clark17-Dec-03 5:43
memberDan Clark17-Dec-03 5:43 
GeneralInterprocess communications with NT Services Pin
mstraub18-Nov-03 7:45
membermstraub18-Nov-03 7:45 
GeneralGood idea Pin
vc199417-Dec-02 16:27
membervc199417-Dec-02 16:27 
GeneralNice article Pin
Kumar Rangarajan16-Sep-02 8:03
memberKumar Rangarajan16-Sep-02 8:03 
GeneralNT Service Pin
Muneet Bakshi26-Oct-01 3:09
memberMuneet Bakshi26-Oct-01 3:09 
GeneralRe: NT Service Pin
Carlos Antollini26-Oct-01 3:49
memberCarlos Antollini26-Oct-01 3:49 
QuestionMFC Service App that includea a task icon? Pin
Anonymous11-Aug-01 6:55
memberAnonymous11-Aug-01 6:55 
AnswerRe: MFC Service App that includea a task icon? Pin
Sardaukar11-Jul-02 19:45
memberSardaukar11-Jul-02 19:45 
GeneralRe: MFC Service App that includea a task icon? Pin
Harry Hv30-Oct-02 9:57
sussHarry Hv30-Oct-02 9:57 
GeneralRe: MFC Service App that includea a task icon? Pin
Sardaukar30-Oct-02 19:29
memberSardaukar30-Oct-02 19:29 
GeneralRe: MFC Service App that includea a task icon? Pin
Minh Hoa1-May-03 17:04
memberMinh Hoa1-May-03 17:04 
GeneralLockDatabase Pin
RainerS22-Jun-01 1:27
memberRainerS22-Jun-01 1:27 
GeneralRe: LockDatabase Pin
Ganesh Ramaswamy22-Jun-01 4:37
memberGanesh Ramaswamy22-Jun-01 4:37 
GeneralWindows 9x/ME support Pin
Michael McGarrah20-Jun-01 9:00
memberMichael McGarrah20-Jun-01 9:00 
GeneralRe: Windows 9x/ME support Pin
Ganesh Ramaswamy21-Jun-01 4:43
memberGanesh Ramaswamy21-Jun-01 4:43 
GeneralRe: Windows 9x/ME support Pin
Sardaukar11-Jul-02 19:47
memberSardaukar11-Jul-02 19:47 

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.

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