Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

A Wrapper Class for NT Services


August 1998/A Wrapper Class for NT Services/Listing 5

Listing 5: Implementation file for MyService class

#include <windows.h>
#include <winsock.h>
#include <stdlib.h>
#include <stdio.h>
#include <process.h>

#include "myserve.h"
#include "tapplog.h"

static const char szRegBasePath[] = 
    "SYSTEM\\CurrentControlSet\\Services\\";

MyService::MyService( 
    const char*              srvname, 
    LPSERVICE_MAIN_FUNCTION  fpSrvMain, 
    LPHANDLER_FUNCTION       fpSrvCtrl
    )
    :    NTService( srvname, fpSrvMain, fpSrvCtrl )
{
    mStat.dwControlsAccepted = 
        SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
#ifdef LOGGING
    m_log = new TAppLog( m_name, LOG_ALL );
#endif
}

MyService::~MyService( void )
{
#ifdef LOGGING
    delete m_log;
#endif
}

DWORD MyService::Init( DWORD ac, LPTSTR *av )
{
    LogEvent( LOG_INFORM, "MyServe init..." );
    mhMailslot = CreateMailslot( 
        "\\\\.\\mailslot\\myserve", //== mailslot name
        4096,                       //== maximum message size
        200,                        //== milliseconds to timeout
        NULL                        //== security attributes
    );
    
    if( mhMailslot == INVALID_HANDLE_VALUE ){
        Error( __LINE__ );
        return false;
    }

    mhOutfile = CreateFile( 
        "c:\\temp\\outfile.txt",    //== filename
        GENERIC_WRITE,              //== access type
        FILE_SHARE_READ,            //== share mode
        NULL,                       //== security mode
        CREATE_ALWAYS,              //== always creates the file
        FILE_ATTRIBUTE_NORMAL,      //== sets file attributes
        NULL                        //== template file
    );

    if( mhOutfile == INVALID_HANDLE_VALUE ){
        Error( __LINE__ );
        return false;
    }

    LogEvent( LOG_INFORM, "MyServe starting..." );
    return NO_ERROR;
}

int MyService::Run( void )
{
    DWORD   msg_sz = 0;
    DWORD   bytes_read = 0; 
    DWORD   bytes_written = 0;
    BOOL    aok; 
    char    buff[4096];
 
    m_running = true;            //== changed by OnStop()

    while( m_running ){
        aok = GetMailslotInfo(
            mhMailslot,         // mailslot handle 
            NULL,               // no maximum message size 
            &msg_sz,            // size of next message 
            NULL,               // number of messages 
            NULL                // no read time-out 
        );
        if( !aok )
            Error( __LINE__ );
                    
        if(aok && (msg_sz != MAILSLOT_NO_MESSAGE)) 
        {
            if( ReadFile( mhMailslot, buff, msg_sz, 
                    &bytes_read, NULL ))
                WriteFile( mhOutfile, buff, bytes_read, 
                    &bytes_written, NULL);
        }
      }
    
    CloseHandle( mhOutfile );
    CloseHandle( mhMailslot );
    return NO_ERROR; 
}

void MyService::OnStop( void )
{
    m_running = false;
}

#ifdef LOGGING
void MyService::LogEvent( LOG_LEVEL level, char* argstr, ... )
{
    va_list vl;
    va_start( vl, argstr );
    
    if( m_log )
        m_log->LogEventVL( level, argstr, vl );
}

void MyService::InstallAid( void )
{
    //== install the logging information
    if( m_log )
        m_log->Install();
}

void MyService::Error( long line = 0 )
{
    DWORD errval = GetLastError();
    LPVOID lpMsgBuf;

    FormatMessage( 
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM | 
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        GetLastError(),
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default 
                                                   // language
        (LPTSTR) &lpMsgBuf,
        0,
        NULL 
    );
    LogEvent( LOG_ERROR, "line (%d): %s", line, lpMsgBuf );
    LocalFree( lpMsgBuf );
}
#endif
//End of File

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.