Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C++
Article

Simple service base class for Windows

Rate me:
Please Sign up or sign in to vote.
1.40/5 (10 votes)
23 Mar 2004 80.7K   1.8K   22   16
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


Written By
Web Developer
Bosnia and Herzegovina Bosnia and Herzegovina
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalbizarre hard-coded service name buffer length Pin
damnedyankee7-Nov-06 3:39
damnedyankee7-Nov-06 3:39 
Generalincorrect use of SERVICE_WIN32 in CreateService / SetServiceStatus Pin
damnedyankee7-Nov-06 2:50
damnedyankee7-Nov-06 2:50 
GeneralBit manipulation Pin
hobyrne24-Oct-05 11:39
hobyrne24-Oct-05 11:39 
GeneralRe: Bit manipulation Pin
EasyWay30-May-06 2:46
EasyWay30-May-06 2:46 
GeneralRe: Bit manipulation Pin
Mike C#13-Oct-06 6:22
Mike C#13-Oct-06 6:22 
GeneralRe: Bit manipulation Pin
damnedyankee7-Nov-06 2:55
damnedyankee7-Nov-06 2:55 
Generalinstall/uninstall -- unmanaged c++ Pin
yageroy3-Jun-04 15:36
yageroy3-Jun-04 15:36 
GeneralNot bad... missing a few pieces... Pin
Peter Mares24-Mar-04 23:13
Peter Mares24-Mar-04 23:13 
GeneralRe: Not bad... missing a few pieces... Pin
EasyWay31-Mar-04 3:47
EasyWay31-Mar-04 3:47 
GeneralFiles not found..... Pin
Josema24-Mar-04 3:44
Josema24-Mar-04 3:44 
GeneralQuestion Pin
dog_spawn24-Mar-04 2:28
dog_spawn24-Mar-04 2:28 
GeneralRe: Question Pin
John M. Drescher24-Mar-04 6:24
John M. Drescher24-Mar-04 6:24 
GeneralRe: Question Pin
Anonymously24-Mar-04 6:30
Anonymously24-Mar-04 6:30 
GeneralRe: Question Pin
EasyWay31-Mar-04 3:45
EasyWay31-Mar-04 3:45 
GeneralRe: Question Pin
damnedyankee6-Nov-06 23:09
damnedyankee6-Nov-06 23:09 
GeneralRe: Question Pin
Gyuri29-Jul-07 2:40
Gyuri29-Jul-07 2:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.