SrvManager.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma once
  2. #include <windows.h>
  3. #include <winsvc.h>
  4. #include <winioctl.h>
  5. #ifndef _CTYPE_DISABLE_MACROS
  6. #define _CTYPE_DISABLE_MACROS
  7. #endif
  8. const int MAX_ERR_MSG_SIZE = 256;
  9. const char SERVICE_WWAN_AutoConfig[] = "WwanSvc";
  10. struct LastErrorInfo
  11. {
  12. DWORD dwErrMsgLen;
  13. DWORD dwLastErrCode;
  14. char szErrMsg[MAX_ERR_MSG_SIZE];
  15. };
  16. enum ServiceStatus
  17. {
  18. SERVICE_STATUS_UNKNOWN,
  19. SERVICE_STATUS_RUNNING,
  20. SERVICE_STATUS_STOPPED,
  21. SERVICE_STATUS_PASUSED
  22. };
  23. class CSvcManager
  24. {
  25. public:
  26. static CSvcManager* GetInstance();
  27. public:
  28. ~CSvcManager(void);
  29. BOOLEAN Init();
  30. BOOLEAN Release();
  31. BOOLEAN InstallService(LPCTSTR szSrvName, LPCTSTR szServiceExe);
  32. /*
  33. The name of the service to be opened.
  34. This is the name specified by the lpServiceName parameter of the CreateService function when the service
  35. object was created, not the service display name that is shown by user interface applications to identify
  36. the service.
  37. */
  38. BOOLEAN StartServiceWith(LPCTSTR szSrvName);
  39. BOOLEAN StopService(LPCTSTR szSrvName);
  40. BOOLEAN RemoveService(LPCTSTR szSrvName);
  41. BOOLEAN QueryServiceStatus(LPCTSTR szSrvName, ServiceStatus& status);
  42. BOOLEAN StopDependentServices(LPCTSTR szSrvName);
  43. void GetLastErrMsg(LastErrorInfo& lastInfo);
  44. BOOLEAN RaiseUpPrivileges();
  45. private:
  46. static CSvcManager* m_pInstance;
  47. SC_HANDLE m_schSCManager;
  48. BOOLEAN m_bInit;
  49. LastErrorInfo m_LastErrInfo;
  50. CSvcManager(void);
  51. void SaveLastErrMsg(LPCTSTR fmt, ...);
  52. void CoverLastErrMsg()
  53. {
  54. m_LastErrInfo.dwErrMsgLen = strlen("Last Operation succ.");
  55. memset(m_LastErrInfo.szErrMsg, 0, sizeof(CHAR)*MAX_ERR_MSG_SIZE);
  56. memcpy_s(m_LastErrInfo.szErrMsg, MAX_ERR_MSG_SIZE,
  57. "Last Operation succ.", strlen("Last Operation succ."));
  58. m_LastErrInfo.dwLastErrCode = 0;
  59. }
  60. BOOLEAN SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege);
  61. };