Click here to Skip to main content
Click here to Skip to main content

Simple service base class for Windows

By , 23 Mar 2004
 

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
No Biography provided

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralBit manipulationmemberhobyrne24-Oct-05 11:39 
File: cservice_base_src.zip : I_ServiceBase.cpp
 
void CI_ServiceBase::set_CanHandlePowerEvent(bool b)
{
//SET_BIT(this->m_CanHandle, P_CAN_HANDLE_POWER_EVENT, b);
if(b)
{
m_Status.dwControlsAccepted |= SERVICE_ACCEPT_POWEREVENT;
}
else
{
m_Status.dwControlsAccepted ^= SERVICE_ACCEPT_POWEREVENT;
}
}
 
Please tell me you're kidding. Do you know what ^ means? It means XOR, it means toggle. set_CanHandlePowerEvent(true) will always turn on the flag, but set_CanHandlePowerEvent(false) will not always turn it off.

GeneralRe: Bit manipulation PinmemberEasyWay30-May-06 2:46 
Ok, I was not long time here. But here is the answer just for the archive.
 
if SERVICE_ACCEPT_POWEREVENT bits are set already, this operation:
m_Status.dwControlsAccepted ^= SERVICE_ACCEPT_POWEREVENT; will do turn off, that's for shure.
 
Thank You Smile | :)
 
Faruk Kasumovic.
Student of Electrical Enginering (Information Technologies) in Tuzla, Bosnia and Herzegovina.
GeneralRe: Bit manipulation PinmemberMike C#13-Oct-06 6:22 
Just so I understand, is this what you want to achieve in this code?
 
set_CanHandlePowerEvent(true) = turn "on" SERVICE_ACCEPT_POWEREVENT
 
set_CanHandlePowerEvent(false) *and* SERVICE_ACCEPT_POWEREVENT is currently "on" = turn "off" SERVICE_ACCEPT_POWEREVENT
set_CanHandlePowerEvent(false) *and* SERVICE_ACCEPT_POWEREVENT is currently "off" = turn "on" SERVICE_ACCEPT_POWEREVENT
 
This is what your code currently does.   Just making sure I understand.   Thanks.
GeneralRe: Bit manipulation Pinmemberdamnedyankee7-Nov-06 2:55 
Clearly what is really desired is this:
 
void CI_ServiceBase::set_CanHandlePowerEvent(bool b)
{
if(b)
{
m_Status.dwControlsAccepted |= SERVICE_ACCEPT_POWEREVENT;
}
else
{
m_Status.dwControlsAccepted &= ~SERVICE_ACCEPT_POWEREVENT;
}
}
 
which has the expected results of:
 
set_CanHandlePowerEvent(true) = turn "on" SERVICE_ACCEPT_POWEREVENT
set_CanHandlePowerEvent(false) = turn "off" SERVICE_ACCEPT_POWEREVENT

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

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