Click here to Skip to main content
13,056,135 members (51,042 online)
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 16 Nov 2012

Easy Command Line Service

, 16 Nov 2012
Rate this:
Please Sign up or sign in to vote.
An easy DOS framework for creating and managing a service


I've been asked many times in another article I submitted the question "can it run as a service?"  The answer to that question was always "no".  Using the code in this article as a starting point, the developer will be able to write their own services.  This code owes a great debt of gratitude to MSDN where I originally found a tutorial.  I wanted to link to that article but can no longer find it.


The developer should have a copy of Debug View so that they can see the output of the program.  Debug View is part of the SysInternals suite which are a wonderful set of tools for any developer interested in probing windows applications and their interactions with the O/S,

Using the code

The compiled code produces an executable called cls.exe, which is short for command line service.  The service is installed with the command cls.exe -install and uninstalled with cls.exe -uninstall.  You can view the output of these operations using Debug View

There are two main classes which implement the creating, deleting, managing, starting, and stopping the service.  These are CNTService and CMyService.  CNTService is the base class and it is responsible for the boiler plate code that does the heavy lifting.  This includes installing, uninstalling, and all the events that the service could possible receive through the services applet. CMyService is a derived class with CNTService as the base class.  This is where we customize and implement the code we want our service to perform

class CMyService : public CNTService
	virtual BOOL OnInit();
	virtual void Run();
	virtual BOOL OnUserControl(DWORD dwOpcode);
	virtual void OnStop();

	void SaveStatus();
	bool m_bRun;

To add your custom behavior to the service you will simply put your code in the "Run" function.  When this function returns the service stops.  Since a service is normally a continuosly running process there is usually a mechanism put in place, possibly similar to this

void CMyService::Run()
	DebugMsg(L"Entering CMyService::Run()\n");

		// Do something here
	while (m_bRun);

	DebugMsg(L"Leaving CMyService::Run()\n");

There you have it.  Now that you are armed with an easy to use template for making a Win32 C++ high performance service module you can do whatever it is you might want to do in a service.


  • 11/16/2012 - Submitted article


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Andy Bantly
United States United States
Working as a software developer since 1989. Started out with Basic, FORTRAN and JCL, moved into Visual Basic 1.0, C, then C++, and now I work mainly in C++ using MFC, Win32, and ATL/COM. I use Microsoft Products only because that is what gives me gainful employment. Through work, I have a lot of experience with HTML, JavaScript, XSL transformations, the XMLHTTP object, PHP 4.x, and simple COM object integrations.

I've worked for the University of Oklahoma in the school of meteorology (Go SOONERS!), consulting, and now as a Senior Software Engineer. These things keep my lights on and electricity going. My dream job is to own a bowling alley and rub elbows with pro-bowlers! I'm also an avid pedicab driver and have my own cab. I like the hustle of picking up people in downtown and biking them to their destination.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Member 1067582325-Jan-15 3:42
memberMember 1067582325-Jan-15 3:42 
GeneralRe: My vote of 5 Pin
Andy Bantly25-Jan-15 5:19
memberAndy Bantly25-Jan-15 5:19 
QuestionHow can we run the service? Pin
Member 1102979023-Aug-14 9:40
memberMember 1102979023-Aug-14 9:40 
AnswerRe: How can we run the service? Pin
Andy Bantly23-Aug-14 10:11
memberAndy Bantly23-Aug-14 10:11 
QuestionDoesn't work with Windows 8? Pin
Member 811741721-Jan-14 8:31
memberMember 811741721-Jan-14 8:31 
AnswerRe: Doesn't work with Windows 8? Pin
Andy Bantly23-Aug-14 10:11
memberAndy Bantly23-Aug-14 10:11 
Questionabout threading Pin
Pony27931-May-13 4:43
memberPony27931-May-13 4:43 
AnswerRe: about threading Pin
Andy Bantly3-Jun-13 4:34
memberAndy Bantly3-Jun-13 4:34 
GeneralMy vote of 5 Pin
Mihai MOGA14-Dec-12 6:13
memberMihai MOGA14-Dec-12 6:13 
GeneralRe: My vote of 5 Pin
Andy Bantly14-Dec-12 9:34
memberAndy Bantly14-Dec-12 9:34 
GeneralMy vote of 5 Pin
Member 793525227-Nov-12 6:49
memberMember 793525227-Nov-12 6:49 
GeneralMy vote of 5 Pin
kanalbrummer19-Nov-12 22:50
memberkanalbrummer19-Nov-12 22:50 
GeneralRe: My vote of 5 Pin
Andy Bantly20-Nov-12 3:44
memberAndy Bantly20-Nov-12 3:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.170728.2 | Last Updated 16 Nov 2012
Article Copyright 2012 by Andy Bantly
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid