WatchService.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // WatchService.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "WatchService.h"
  5. #include "WindowService.h"
  6. //////////////////////////////////////////////////////////////////////////
  7. // 包含主要处理类;
  8. #include "MainProcess.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #endif
  12. // 唯一的应用程序对象
  13. CWinApp theApp;
  14. using namespace std;
  15. // 工作启动回调函数;
  16. void CALLBACK WorkStart()
  17. {
  18. #ifdef _DEBUG
  19. Sleep(8000);
  20. #endif
  21. WindowsService::GetDebugPriv();
  22. CMainProcess::GetInstance()->StartWork();
  23. }
  24. // 工作结束回调函数;
  25. void CALLBACK WorkEndof()
  26. {
  27. CMainProcess::GetInstance()->EndofWork();
  28. }
  29. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  30. {
  31. int nRetCode = 0;
  32. // 直接获得前景窗口的句柄;
  33. HWND hwnd = GetForegroundWindow();
  34. SendMessage(hwnd , WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL,MAKEINTRESOURCE(IDI_MICON)));
  35. HMODULE hModule = ::GetModuleHandle(NULL);
  36. // 初始化 MFC 并在失败时显示错误
  37. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  38. {
  39. // TODO: 更改错误代码以符合您的需要
  40. _tprintf(_T("错误: MFC 初始化失败\n"));
  41. nRetCode = 1;
  42. }
  43. else
  44. {
  45. Global::GetIniInfo();
  46. // 进程存在,直接退出;
  47. TCHAR szExeName[MAX_PATH] = {0};
  48. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  49. TCHAR szDir[_MAX_DIR] = { 0 };
  50. TCHAR szFna[_MAX_DIR] = { 0 };
  51. TCHAR szExt[_MAX_DIR] = { 0 };
  52. _tsplitpath_s(Global::g_szModuleFileName, szDrive, szDir, szFna, szExt);
  53. _stprintf_s(szExeName, _T("%s%s"), szFna, szExt);
  54. if ( Global::FindProcessCount(szExeName) > 1)
  55. {// 进程计数大于1,退出;
  56. return 0;
  57. }
  58. // 设置服务名和描述;
  59. _stprintf_s(WindowsService::g_szSvcName, _T("Watch Service"));
  60. _stprintf_s(WindowsService::g_szlpSvrDescription, _T("监控服务"));
  61. // 1.先判断服务是否存在;
  62. if ( WindowsService::IsSvcInstalled())
  63. {
  64. // 卸载服务;
  65. if ( argc > 1)
  66. {
  67. if (_tcscmp((TCHAR *)argv[1], _T("/uninstall")) == 0)
  68. WindowsService::DoDeleteSvc(WindowsService::g_szSvcName);
  69. else
  70. _tprintf_s(_T("未有卸载服务参数行\n"));
  71. return 0;
  72. }
  73. else
  74. {
  75. WindowsService::InitSvcVar();
  76. WindowsService::SetCallBack(WorkStart, WorkEndof);
  77. SERVICE_TABLE_ENTRY st[] =
  78. {
  79. { WindowsService::g_szSvcName, (LPSERVICE_MAIN_FUNCTION)WindowsService::ServiceMain },
  80. { NULL, NULL }
  81. };
  82. if (!::StartServiceCtrlDispatcher(st)) // 在直接双击.exe时,这步会运行失败;
  83. {
  84. //LOG4C((LOG_NOTICE,"服务已安装,须系统运行控制"));
  85. WindowsService::SvcReportEvent(_T("Register Service Main Function Error!"));
  86. return 0;
  87. }
  88. }
  89. }
  90. // 2.不存在,安装服务;
  91. WindowsService::InitSvcVar();
  92. WindowsService::SetCallBack(WorkStart, WorkEndof);
  93. SERVICE_TABLE_ENTRY st[] =
  94. {
  95. { WindowsService::g_szSvcName, (LPSERVICE_MAIN_FUNCTION)WindowsService::ServiceMain },
  96. { NULL, NULL }
  97. };
  98. WindowsService::SvcInstall();
  99. }
  100. return nRetCode;
  101. }