Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cNTServiceImpl.h
Go to the documentation of this file.
1 //
5 //
6 
7 #ifndef _INC_cNTServiceImpl_H
8 #define _INC_cNTServiceImpl_H
9 #ifndef NO_PRAGMA_ONCE
10 #pragma once
11 #endif
12 
13 #include "cNTService.h"
14 
15 #if defined(_WIN32) && !defined(UNDER_CE) && ! defined(_MFC_VER)
16 #include "../Thread/cSysEvent.h"
19 
20 namespace GrayLib
21 {
22  enum NTS_CMD_TYPE
23  {
26 #define CNTSERVICECMD(a,b,c) NTS_CMD_##a,
27 #include "cNTServiceCmd.tbl"
28 #undef CNTSERVICECMD
29  NTS_CMD_QTY,
30  };
31 
32  class GRAYLIB_LINK cNTServiceImpl
33  : public cSingletonStatic< cNTServiceImpl >
34  {
40 
41  typedef cSingletonStatic< cNTServiceImpl > SUPER_t;
42 
43  public:
44  static const FILECHAR_t* const k_szCommandLines[NTS_CMD_QTY + 1];
45  const FILECHAR_t* m_pszNameSymbolic;
46  const FILECHAR_t* m_pszNameDesc;
47 
48  private:
49  bool m_bIsNTService;
50  bool m_bDebugService;
51 
52  cSysEvent m_StopEvent;
53  SERVICE_STATUS_HANDLE m_hStatusHandle;
54  SERVICE_STATUS m_Status;
55 
56  private:
57  HRESULT ServiceStart();
58  void ServiceMainStart();
59 
60  // Our exported API.
61  static void WINAPI ServiceHandler(DWORD dwCtrlCode);
62  static void WINAPI ServiceMain(DWORD argc, LPSTR* lpszArgv);
63 
64  protected:
65  // command line entries.
66  HRESULT CmdServiceInstall();
67  HRESULT CmdServiceRemove();
68  HRESULT CmdServiceDebug();
69  HRESULT CmdServiceRun();
70 
71  HRESULT CmdService();
72 
73  public:
74  cNTServiceImpl( const FILECHAR_t* pszName, const FILECHAR_t* pszNameDesc = nullptr );
75  virtual ~cNTServiceImpl();
76 
77  bool isRunningAsService() const
78  {
79  return m_bIsNTService;
80  }
81  StrLen_t MakeRegKey(FILECHAR_t* pszKey, const FILECHAR_t* pszExt = nullptr) const;
82 
83  void ReportEventX(WORD wType, DWORD dwMsgID, const GChar_t* lpszMsg, const GChar_t* lpszArgs = nullptr);
84  bool SetServiceStatus(DWORD dwCurrentState, LSTATUS dwWin32ExitCode, TIMESYSD_t nTimeHint = cTimeSys::k_FREQ);
85  void ServiceStop();
86 
87  // ASSUME called from external source.
88  bool onTickService();
89 
90  static cString GRAYCALL get_HelpText();
91  static bool GRAYCALL HasServiceCommandLine();
92  APP_EXITCODE_t Main();
93  };
94 }
95 
96 #ifdef GRAY_DLL // force implementation/instantiate for DLL/SO.
97 namespace Gray
98 {
99  template class GRAYLIB_LINK cSingletonStatic< GrayLib::cNTServiceImpl >;
100 }
101 #endif
102 
103 #endif // ! _MFC_VER
104 #endif // _INC_cNTServiceImpl_H
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
static const TIMESYS_t k_FREQ
milliSec per Sec
Definition: cTimeSys.h:100
Definition: cMesh.h:22
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
LONG LSTATUS
AKA error_status_t. FACILITY_WIN32 codes returned from RegCreateKeyEx() etc. Maybe NOT GetLastError()...
Definition: HResult.h:74
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
INT32 TIMESYSD_t
Time delta. signed milli-Seconds Span. cTimeSys::k_DMAX, cTimeSys::k_INF = MAILSLOT_WAIT_FOREVER.
Definition: cTimeSys.h:28
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22
char GChar_t
My version of TCHAR, _TCHAR.
Definition: StrConst.h:26
cStringT< GChar_t > cString
Definition: cString.h:636