SOL9 2.0 Class: ServiceController

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * ServiceController.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9

#pragma once
// 2010/05/30

#include <sol/service/ServiceControl.h>

namespace SOL {

class ServiceController :public ServiceControl {

public:
    ServiceController(SC_HANDLE handle=NULL)
        :ServiceControl(handle)
    {
    }

public:
    ~ServiceController()
    {
    }

public:
    BOOL controlService(
        __in  DWORD             dwControl,
        __out LPSERVICE_STATUS  lpServiceStatus)
    {
        SC_HANDLE hService = getServiceControl();
        BOOL rc = FALSE;

        if ((rc = ControlService(
            hService,
            dwControl,
            lpServiceStatus)) == FALSE) {
            throw E_FAIL;
        }
        return rc;
    }

public:
    BOOL
        startService(
            __in    DWORD   dwNumServiceArgs,
            __inout LPCTSTR *lpServiceArgVectors)
    {
        SC_HANDLE hService = getServiceControl();
        BOOL rc = FALSE;
        if ((rc = StartService(
                hService,
                dwNumServiceArgs,
                lpServiceArgVectors)) == FALSE) {
            throw E_FAIL;
        }
        return rc;
    }

public:
    BOOL deleteService()
    {
        SC_HANDLE hService = getServiceControl();
        BOOL rc = FALSE;
        if ((rc = DeleteService(hService)) == FALSE) {
            throw E_FAIL;
        }
        return rc;
    }

public:
    /*
    dwServiceState takes one of the following values
    SERVICE_ACTIVE 
    SERVICE_INACTIVE
    SERVICE_STATE_ALL  
    */
    BOOL enumDependentServices(
        __in  DWORD                   dwServiceState,        
        __in  LPENUM_SERVICE_STATUS  lpServices,
        __in  DWORD                   cbBufSize,
        __out LPDWORD                 pcbBytesNeeded,
        __out LPDWORD                 lpServicesReturned)
    {
        SC_HANDLE hService = getServiceControl();
        BOOL rc = FALSE;
        if ((rc = EnumDependentServices(
            hService,
            dwServiceState,
            lpServices,
            cbBufSize,
            pcbBytesNeeded,
            lpServicesReturned)) == FALSE) {
            throw E_FAIL;
        }
        return rc;
    }

public:
    SERVICE_STATUS queryStatus()
    {
        SERVICE_STATUS status;
        ZeroMemory(&status, sizeof(status));
        
        SC_HANDLE hService = getServiceControl();

        if (QueryServiceStatus(hService, &status) == FALSE) {
            throw E_FAIL;    
        }
        return status;
    }

public:
    BOOL queryServiceStatusEx(
            __in  SC_STATUS_TYPE infoLevel,
            __in  LPBYTE         lpBuffer,
            __in  DWORD          cbBufSize,
            __out LPDWORD        pcbBytesNeeded)
    {
        SC_HANDLE hService = getServiceControl();

        BOOL rc = FALSE;
        if ((rc = QueryServiceStatusEx(
                hService,
                infoLevel,
                lpBuffer,
                cbBufSize,
                pcbBytesNeeded)) == FALSE) {
            throw E_FAIL; 
        }
        return rc;
    }

public:

    BOOL isServiceRunning()
    {
        BOOL rc = FALSE;
        SERVICE_STATUS status;
        ZeroMemory(&status, sizeof(status));

        SC_HANDLE hService = getServiceControl();

        if (QueryServiceStatus(hService, &status) == FALSE) {
            throw E_FAIL;    
        }
        if (status.dwCurrentState == SERVICE_RUNNING) {
            rc = TRUE;
        }
        return rc;
    }

public:
    DWORD getCurentState()
    {
        SERVICE_STATUS status;
        ZeroMemory(&status,sizeof(status));
        SC_HANDLE hService = getServiceControl();

        if (QueryServiceStatus(hService, &status ) == FALSE) {
            throw E_FAIL;    
        }
        return status.dwCurrentState;
    }

public:
    BOOL setServiceObjectSecurity(
        __in  SECURITY_INFORMATION    dwSecurityInformation,
        __in  PSECURITY_DESCRIPTOR    lpSecurityDescriptor)
    {
        SC_HANDLE hService = getServiceControl();
        BOOL rc = FALSE;
        if ((rc = SetServiceObjectSecurity(
                hService,
                dwSecurityInformation,
                lpSecurityDescriptor)) == FALSE) {
            throw E_FAIL;
        }
        return rc;
    }

public:
    BOOL changeServiceConfig(
        __in       DWORD    dwServiceType,
        __in       DWORD    dwStartType,
        __in       DWORD    dwErrorControl,
        __in_opt   LPCTSTR  lpBinaryPathName,
        __in_opt   LPCTSTR  lpLoadOrderGroup,
        __out_opt  LPDWORD  lpdwTagId,
        __in_opt   LPCTSTR  lpDependencies,
        __in_opt   LPCTSTR  lpServiceStartName,
        __in_opt   LPCTSTR  lpPassword,
        __in_opt   LPCTSTR  lpDisplayName)
    {
        SC_HANDLE hService = getServiceControl();
        BOOL rc = FALSE;
        if ((rc = ChangeServiceConfig(
            hService,
            dwServiceType,
            dwStartType,
            dwErrorControl,
            lpBinaryPathName,
            lpLoadOrderGroup,
            lpdwTagId,
            lpDependencies,
            lpServiceStartName,
            lpPassword,
            lpDisplayName)) == FALSE) {
            throw E_FAIL;
        }
        return rc;
    }

public:
    BOOL queryServiceConfig(
            __in  LPQUERY_SERVICE_CONFIG lpServiceConfig,
            __in  DWORD   cbBufSize,
            __out LPDWORD pcbBytesNeeded)
    {
        SC_HANDLE hService = getServiceControl();
        BOOL rc = FALSE;
        if ((rc = QueryServiceConfig(
                hService,
                lpServiceConfig,
                cbBufSize,
                pcbBytesNeeded)) == FALSE) {
            throw E_FAIL;
        }
        return rc;
    }


public:
    BOOL queryServiceConfig2(
            __in   DWORD   dwInfoLevel,
            __in   LPBYTE  lpBuffer,
            __in   DWORD   cbBufSize,
            __out  LPDWORD pcbBytesNeeded)
    {
        SC_HANDLE hService = getServiceControl();
        BOOL rc = FALSE;
        if ((rc = QueryServiceConfig2(
                    hService,
                    dwInfoLevel,
                    lpBuffer,
                    cbBufSize,
                    pcbBytesNeeded)) == FALSE) {
            throw E_FAIL;
        }
        return rc;
    }


public:
    BOOL queryServiceObjectSecurity(
            __in  SECURITY_INFORMATION    dwSecurityInformation,
            __in  PSECURITY_DESCRIPTOR    lpSecurityDescriptor,
            __in  DWORD                   cbBufSize,
            __out LPDWORD                 pcbBytesNeeded)
    {
        SC_HANDLE hService = getServiceControl();
        BOOL rc = FALSE;
        if ((rc = QueryServiceObjectSecurity(
                    hService,
                    dwSecurityInformation,
                    lpSecurityDescriptor,
                    cbBufSize,
                    pcbBytesNeeded)) == FALSE) {
            throw E_FAIL;
        }
        return rc;
    }


#if (NTDDI_VERSION >= NTDDI_VISTA)

public:

    DWORD notifyServiceStatusChange (
        __in  DWORD            dwNotifyMask,
        __in  PSERVICE_NOTIFY  pNotifyBuffer)
    {
        SC_HANDLE hService = getServiceControl();
        return NotifyServiceStatusChange (
            hService,
            dwNotifyMask,
            pNotifyBuffer);
    }

public:
    BOOL controlServiceEx(
        __in    DWORD  dwControl,
        __in    DWORD  dwInfoLevel,
        __inout PVOID  pControlParams)
    {
        SC_HANDLE hService = getServiceControl();
        BOOL rc = FALSE;
        if ((rc = ControlServiceEx(
            hService,
            dwControl,
            dwInfoLevel,
            pControlParams)) == FALSE) {
            throw E_FAIL;
        }
        return rc;
    }

#endif // NTDDI_VERSION >= NTDDI_VISTA
    

};


}

Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.