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