Click here to Skip to main content
15,884,425 members
Articles / Desktop Programming / MFC

Easy Command Line Service

Rate me:
Please Sign up or sign in to vote.
4.56/5 (8 votes)
16 Nov 2012CPOL2 min read 29.1K   922   33   13
An easy DOS framework for creating and managing a service

Introduction

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.

Background

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

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

protected:
	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

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

	do
	{
		// 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.

History

  • 11/16/2012 - Submitted article

License

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


Written By
Founder
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 1067582325-Jan-15 3:42
Member 1067582325-Jan-15 3:42 
GeneralRe: My vote of 5 Pin
Andy Bantly25-Jan-15 5:19
Andy Bantly25-Jan-15 5:19 
QuestionHow can we run the service? Pin
Member 1102979023-Aug-14 9:40
Member 1102979023-Aug-14 9:40 
AnswerRe: How can we run the service? Pin
Andy Bantly23-Aug-14 10:11
Andy Bantly23-Aug-14 10:11 
QuestionDoesn't work with Windows 8? Pin
Member 811741721-Jan-14 8:31
Member 811741721-Jan-14 8:31 
AnswerRe: Doesn't work with Windows 8? Pin
Andy Bantly23-Aug-14 10:11
Andy Bantly23-Aug-14 10:11 
Questionabout threading Pin
roxma31-May-13 4:43
roxma31-May-13 4:43 
AnswerRe: about threading Pin
Andy Bantly3-Jun-13 4:34
Andy Bantly3-Jun-13 4:34 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA14-Dec-12 6:13
professionalȘtefan-Mihai MOGA14-Dec-12 6:13 
GeneralRe: My vote of 5 Pin
Andy Bantly14-Dec-12 9:34
Andy Bantly14-Dec-12 9:34 
GeneralMy vote of 5 Pin
Member 793525227-Nov-12 6:49
Member 793525227-Nov-12 6:49 
GeneralMy vote of 5 Pin
kanalbrummer19-Nov-12 22:50
kanalbrummer19-Nov-12 22:50 
GeneralRe: My vote of 5 Pin
Andy Bantly20-Nov-12 3:44
Andy 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.