WxService.cpp 3.2 KB

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