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();
m_sh.SetServiceName("HelloService3000");
m_sh.Start();
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.
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.
Nish authored C++/CLI in Action for Manning Publications in 2005, and 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 experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.
Nish's Technology Blog :
voidnish.wordpress.com