WinService.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef __WINDOWS_SERVICE_20160113__
  2. #define __WINDOWS_SERVICE_20160113__
  3. #include <WinSvc.h>
  4. namespace WinService
  5. {
  6. // 工作启动回调函数;
  7. typedef void (CALLBACK *WorkStartCallback)();
  8. // 工作结束回调函数;
  9. typedef void (CALLBACK *WorkEndofCallback)();
  10. typedef struct SCVARIANT {
  11. TCHAR scName[MAX_PATH]; // 服务名称;
  12. TCHAR scDescription[MAX_PATH]; // 服务描述;
  13. HANDLE hscEvent; // 服务控制事件;
  14. SERVICE_STATUS scStatus; // 服务状态;
  15. SERVICE_STATUS_HANDLE scStatusHandle; // 服务状态句柄;
  16. WorkStartCallback lpStartWorkCallBack; // 开启工作者;
  17. WorkEndofCallback lpEndofWorkCallBack; // 结束工作者;
  18. SCVARIANT()
  19. {
  20. hscEvent = NULL;
  21. scStatusHandle = NULL;
  22. lpStartWorkCallBack = NULL;
  23. lpEndofWorkCallBack = NULL;
  24. memset(scName, 0, MAX_PATH * sizeof(TCHAR));
  25. memset(scDescription, 0, MAX_PATH * sizeof(TCHAR));
  26. // 设置服务类型;
  27. scStatus.dwServiceType =
  28. SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS; // | SERVICE_INTERACTIVE_PROCESS与桌面交互;
  29. // 设置服务默认状态为停止;
  30. scStatus.dwCurrentState = SERVICE_STOPPED; // SERVICE_RUNNING;
  31. // 设置服务接受停止控制;
  32. scStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  33. scStatus.dwWin32ExitCode = 0;
  34. scStatus.dwServiceSpecificExitCode = 0;
  35. scStatus.dwCheckPoint = 0;
  36. scStatus.dwWaitHint = 0;
  37. }
  38. }SCVariant, *pSCVariant;
  39. extern SCVariant g_scVariant;
  40. // 设置服务名称和描述名称;
  41. void InitServiceName(IN LPCTSTR lpServiceName = NULL, IN LPCTSTR lpServiceDescription = NULL);
  42. // 服务是否安装;
  43. BOOL IsInstalled();
  44. // 安装服务;
  45. BOOL Install();
  46. BOOL Uninstall();
  47. // windows服务程序入口函数;
  48. extern void WINAPI ServiceMain();
  49. // windows服务状态控制;
  50. extern void WINAPI SvcCtrlHandler(IN DWORD dwControlCode);
  51. // 初始化SOCKET库;
  52. extern int InitWinSock();
  53. // 服务日志事件;
  54. extern void SvcReportEvent(IN LPCTSTR pFormat, ...);
  55. // 向SCM报告服务状态;
  56. extern BOOL ReportSvcStatus(
  57. IN DWORD dwCurrentState,
  58. IN DWORD dwWin32ExitCode,
  59. IN DWORD dwServiceSpecificExitCode,
  60. IN DWORD dwCheckPoint,
  61. IN DWORD dwWaitHint);
  62. // 启动指定的服务;
  63. extern void StartSvc(IN LPCTSTR lpSvcName = NULL);
  64. extern void StopSvc(IN LPCTSTR lpSvcName = NULL);
  65. extern void KillService();
  66. // 设置回调函数指针;
  67. extern void SetCallBack(IN LPVOID lpStartWorkCallBack, IN LPVOID lpEndofWorkCallBack);
  68. // 在服务进程里调用外部exe;
  69. extern BOOL StartProcess(IN LPCTSTR lpExePath);
  70. // 服务日志;
  71. extern void LogEvent(IN LPCTSTR pFormat, ...);
  72. extern void UpdateSvcDacl(IN LPCTSTR lpSvcName);
  73. extern BOOL StopDependentServices();
  74. // 查询服务状态;
  75. extern void DoQuerySvc(IN LPCTSTR lpSvcName);
  76. // 更新服务描述;
  77. extern void DoUpdateSvcDesc(IN LPCTSTR lpSvcName);
  78. extern void DoDisableSvc(IN LPCTSTR lpSvcName);
  79. extern void DoEnableSvc(IN LPCTSTR lpSvcName);
  80. extern void DoDeleteSvc(IN LPCTSTR lpSvcName);
  81. //
  82. extern BOOL GetTokenByName(IN HANDLE &hToken, IN LPCTSTR lpName);
  83. //
  84. extern BOOL GetDebugPriv();
  85. //
  86. extern DWORD GetExplorerToken(OUT HANDLE* phExplorerToken);
  87. };
  88. #endif