Click here to Skip to main content
Licence 
First Posted 23 Mar 2004
Views 58,959
Bookmarked 20 times

Simple service base class for Windows

By | 23 Mar 2004 | Article
This class provides a simple way to implement Windows service in C++.

Introduction

The purpose of this code is to show how to make a simple C++ class which wraps WinNT API for service programs. With this class, you can implement a Windows service in a few minutes. This class is not made for advanced service programming, but it should not be too hard modify this source to append any additional functionality. When I implemented this class, I was trying to copy the interface of ServiceBase class from .NET framework. I think it's good design for most requirements of service applications.

Have on mind that using this class you can't implement multiple services in one application. I will implement this in future updates and add more functionality.

Using the code

I didn't provide a sample project this time but here is the code you need for that:

#include <windows.h>
#include <stdio.h>
#include "smart_reference.h"
#include "I_ServiceBase.h"

void WriteLog(const char* message)
{
    FILE* pFile = fopen("C:\\test_service.log","a");
    if(pFile)
    {
        fwrite(message, sizeof(char), strlen(message), pFile); 
        fclose(pFile);
    }
}

class TestService : public CI_ServiceBase
{
public:
    TestService()
    {
        m_bStop = false;
    }

    ~TestService()
    {
        CI_ServiceBase::~CI_ServiceBase();
    }

    virtual void OnStart(int argc, LPTSTR* argv)
    {
        WriteLog("Server started.\n\r");
        while(!m_bStop)
        {
            Sleep(1000);
        }
    }
    virtual void OnStop()
    {
        WriteLog("Server stopped.\n\r");
        m_bStop = true;
    }

    virtual void OnPause()
    {
        WriteLog("Server paused.\n\r");
    }

    virtual void OnContinue()
    {
        WriteLog("Server continued.\n\r");
    }

private:
    bool m_bStop;
};

TestService ts;

int main(int argc, char* argv[])
{
    ts.set_CanPauseAndContinue(false);
    ts.set_ServiceName("isw_test");
    CI_ServiceBase::Run((TestService*)ts);
    return 0;
}

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

EasyWay

Web Developer

Bosnia And Herzegovina Bosnia And Herzegovina

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalbizarre hard-coded service name buffer length Pinmemberdamnedyankee3:39 7 Nov '06  
Generalincorrect use of SERVICE_WIN32 in CreateService / SetServiceStatus Pinmemberdamnedyankee2:50 7 Nov '06  
GeneralBit manipulation Pinmemberhobyrne11:39 24 Oct '05  
GeneralRe: Bit manipulation PinmemberEasyWay2:46 30 May '06  
GeneralRe: Bit manipulation PinmemberMike C#6:22 13 Oct '06  
GeneralRe: Bit manipulation Pinmemberdamnedyankee2:55 7 Nov '06  
Generalinstall/uninstall -- unmanaged c++ Pinmemberasmotritsky15:36 3 Jun '04  
GeneralNot bad... missing a few pieces... PinmemberPeter Mares23:13 24 Mar '04  
GeneralRe: Not bad... missing a few pieces... PinmemberEasyWay3:47 31 Mar '04  
GeneralFiles not found..... PinmemberJosema3:44 24 Mar '04  
GeneralQuestion Pinmemberdog_spawn2:28 24 Mar '04  
GeneralRe: Question PinmemberJohn M. Drescher6:24 24 Mar '04  
GeneralRe: Question PinsussAnonymously6:30 24 Mar '04  
GeneralRe: Question PinmemberEasyWay3:45 31 Mar '04  
GeneralRe: Question Pinmemberdamnedyankee23:09 6 Nov '06  
>>In case when base class destructor is not declared as virtual, he will not be called in derived class destructor, you have to call it explicit to release any resources used by base class. It's C++.
 

NO IT'S NOT C++ !!!
 
#include
 
using std::cout;
using std::endl;
 
class b_virt
{
public:
b_virt(){}
virtual ~b_virt(){ cout << "b_virt virtual destructor" << endl; }
};
 
class b_std
{
public:
b_std(){}
~b_std(){ cout << "b_std destructor" << endl; }
};
 
class d_virt : public b_virt
{
public:
d_virt(){}
~d_virt(){ cout << "d_virt derived destructor" << endl; }
};
 
class d_std : public b_std
{
public:
d_std(){}
~d_std(){ cout << "d_std derived destructor" << endl; }
};
 
int main(int argc, char* argv[])
{
d_virt dv;
d_std ds;
 
return 0;
}
 

produces this:
 
d_std derived destructor
b_std destructor
d_virt derived destructor
b_virt virtual destructor
 
Notice there is no explicit call to either destructor, yet *somehow*, this code magically calls the virtual destructor in class b_virt. That is C++ !
 
It is not necessary to call the base clas destructor from within the derived class, the compiler will generate code to do this, just as it generates code to call destructors on class members.
 

>>When I wrote the sample code I just do it for habit, but it should not produce the problem even if base class destructor is virtual.
 
It is very dangerous to assume it is safe to call a destructor twice. Your habit is dangerous, and you should stop it. Your code is otherwise quite good.
GeneralRe: Question PinmemberGyuri2:40 29 Jul '07  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 24 Mar 2004
Article Copyright 2004 by EasyWay
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid