5,276,156 members and growing! (19,246 online)
Email Password   helpLost your password?
General Reading » Hardware & System » Services     Intermediate

Simple service base class for Windows

By EasyWay

This class provides a simple way to implement Windows service in C++.
VC6, VC7, VC7.1, C++Windows, NT4, Win2K, WinXP, Win2003VS.NET2002, VS, Dev

Posted: 23 Mar 2004
Updated: 23 Mar 2004
Views: 34,808
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
10 votes for this Article.
Popularity: 2.12 Rating: 2.12 out of 5
7 votes, 70.0%
1
0 votes, 0.0%
2
1 vote, 10.0%
3
0 votes, 0.0%
4
2 votes, 20.0%
5

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



Occupation: Web Developer
Location: Bosnia And Herzegovina Bosnia And Herzegovina

Other popular Hardware & System articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh)FirstPrevNext
Subject  Author Date 
Generalbizarre hard-coded service name buffer lengthmemberdamnedyankee4:39 7 Nov '06  
Generalincorrect use of SERVICE_WIN32 in CreateService / SetServiceStatusmemberdamnedyankee3:50 7 Nov '06  
GeneralBit manipulationmemberhobyrne12:39 24 Oct '05  
GeneralRe: Bit manipulationmemberEasyWay3:46 30 May '06  
GeneralRe: Bit manipulationmemberMike C#7:22 13 Oct '06  
GeneralRe: Bit manipulationmemberdamnedyankee3:55 7 Nov '06  
Generalinstall/uninstall -- unmanaged c++memberasmotritsky16:36 3 Jun '04  
GeneralNot bad... missing a few pieces...memberPeter Mares0:13 25 Mar '04  
GeneralRe: Not bad... missing a few pieces...memberEasyWay4:47 31 Mar '04  
GeneralFiles not found.....memberJosema4:44 24 Mar '04  
GeneralQuestionmemberdog_spawn3:28 24 Mar '04  
GeneralRe: QuestionmemberJohn M. Drescher7:24 24 Mar '04  
GeneralRe: QuestionsussAnonymously7:30 24 Mar '04  
GeneralRe: QuestionmemberEasyWay4:45 31 Mar '04  
GeneralRe: Questionmemberdamnedyankee0:09 7 Nov '06  
GeneralRe: QuestionmemberGyuri3:40 29 Jul '07  

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

PermaLink | Privacy | Terms of Use
Last Updated: 23 Mar 2004
Editor: Smitha Vijayan
Copyright 2004 by EasyWay
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project