WxService.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. if (CreatePoolInstance())
  16. {
  17. // 尝试10次启动;
  18. INT i = 10;
  19. while (0 == g_pAdoPool->InitializePool(Global::g_szDBSource, Global::g_dwDBServerPort, Global::g_szDBAccount, Global::g_szDBPassWord, Global::g_szDBName, Global::g_dwDBPoolDef, Global::g_dwDBPoolMaxCount))
  20. {
  21. Sleep(5000);
  22. if (i < 0)
  23. {
  24. break;
  25. }
  26. i--;
  27. }
  28. }
  29. WinService::GetDebugPriv();
  30. CMainProcess::GetInstance()->StartWork();
  31. }
  32. // 工作结束回调函数;
  33. void CALLBACK WorkEndof()
  34. {
  35. CMainProcess::GetInstance()->EndofWork();
  36. }
  37. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  38. {
  39. int nRetCode = 0;
  40. // 直接获得前景窗口的句柄;
  41. HWND hwnd = GetForegroundWindow();
  42. SendMessage(hwnd , WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL,MAKEINTRESOURCE(IDI_MICON)));
  43. HMODULE hModule = ::GetModuleHandle(NULL);
  44. // 初始化 MFC 并在失败时显示错误
  45. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  46. {
  47. // TODO: 更改错误代码以符合您的需要
  48. _tprintf(_T("错误: MFC 初始化失败\n"));
  49. nRetCode = 1;
  50. }
  51. else
  52. {
  53. Global::GetIniInfo();
  54. // 进程存在,直接退出;
  55. TCHAR szExeName[MAX_PATH] = {0};
  56. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  57. TCHAR szDir[_MAX_DIR] = { 0 };
  58. TCHAR szFna[_MAX_DIR] = { 0 };
  59. TCHAR szExt[_MAX_DIR] = { 0 };
  60. _tsplitpath_s(Global::g_szModuleFileName, szDrive, szDir, szFna, szExt);
  61. _stprintf_s(szExeName, _T("%s%s"), szFna, szExt);
  62. if ( Global::FindProcessCount(szExeName) > 1)
  63. {// 进程计数大于1,退出;
  64. return 0;
  65. }
  66. #if _DEBUG
  67. WorkStart();
  68. system("pause");
  69. WorkEndof();
  70. #else
  71. // 设置服务名和描述;
  72. _stprintf_s(WinService::g_scVariant.scName, _T("WxService"));
  73. _stprintf_s(WinService::g_scVariant.scDisplayName, _T("WxService"));
  74. _stprintf_s(WinService::g_scVariant.scDescription, _T("微信服务"));
  75. // 1.先判断服务是否存在;
  76. if ( WinService::IsInstalled())
  77. {
  78. // 卸载服务;
  79. if ( argc > 1)
  80. {
  81. if (_tcscmp((TCHAR *)argv[1], _T("/uninstall")) == 0)
  82. WinService::DoDeleteSvc(WinService::g_scVariant.scName);
  83. else
  84. printf(_T("未有卸载服务参数行\n"));
  85. system("pause");
  86. return 0;
  87. }
  88. else
  89. {
  90. WinService::SetCallBack(WorkStart, WorkEndof);
  91. SERVICE_TABLE_ENTRY st[] =
  92. {
  93. { WinService::g_scVariant.scName, (LPSERVICE_MAIN_FUNCTION)WinService::ServiceMain },
  94. { NULL, NULL }
  95. };
  96. if (!::StartServiceCtrlDispatcher(st)) // 在直接双击.exe时,这步会运行失败;
  97. {
  98. WinService::SvcReportEvent(_T("Register Service Main Function Error!"));
  99. printf(_T("双击无法安装\n"));
  100. system("pause");
  101. return 0;
  102. }
  103. }
  104. }
  105. // 2.不存在,安装服务;
  106. WinService::SetCallBack(WorkStart, WorkEndof);
  107. SERVICE_TABLE_ENTRY st[] =
  108. {
  109. { WinService::g_scVariant.scName, (LPSERVICE_MAIN_FUNCTION)WinService::ServiceMain },
  110. { NULL, NULL }
  111. };
  112. WinService::Install();
  113. #endif
  114. }
  115. return nRetCode;
  116. }