WxService.cpp 3.3 KB

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