Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

Service.h

Go to the documentation of this file.
00001 /*  Service.h - NT service helper
00002     Copyright (C) 2001-2004 Mark Weaver
00003     Written by Mark Weaver <mark@npsl.co.uk>
00004 
00005     Part of the Open-Win32 library.
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public
00017     License along with this library; if not, write to the
00018     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA  02111-1307, USA.
00020 */
00021 
00026 #ifndef OW32_Service_h
00027 #define OW32_Service_h
00028 
00029 #include <OW32/OW32Libs.h>
00030 #include <OW32/EventLog.h>
00031 #include <OW32/tstl.h>
00032 
00033 namespace OW32
00034 {
00035 
00039 class OW32_LIB_EXPORT CService
00040 {
00041 private:
00042     bool m_DebugFlag;
00043     bool m_Interactive;
00044 
00045     LPCTSTR m_ServiceName;
00046     LPCTSTR m_ServiceDescription;
00047     DWORD m_dwDesiredAccess;
00048     DWORD m_dwServiceType;
00049     DWORD m_dwStartType;
00050     DWORD m_dwErrorControl;
00051     LPCTSTR m_lpBinaryPathName;
00052     LPCTSTR m_lpLoadOrderGroup;
00053     LPCTSTR m_lpDependencies;
00054     LPCTSTR m_lpServiceStartName;
00055     LPCTSTR m_lpPassword;
00056 
00057     static CService* m_pThis;
00058     
00059 public:
00060     // Please note that strings are NOT copied; they must be static or
00061     // available for the lifetime of this object.
00062     CService(LPCTSTR pServiceName, LPCTSTR pServiceDescription, 
00063              DWORD dwDesiredAccess = STANDARD_RIGHTS_REQUIRED,
00064              DWORD dwServiceType = SERVICE_WIN32_OWN_PROCESS,
00065              DWORD dwStartType = SERVICE_AUTO_START,
00066              DWORD dwErrorControl = SERVICE_ERROR_NORMAL,
00067              LPCTSTR lpBinaryPathName = NULL,
00068              LPCTSTR lpLoadOrderGroup = NULL,
00069              LPCTSTR lpDependencies = NULL,
00070              LPCTSTR lpServiceStartName = NULL, 
00071              LPCTSTR lpPassword = NULL) :
00072         m_ServiceName(pServiceName),
00073         m_ServiceDescription(pServiceDescription),
00074         m_dwDesiredAccess(dwDesiredAccess),
00075         m_dwServiceType(dwServiceType),
00076         m_dwStartType(dwStartType),
00077         m_dwErrorControl(dwErrorControl),
00078         m_lpBinaryPathName(lpBinaryPathName),
00079         m_lpLoadOrderGroup(lpLoadOrderGroup),
00080         m_lpDependencies(lpDependencies),
00081         m_lpServiceStartName(lpServiceStartName), 
00082         m_lpPassword(lpPassword),
00083         m_hServiceStatus(NULL),
00084         m_DebugFlag(false),
00085         m_Interactive(false)
00086     {
00087         // TODO: assert that only one service exists!
00088         m_pThis = this;
00089     }
00090 
00091     // create/delete service
00092     virtual void Install();
00093     virtual void Uninstall();
00094 
00095     // Get the service name
00096     virtual LPCTSTR GetServiceName() const { return m_ServiceName; }
00097 
00098     // callable to start the service running (register with SCM, etc. - doesn't return)
00099     //virtual void Startup(); 
00100 
00101     // callable from user's main as a total service oriented replacment
00102     int _tmain(int argc, TCHAR* argv[]);
00103 
00104     // Main function for service (can be overridden if desired)
00105     virtual DWORD ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv) = 0;
00106 
00107     // The service handler.
00108     virtual void ServiceHandler(DWORD fdwControl);
00109 
00110     bool InDebugMode() { return m_DebugFlag; }
00111     void SetDebugMode(bool bDebugFlag) { m_DebugFlag = bDebugFlag; }
00112     bool InInteractiveMode() { return m_Interactive; }
00113     void SetInteractiveMode(bool bInteractive) { m_Interactive = bInteractive; }
00114 
00115     // Called when the stop signal is received
00116     virtual void Stop() {}
00117     // Called on pause
00118     virtual void Pause() {}
00119     // Called on continue
00120     virtual void Continue() {}
00121     // Called on system shutdown
00122     virtual void Shutdown() {}
00123     // Called on interrogate, defaults to "running"
00124     virtual void Interrogate() {
00125         UpdateSCM(SERVICE_RUNNING);
00126     }
00127 
00128     // utility function to advise the SCM as to the state of the service
00129     void UpdateSCM(DWORD state, DWORD dwCheckPoint = 0, DWORD dwWaitHint = 1000);
00130 
00131     // Service status handle, in case you want to do something
00132     // special the UpdateSCM doesn't cover.
00133     SERVICE_STATUS_HANDLE m_hServiceStatus;
00134 
00135     // event log object - available to derived class - will be opened if _tmain called
00136     CEventLog m_SysLog;
00137 
00138 private:
00139     // callbacks from Windows service entry points
00140     static void WINAPI ServiceMainCallback(DWORD dwArgc, LPTSTR* lpszArgv);
00141     static void WINAPI ServiceHandlerCallback(DWORD fdwControl);
00142 };
00143 
00144 } // namespace OW32
00145 
00146 #endif // OW32_Service_h

Generated on Sun Jun 5 01:29:18 2005 for OW32 by  doxygen 1.3.9.1