WxService.cpp 2.7 KB

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