/*=========================================================================== (c) Copyright 1999, Emmanuel KARTMANN, all rights reserved =========================================================================== File : ServiceManager.h $Header: $ Author : Emmanuel KARTMANN Creation : Wednesday 12/1/99 4:57:15 PM Remake : ------------------------------- Description ------------------------------- Declaration of the CServiceManager class. ------------------------------ Modifications ------------------------------ $Log: $ =========================================================================== */ #if !defined(AFX_SERVICEMANAGER_H__066D9109_A7C8_11D3_BFE2_0010E3B966CE__INCLUDED_) #define AFX_SERVICEMANAGER_H__066D9109_A7C8_11D3_BFE2_0010E3B966CE__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include typedef enum _NTSAction { NTS_LIST_PRINT, NTS_LIST_PRINT_NO_HEADER, NTS_LIST_STOP, NTS_LIST_START } NTSAction; /* ----------------------------------------------------------------- CLASS CServiceManager Provides enhanced remote control functions for Windows NT Services. DESCRIPTION This class enhanced remote control functions for Windows NT Services. USAGE To use this class: Note: By default, this class prints log/error messages in stdout. To change this behavior, you must derive this class and override functions Print() and PrintServiceInfo() with your own in the derived class. EXAMPLE
    CServiceManager oServiceManager;

    CString szMyRemoteMachine = "\\\\remotename";
    CString szMyServiceName = "MyService";

    if (oServiceManager.OpenService(szMyRemoteMachine, szMyServiceName)) {
        if (oServiceManager.StartService(NULL, // Use currently opened machine
                                         NULL, // Use currently opened service
                                         TRUE  // Start dependent services
                                         )) {

            cout << "Service started!\n";

        } else {

            cerr << "Cannot start service " << szMyServiceName << " on host " << szMyRemoteMachine << "\n";

        }
    } else {

        cerr << "Cannot connect to service " << szMyServiceName << " on host " << szMyRemoteMachine << "\n";

    }

ADMINISTRATIVE Author Emmanuel KARTMANN Date Monday 12/6/99 SEE ALSO ----------------------------------------------------------------- */ class CServiceManager { public: ///////////////////////////////////////////////////////////////////// // // Purpose: create an instance of the class // // Parameters: none (C++ constructor) // // Return value : none (C++ constructor) // // Description : // CServiceManager(); ///////////////////////////////////////////////////////////////////// // // Purpose: delete an instance of the class // // Parameters: none (C++ destructor) // // Return value : none (C++ destructor) // // Description : // virtual ~CServiceManager(); ///////////////////////////////////////////////////////////////////// // // Purpose: open a service (possibly remote) // // Parameters: // // [in] lpszMachineName // name of the remote machine // [in] lpszServiceName // name of the service (internal name OR display name) // // Return value : BOOL = TRUE for success, FALSE otherwise // // Description : // BOOL Open(LPCTSTR lpszMachineName, LPCTSTR lpszServiceName); ///////////////////////////////////////////////////////////////////// // // Purpose: start a service (possibly remote) // // Parameters: // // [in] lpszMachineName // name of the remote machine, or NULL // [in] lpszServiceName // name of the service (internal name OR display name), or NULL // [in] bStartDependents // flag, TRUE if dependent service must be started too. // // Return value : BOOL = TRUE for success, FALSE otherwise // // Description : If any of the parameters are NULL, this // function reuses the currently open SCM/Service. // BOOL StartService(LPCTSTR lpszMachineName = NULL, LPCTSTR lpszServiceName = NULL, BOOL bStartDependents = FALSE); ///////////////////////////////////////////////////////////////////// // // Purpose: stop a service (possibly remote) // // Parameters: // // [in] lpszMachineName // name of the remote machine // [in] lpszServiceName // name of the service (internal name OR display name) // [in] bStopDependents // flag, TRUE if dependent service must be stopped too. // // Return value : BOOL = TRUE for success, FALSE otherwise // // Description : If any of the parameters are NULL, this // function reuses the currently open SCM/Service. // BOOL StopService(LPCTSTR lpszMachineName = NULL, LPCTSTR lpszServiceName = NULL, BOOL bStopDependents = FALSE); ///////////////////////////////////////////////////////////////////// // // Purpose: fetch service information (possibly remote) // // Parameters: // // [in] lpszMachineName // name of the remote machine, or NULL // [in] lpszServiceName // name of the service (internal name OR display name), or NULL // // Return value : BOOL = TRUE for success, FALSE otherwise // // Description : If any of the parameters are NULL, this // function reuses the currently open SCM/Service. // BOOL QueryService(LPCTSTR lpszMachineName = NULL, LPCTSTR lpszServiceName = NULL); ///////////////////////////////////////////////////////////////////// // // Purpose: list the services installed on a machine (possibly remote) // // Parameters: // // [in] lpszMachineName // name of the remote machine, or NULL // // Return value : BOOL = TRUE for success, FALSE otherwise // // Description : If the parameter is NULL, this // function reuses the currently open SCM. // BOOL ListServices(LPCTSTR lpszMachineName = NULL); ///////////////////////////////////////////////////////////////////// // // Purpose: print formatted data // // Parameters: // // [in] lpszFormat // printf-like format of the data (e.g. "%s %s %s") // [in] lpszParam1 // 1st parameter for the data // [in] lpszParam2 // 2nd parameter for the data // [in] lpszParam3 // 3rd parameter for the data // [in] lpszParam4 // 4th parameter for the data // [in] lpszParam5 // 5th parameter for the data // // Return value : BOOL = TRUE for success, FALSE otherwise // // Description : Derive this class and override this function // if you don't want to print data in stdout. // virtual BOOL Print(LPCTSTR lpszFormat, LPCTSTR lpszParam1 = NULL, LPCTSTR lpszParam2 = NULL, LPCTSTR lpszParam3 = NULL, LPCTSTR lpszParam4 = NULL, LPCTSTR lpszParam5 = NULL); ///////////////////////////////////////////////////////////////////// // // Purpose: // // Parameters: // // [in] nCount // service number // [in] lpszServiceName // service name (internal name) // [in] lpszDisplayName // service display name // [in] ServiceStatus // pointer // [in] lpServiceConfig // pointer to service configuration info // // Return value : BOOL = TRUE for success, FALSE otherwise // // Description : Derive this class and override this function // if you don't want to print data in stdout. // virtual BOOL PrintServiceInfo(int nCount, SERVICE_STATUS *ServiceStatus, LPCTSTR lpszServiceName, LPCTSTR lpszDisplayName, LPQUERY_SERVICE_CONFIG lpServiceConfig = NULL); ///////////////////////////////////////////////////////////////////// // // Purpose: open SCM on a machine (possibly remote) // // Parameters: // // [in] lpszMachineName // name of the remote machine, or NULL // // Return value : BOOL = TRUE for success, FALSE otherwise // // Description : // BOOL OpenServiceControlManager(LPCTSTR lpszMachineName = NULL); ///////////////////////////////////////////////////////////////////// // // Purpose: open a service on a machine // // Parameters: // // [in] lpszServiceName // name of the service, or NULL // // Return value : BOOL = TRUE for success, FALSE otherwise // // Description : // BOOL OpenService(LPCTSTR lpszServiceName = NULL); protected: BOOL CloseServiceControlManager(void); BOOL CloseService(void); LPSTR GetStatusString(DWORD dwServiceStatus); LPSTR GetStartupString(DWORD dwServiceStartupType); BOOL QueryServiceConfiguration(LPCTSTR lpszMachineName = NULL, LPCTSTR lpszServiceName = NULL); BOOL QueryDependentServices(LPCTSTR lpszMachineName, LPCTSTR lpszServiceName, NTSAction nServiceAction = NTS_LIST_PRINT); LPQUERY_SERVICE_CONFIG QueryServiceConfiguration(SC_HANDLE m_schSCManager, SC_HANDLE m_schService); BOOL StopService(SC_HANDLE m_schSCManager, SC_HANDLE m_schService); BOOL StartService(SC_HANDLE m_schSCManager, SC_HANDLE m_schService); void SetMachineName(LPCTSTR lpszMachineName); void SetServiceName(LPCTSTR lpszServiceName); SC_HANDLE m_schSCManager; SC_HANDLE m_schService; TCHAR m_lpszMachineName[256]; TCHAR m_lpszServiceName[256]; static LPTSTR GetErrorText(DWORD errorCode, LPTSTR lpszBuf, DWORD dwSize); }; #endif // !defined(AFX_SERVICEMANAGER_H__066D9109_A7C8_11D3_BFE2_0010E3B966CE__INCLUDED_)