Click here to Skip to main content
6,305,776 members and growing! (15,759 online)
Email Password   helpLost your password?
General Reading » Hardware & System » Services     Beginner

CServiceHelper

By Nishant Sivakumar

A simple class to install, uninstall, start, stop, pause, continue Win32 services
VC6, VC7Win2K, WinXP, Visual Studio, Dev
Posted:20 May 2002
Views:99,986
Bookmarked:36 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
42 votes for this article.
Popularity: 6.86 Rating: 4.23 out of 5
1 vote, 4.2%
1

2

3
2 votes, 8.3%
4
21 votes, 87.5%
5

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

About the Author

Nishant Sivakumar


Member
Nish is a real nice guy living in Atlanta, who has been coding since 1990, when he was 13 years old. Originally from sunny Trivandrum in India, he recently moved to Atlanta from Toronto and is a little sad that he won't be able to play in snow anymore.

Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com

Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.

Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.

Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.
Location: United States United States

Other popular Hardware & System articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 48 (Total in Forum: 48) (Refresh)FirstPrevNext
QuestionError : 1053 with Service application. PinmemberSriphani0:43 25 Aug '08  
Generalhi nish Pinmemberashu_om4:16 25 Apr '07  
Generalpassing parameters to Service main from another prog...please help Pinmemberhiitsshiva22:01 10 Apr '06  
GeneralIsInstalled PinmemberSohailB23:51 18 Sep '05  
GeneralRe: IsInstalled Pinmembershtorm13:08 16 May '06  
GeneralHow to complie this classes with my W32 console programm? I got too many errors Pinsussflyflyfly9:38 11 Apr '05  
Generaltype of execution file PinmemberAkram Majed3:07 21 Jun '04  
Generalcan not start service ? PinmemberAkram Majed23:02 20 Jun '04  
GeneralCould not start the service PinmemberTommy N8:55 25 Feb '04  
Generallink error Pinmembermafzo0:50 6 Aug '03  
GeneralRe: link error PineditorNishant S8:55 7 Aug '03  
GeneralAAAAAHH !!!! PinmemberMartin Schneider5:57 10 Jul '02  
GeneralRe: AAAAAHH !!!! PinmemberMartin Schneider6:12 10 Jul '02  
GeneralRe: AAAAAHH !!!! PinsubeditorNishant S0:46 15 Jul '02  
GeneralI Like it Nish PinmemberMatt Newman16:36 21 May '02  
GeneralRe: I Like it Nish PinmemberNish - Native CPian16:48 21 May '02  
GeneralRe: I Like it Nish PinmemberMatt Newman11:31 22 May '02  
GeneralRe: I Like it Nish PinsubeditorNishant S0:45 15 Jul '02  
GeneralWell, looks better but... PinmemberAndreas Saurwein13:00 21 May '02  
GeneralRe: Well, looks better but... PinmemberNish - Native CPian16:50 21 May '02  
GeneralRe: Well, looks better but... PinmemberRama Krishna16:55 21 May '02  
GeneralRe: Well, looks better but... PinmemberAndreas Saurwein22:40 21 May '02  
GeneralRe: Well, looks better but... PinmemberRama Krishna2:36 22 May '02  
GeneralThis article must suck :-) PinmemberNish - Native CPian4:46 21 May '02  
GeneralRe: This article must suck :-) PinmemberEd Gadziemski5:40 21 May '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 May 2002
Editor: Nishant Sivakumar
Copyright 2002 by Nishant Sivakumar
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project