Click here to Skip to main content
13,260,108 members (51,031 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 23 Mar 2004

Simple service base class for Windows

, 23 Mar 2004
Rate this:
Please Sign up or sign in to vote.
This class provides a simple way to implement Windows service in C++.


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");
        fwrite(message, sizeof(char), strlen(message), pFile); 

class TestService : public CI_ServiceBase
        m_bStop = false;


    virtual void OnStart(int argc, LPTSTR* argv)
        WriteLog("Server started.\n\r");
    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");

    bool m_bStop;

TestService ts;

int main(int argc, char* argv[])
    return 0;


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

Web Developer
Bosnia and Herzegovina Bosnia and Herzegovina
No Biography provided

You may also be interested in...


Comments and Discussions

Generalbizarre hard-coded service name buffer length Pin
damnedyankee7-Nov-06 4:39
memberdamnedyankee7-Nov-06 4:39 
Generalincorrect use of SERVICE_WIN32 in CreateService / SetServiceStatus Pin
damnedyankee7-Nov-06 3:50
memberdamnedyankee7-Nov-06 3:50 
I am confused by your use of SERVICE_WIN32 as the default ServiceType.

The help for SERVICE_STATUS states that the dwServiceType should be one of the values listed, yet SERVCIE_WIN32 is a bitwise or of two of the possible values - and seems to be use primarily for enumeration of services.

Type of service. This member can be one of the following values. Value Meaning
SERVICE_FILE_SYSTEM_DRIVER The service is a file system driver.
SERVICE_KERNEL_DRIVER The service is a device driver.
SERVICE_WIN32_OWN_PROCESS The service runs in its own process.
SERVICE_WIN32_SHARE_PROCESS The service shares a process with other services.

GeneralBit manipulation Pin
hobyrne24-Oct-05 12:39
memberhobyrne24-Oct-05 12:39 
GeneralRe: Bit manipulation Pin
EasyWay30-May-06 3:46
memberEasyWay30-May-06 3:46 
GeneralRe: Bit manipulation Pin
Mike C#13-Oct-06 7:22
memberMike C#13-Oct-06 7:22 
GeneralRe: Bit manipulation Pin
damnedyankee7-Nov-06 3:55
memberdamnedyankee7-Nov-06 3:55 
Generalinstall/uninstall -- unmanaged c++ Pin
asmotritsky3-Jun-04 16:36
memberasmotritsky3-Jun-04 16:36 
GeneralNot bad... missing a few pieces... Pin
Peter Mares25-Mar-04 0:13
memberPeter Mares25-Mar-04 0:13 
GeneralRe: Not bad... missing a few pieces... Pin
EasyWay31-Mar-04 4:47
memberEasyWay31-Mar-04 4:47 
GeneralFiles not found..... Pin
Josema24-Mar-04 4:44
memberJosema24-Mar-04 4:44 
GeneralQuestion Pin
dog_spawn24-Mar-04 3:28
memberdog_spawn24-Mar-04 3:28 
GeneralRe: Question Pin
John M. Drescher24-Mar-04 7:24
memberJohn M. Drescher24-Mar-04 7:24 
GeneralRe: Question Pin
Anonymously24-Mar-04 7:30
sussAnonymously24-Mar-04 7:30 
GeneralRe: Question Pin
EasyWay31-Mar-04 4:45
memberEasyWay31-Mar-04 4:45 
GeneralRe: Question Pin
damnedyankee7-Nov-06 0:09
memberdamnedyankee7-Nov-06 0:09 
GeneralRe: Question Pin
Gyuri29-Jul-07 3:40
memberGyuri29-Jul-07 3: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.

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