Click here to Skip to main content
11,709,844 members (49,448 online)
Click here to Skip to main content

CServiceHelper

, 20 May 2002 CPOL 154.4K 2.1K 53
Rate this:
Please Sign up or sign in to vote.
A simple class to install, uninstall, start, stop, pause, continue Win32 services

Introduction

The CServiceHelper class is a class I wrote when I was distributing Service programs and I found that I was repeatedly having to write a program to install the service, another program to uninstall the service and quite often yet another program to start/stop the service. So I wrote a simple class that allows me to easily install, delete, start, stop, pause and continue a Win32 service. The class is very simple in nature and purpose and it is not an exhaustive class in the sense that there are lots of other operations you might want to do with a service. But I have covered the most frequently used operations and if anyone would like to extend this class, they are quite welcome to do so. While installing the service I am only allowing the two most common options - automatic starting and manual starting services. There are other options available and as I already said, I'd be very happy if anyone wants to enhance the class.

CServiceHelper members

BOOL CServiceHelper::Create()

This function is used to install a service on the target machine. It returns true on success and false on failure.

CServiceHelper m_sh;
m_sh.SetServiceDisplayName("Hello Service 2000");
m_sh.SetServiceName("HelloService2000");
m_sh.SetServicePath("D:\\nish\\Hello2000.exe"); 
m_sh.SetAutoStart(true); 
m_sh.Create();

BOOL CServiceHelper::Delete()

This function is used to uninstall a service from the target machine. It returns true on success and false on failure.

CServiceHelper m_sh; 
m_sh.SetServiceName("HelloService2000");
m_sh.Delete();

BOOL CServiceHelper::Start()

This function is used to start a service installed on the target machine. It returns true on success and false on failure.

CServiceHelper m_sh; 
m_sh.SetServiceName("HelloService2000");
m_sh.Start();

BOOL CServiceHelper::Stop()

This function is used to stop a running service installed on the target machine. It returns true on success and false on failure.

CServiceHelper m_sh; 
m_sh.SetServiceName("HelloService2000");
m_sh.Stop();

BOOL CServiceHelper::Pause()

This function is used to pause a running service installed on the target machine. It returns true on success and false on failure.

CServiceHelper m_sh; 
m_sh.SetServiceName("HelloService2000");
m_sh.Pause();

BOOL CServiceHelper::Continue()

This function is used to continue a paused service installed on the target machine. It returns true on success and false on failure.

CServiceHelper m_sh; 
m_sh.SetServiceName("HelloService2000");
m_sh.Continue();

void CServiceHelper::SetServiceName(LPCTSTR str)

This function is used to set the name of the service and must be compulsorily called before any of the major functions are called.

m_sh.SetServiceName("HelloService2000");

void CServiceHelper::SetServiceDisplayName(LPCTSTR str)

This function is used to set the display name of the service. It should be called before calling Create()

m_sh.SetServiceDisplayName("Hello Service 2000");

void CServiceHelper::SetServicePath(LPCTSTR str)

This function is used to set the path to the service executable. It should be called before calling Create()

m_sh.SetServicePath("D:\\nish\\Hello2000.exe"); 

void CServiceHelper::SetAutoStart(BOOL b)

This function is used to set whether the service starts automatically at boot-up or whether it is manually started by the user. Set this to true for automatic starting and false otherwise.

m_sh.SetAutoStart(true); 

Tips

You can actually use an instance of the class to handle multiple services. Just change the members of the m_serviceinfo struct and call the required function. I'll show a simple example below.

CServiceHelper m_sh; 
m_sh.SetServiceName("HelloService2000"); 
m_sh.Stop(); //we stop the old service 
m_sh.SetServiceName("HelloService3000");
m_sh.Start(); //and start the new one

History

Within 6 hours after I had posted this class, I got severely criticized for the non-OOP approach I had adopted. I therefore posted a question on the Lounge and I got very useful replies from several gentlemen like Chris Losinger, John Simmons, Kilowatt, Tim Smith, Nemenja, Michael Butler, James T Johnson to name but a few of those really nice fellows who helped me out in a very kind display of helpful mindedness. I am very thankful to them for helping me correct my errored ways.

License

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

Share

About the Author

Nish Nishant
United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

You may also be interested in...

Comments and Discussions

 
QuestionStart Windows Service with user Account Pin
aminhosseini26-Apr-13 21:08
professionalaminhosseini26-Apr-13 21:08 
GeneralMy vote of 5 Pin
JetDraft12-Dec-12 17:56
groupJetDraft12-Dec-12 17:56 
QuestionError : 1053 with Service application. Pin
Sriphani24-Aug-08 23:43
memberSriphani24-Aug-08 23:43 
AnswerRe: Error : 1053 with Service application. Pin
xxl17813-Aug-13 17:20
memberxxl17813-Aug-13 17:20 
Generalhi nish Pin
ashu_om25-Apr-07 3:16
memberashu_om25-Apr-07 3:16 
Generalpassing parameters to Service main from another prog...please help Pin
hiitsshiva10-Apr-06 21:01
memberhiitsshiva10-Apr-06 21:01 
GeneralIsInstalled Pin
SohailB18-Sep-05 22:51
memberSohailB18-Sep-05 22:51 
GeneralRe: IsInstalled Pin
shtorm16-May-06 12:08
membershtorm16-May-06 12:08 
QuestionHow to complie this classes with my W32 console programm? I got too many errors Pin
flyflyfly11-Apr-05 8:38
sussflyflyfly11-Apr-05 8:38 
Generaltype of execution file Pin
Akram Majed21-Jun-04 2:07
memberAkram Majed21-Jun-04 2:07 
Questioncan not start service ? Pin
Akram Majed20-Jun-04 22:02
memberAkram Majed20-Jun-04 22:02 
GeneralCould not start the service Pin
Tommy N25-Feb-04 7:55
memberTommy N25-Feb-04 7:55 
Generallink error Pin
mafzo5-Aug-03 23:50
membermafzo5-Aug-03 23:50 
GeneralRe: link error Pin
Nishant S7-Aug-03 7:55
editorNishant S7-Aug-03 7:55 
GeneralAAAAAHH !!!! Pin
Martin Schneider10-Jul-02 4:57
memberMartin Schneider10-Jul-02 4:57 
GeneralRe: AAAAAHH !!!! Pin
Martin Schneider10-Jul-02 5:12
memberMartin Schneider10-Jul-02 5:12 
GeneralRe: AAAAAHH !!!! Pin
Nishant S14-Jul-02 23:46
subeditorNishant S14-Jul-02 23:46 
GeneralI Like it Nish Pin
Matt Newman21-May-02 15:36
memberMatt Newman21-May-02 15:36 
GeneralRe: I Like it Nish Pin
Nish - Native CPian21-May-02 15:48
memberNish - Native CPian21-May-02 15:48 
GeneralRe: I Like it Nish Pin
Matt Newman22-May-02 10:31
memberMatt Newman22-May-02 10:31 
GeneralRe: I Like it Nish Pin
Nishant S14-Jul-02 23:45
subeditorNishant S14-Jul-02 23:45 
GeneralWell, looks better but... Pin
Andreas Saurwein21-May-02 12:00
memberAndreas Saurwein21-May-02 12:00 
GeneralRe: Well, looks better but... Pin
Nish - Native CPian21-May-02 15:50
memberNish - Native CPian21-May-02 15:50 
GeneralRe: Well, looks better but... Pin
Rama Krishna21-May-02 15:55
memberRama Krishna21-May-02 15:55 
GeneralRe: Well, looks better but... Pin
Andreas Saurwein21-May-02 21:40
memberAndreas Saurwein21-May-02 21:40 
GeneralRe: Well, looks better but... Pin
Rama Krishna22-May-02 1:36
memberRama Krishna22-May-02 1:36 
GeneralThis article must suck :-) Pin
Nish - Native CPian21-May-02 3:46
memberNish - Native CPian21-May-02 3:46 
GeneralRe: This article must suck :-) Pin
Ed Gadziemski21-May-02 4:40
memberEd Gadziemski21-May-02 4:40 
GeneralRe: This article must suck :-) Pin
Nish - Native CPian21-May-02 6:35
memberNish - Native CPian21-May-02 6:35 
GeneralRe: This article must suck :-) Pin
Chris Maunder31-May-02 14:15
adminChris Maunder31-May-02 14:15 
GeneralRe: This article must suck :-) Pin
Nish - Native CPian31-May-02 14:48
memberNish - Native CPian31-May-02 14:48 
GeneralRe: This article must suck :-) Pin
John Smith21-May-02 6:57
memberJohn Smith21-May-02 6:57 
GeneralRe: This article must suck :-) Pin
Nish - Native CPian21-May-02 8:33
memberNish - Native CPian21-May-02 8:33 
GeneralRe: This article must suck :-) Pin
Michael P Butler21-May-02 8:38
memberMichael P Butler21-May-02 8:38 
GeneralRe: This article must suck :-) Pin
Nish - Native CPian21-May-02 9:04
memberNish - Native CPian21-May-02 9:04 
GeneralRe: Here goes a better article link Pin
Tad21-May-02 9:04
memberTad21-May-02 9:04 
GeneralRe: Here goes a better article link Pin
Tad21-May-02 10:42
memberTad21-May-02 10:42 
GeneralRe: Here goes a better article link Pin
Nish - Native CPian21-May-02 11:22
memberNish - Native CPian21-May-02 11:22 
GeneralRe: Here goes a better article link Pin
Tad21-May-02 20:24
memberTad21-May-02 20:24 
GeneralRe: Here goes a better article link Pin
Nish - Native CPian21-May-02 23:33
memberNish - Native CPian21-May-02 23:33 
GeneralRe: Here goes a better article link Pin
Black Cat21-May-02 20:20
memberBlack Cat21-May-02 20:20 
GeneralRe: Here goes a better article link Pin
Tad21-May-02 20:53
memberTad21-May-02 20:53 
GeneralRe: Here goes a better article link Pin
Anonymously21-May-02 21:06
memberAnonymously21-May-02 21:06 
GeneralRe: Here goes a better article link Pin
Tad22-May-02 1:40
memberTad22-May-02 1:40 
GeneralRe: Here goes a better article link Pin
Nish - Native CPian21-May-02 23:35
memberNish - Native CPian21-May-02 23:35 
GeneralRe: Here goes a better article link Pin
Tad22-May-02 1:43
memberTad22-May-02 1:43 
GeneralRe: Here goes a better article link Pin
Nish - Native CPian22-May-02 1:53
memberNish - Native CPian22-May-02 1:53 
GeneralRe: Here goes a better article link Pin
Nish - Native CPian21-May-02 23:30
memberNish - Native CPian21-May-02 23:30 
GeneralRe: This article must suck :-) Pin
soptest21-May-02 8:57
membersoptest21-May-02 8:57 
GeneralRe: This article must suck :-) Pin
Damon Chitsaz21-May-02 20:46
memberDamon Chitsaz21-May-02 20:46 

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
Web03 | 2.8.150819.1 | Last Updated 21 May 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid