WxService.cpp 3.1 KB

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