Click here to Skip to main content
Click here to Skip to main content

Easy Command Line Service

By , 16 Nov 2012
Rate this:
Please Sign up or sign in to vote.

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

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

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)

About the Author

Andy Bantly
Founder
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.
Follow on   Google+   LinkedIn

Comments and Discussions

 
QuestionDoesn't work with Windows 8? PinmemberMember 811741721-Jan-14 8:31 
AnswerRe: Doesn't work with Windows 8? PinmemberAndy Bantly4-Feb-14 2:42 
Questionabout threading PinmemberPony27931-May-13 4:43 
AnswerRe: about threading PinmemberAndy Bantly3-Jun-13 4:34 
GeneralMy vote of 5 PinmemberMihai MOGA14-Dec-12 6:13 
GeneralRe: My vote of 5 PinmemberAndy Bantly14-Dec-12 9:34 
GeneralMy vote of 5 PinmemberMember 793525227-Nov-12 6:49 
GeneralMy vote of 5 Pinmemberkanalbrummer19-Nov-12 22:50 
GeneralRe: My vote of 5 PinmemberAndy Bantly20-Nov-12 3:44 

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 | Mobile
Web01 | 2.8.140415.2 | Last Updated 16 Nov 2012
Article Copyright 2012 by Andy Bantly
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid