Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cNTService.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cNTService_H
7 #define _INC_cNTService_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "../GrayLibBase.h"
13 
14 #if defined(_WIN32) && !defined(UNDER_CE)
16 #include "GrayCore/include/cOSHandle.h" // HANDLE_NULL
18 #include <winsvc.h>
19 
20 namespace Gray
21 {
22  template <> inline void cHandlePtr<SC_HANDLE>::CloseHandle(SC_HANDLE h) // static
23  {
25  ::CloseServiceHandle(h); // ignored BOOL return.
26  }
27 }
28 
29 namespace GrayLib
30 {
31  class GRAYLIB_LINK cNTServiceHandle : public cHandlePtr < SC_HANDLE >
32  {
37 
38  typedef cHandlePtr<SC_HANDLE> SUPER_t;
39  public:
40  cNTServiceHandle(SC_HANDLE hSC = HANDLE_NULL)
41  : cHandlePtr<SC_HANDLE>(hSC)
42  {
43  }
44  ~cNTServiceHandle()
45  {
46  }
47  bool QueryServiceConfigX(QUERY_SERVICE_CONFIG* pCfg, DWORD dwSizeCfgMax, DWORD& rdwSizeCfgRet) const
48  {
50  if (!_FNF(::QueryServiceConfig)(get_Handle(), pCfg, dwSizeCfgMax, &rdwSizeCfgRet))
51  return false;
52  return true;
53  }
54  bool DeleteService()
55  {
56  return ::DeleteService(get_Handle());
57  }
58  bool ControlService(DWORD dwControl = SERVICE_CONTROL_STOP, LPSERVICE_STATUS lpServiceStatus = nullptr)
59  {
60  return ::ControlService(get_Handle(), dwControl, lpServiceStatus) ? true : false;
61  }
62  bool OpenServiceX(SC_HANDLE hSCMgr, const FILECHAR_t* pszServiceName, DWORD dwDesiredAccess = SERVICE_ALL_ACCESS)
63  {
65  AttachHandle(_FNF(::OpenService)(hSCMgr, pszServiceName, dwDesiredAccess));
66  return(isValidHandle());
67  }
68  };
69 
70  class GRAYLIB_LINK cNTServiceMgr : public cNTServiceHandle
71  {
77  public:
78  cNTServiceMgr(const FILECHAR_t* lpMachineName = nullptr, const FILECHAR_t* lpDatabaseName = nullptr, DWORD dwDesiredAccess = SC_MANAGER_ALL_ACCESS)
79  : cNTServiceHandle(_FNF(::OpenSCManager)(lpMachineName, lpDatabaseName, dwDesiredAccess))
80  {
81  // call isValidHandle();
82  }
83  ~cNTServiceMgr()
84  {
85  }
86  void OpenServiceMgr(const FILECHAR_t* lpMachineName = nullptr, const FILECHAR_t* lpDatabaseName = nullptr, DWORD dwDesiredAccess = SC_MANAGER_ALL_ACCESS)
87  {
88  // call isValidHandle(); after
89  AttachHandle(_FNF(::OpenSCManager)(lpMachineName, lpDatabaseName, dwDesiredAccess));
90  }
91  SC_HANDLE OpenServiceX(const FILECHAR_t* pszServiceName, DWORD dwDesiredAccess = SERVICE_ALL_ACCESS) const
92  {
97  return(_FNF(::OpenService)(get_Handle(), pszServiceName, dwDesiredAccess));
98  }
99  bool OpenServiceX(cNTServiceHandle& sh, const FILECHAR_t* pszServiceName, DWORD dwDesiredAccess = SERVICE_ALL_ACCESS) const
100  {
104  sh.AttachHandle( OpenServiceX(pszServiceName, dwDesiredAccess));
105  return(sh.isValidHandle());
106  }
107  };
108 };
109 #endif // _WIN32
110 #endif // _INC_CNTSERVICE_H
#define _FNF(c)
_WIN32 name has a A or W for UTF8 or UNICODE
Definition: FileName.h:24
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define HANDLE_NULL
Invalid OS handle for _WIN32. Not invalid OS handle for linux.
Definition: cOSHandle.h:21
void CloseHandle()
Definition: cHandlePtr.h:79
Definition: cMesh.h:22
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22