WxService.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // WxService.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "WxService.h"
  5. #include "MainProcess.h"
  6. #define _CRTDBG_MAP_ALLOC
  7. #include<stdlib.h>
  8. #include<crtdbg.h>
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #endif
  12. // 唯一的应用程序对象
  13. CWinApp theApp;
  14. using namespace std;
  15. // 工作启动回调函数;
  16. void CALLBACK WorkStart()
  17. {
  18. if (CreatePoolInstance())
  19. {
  20. // 尝试10次启动;
  21. INT i = 10;
  22. 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))
  23. {
  24. Sleep(5000);
  25. if (i < 0)
  26. {
  27. break;
  28. }
  29. i--;
  30. }
  31. }
  32. #ifdef _DEBUG
  33. tb_customer customer;
  34. TCHAR szMax[MAX_PATH] = { 0 };
  35. TString str;
  36. g_pAdoPool->IsUserExist(_T("13352667321"), _T("123456"), (LPVOID)&customer);
  37. customer.set_customer_phone(_T("1508923138"));
  38. customer.set_customer_password(_T("1508923138"));
  39. g_pAdoPool->AddCustomer((LPVOID)&customer);
  40. #endif
  41. WinService::GetDebugPriv();
  42. CMainProcess::GetInstance()->StartWork();
  43. }
  44. // 工作结束回调函数;
  45. void CALLBACK WorkEndof()
  46. {
  47. FreelyfzodbcLibrary();
  48. CMainProcess *pObj = CMainProcess::GetInstance();
  49. if (pObj)
  50. {
  51. pObj->EndofWork();
  52. delete pObj;
  53. pObj = NULL;
  54. }
  55. }
  56. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  57. {
  58. int nRetCode = 0;
  59. _CrtSetBreakAlloc(165);
  60. // 直接获得前景窗口的句柄;
  61. HWND hwnd = GetForegroundWindow();
  62. SendMessage(hwnd , WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL,MAKEINTRESOURCE(IDI_MICON)));
  63. HMODULE hModule = ::GetModuleHandle(NULL);
  64. // 初始化 MFC 并在失败时显示错误
  65. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  66. {
  67. // TODO: 更改错误代码以符合您的需要
  68. _tprintf(_T("错误: MFC 初始化失败\n"));
  69. nRetCode = 1;
  70. }
  71. else
  72. {
  73. Global::GetIniInfo();
  74. // 进程存在,直接退出;
  75. TCHAR szExeName[MAX_PATH] = {0};
  76. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  77. TCHAR szDir[_MAX_DIR] = { 0 };
  78. TCHAR szFna[_MAX_DIR] = { 0 };
  79. TCHAR szExt[_MAX_DIR] = { 0 };
  80. _tsplitpath_s(Global::g_szModuleFileName, szDrive, szDir, szFna, szExt);
  81. _stprintf_s(szExeName, _T("%s%s"), szFna, szExt);
  82. if ( Global::FindProcessCount(szExeName) > 1)
  83. {// 进程计数大于1,退出;
  84. return 0;
  85. }
  86. #ifdef _DEBUG
  87. WorkStart();
  88. system("pause");
  89. WorkEndof();
  90. #else
  91. // 设置服务名和描述;
  92. _stprintf_s(WinService::g_scVariant.scName, _T("WxService"));
  93. _stprintf_s(WinService::g_scVariant.scDisplayName, _T("WxService"));
  94. _stprintf_s(WinService::g_scVariant.scDescription, _T("微信服务"));
  95. // 1.先判断服务是否存在;
  96. if ( WinService::IsInstalled())
  97. {
  98. // 卸载服务;
  99. if ( argc > 1)
  100. {
  101. if (_tcscmp((TCHAR *)argv[1], _T("/uninstall")) == 0)
  102. WinService::DoDeleteSvc(WinService::g_scVariant.scName);
  103. else
  104. printf(_T("未有卸载服务参数行\n"));
  105. system("pause");
  106. return 0;
  107. }
  108. else
  109. {
  110. WinService::SetCallBack(WorkStart, WorkEndof);
  111. SERVICE_TABLE_ENTRY st[] =
  112. {
  113. { WinService::g_scVariant.scName, (LPSERVICE_MAIN_FUNCTION)WinService::ServiceMain },
  114. { NULL, NULL }
  115. };
  116. if (!::StartServiceCtrlDispatcher(st)) // 在直接双击.exe时,这步会运行失败;
  117. {
  118. WinService::SvcReportEvent(_T("Register Service Main Function Error!"));
  119. printf(_T("双击无法安装\n"));
  120. system("pause");
  121. return 0;
  122. }
  123. }
  124. }
  125. // 2.不存在,安装服务;
  126. WinService::SetCallBack(WorkStart, WorkEndof);
  127. SERVICE_TABLE_ENTRY st[] =
  128. {
  129. { WinService::g_scVariant.scName, (LPSERVICE_MAIN_FUNCTION)WinService::ServiceMain },
  130. { NULL, NULL }
  131. };
  132. WinService::Install();
  133. #endif
  134. }
  135. return nRetCode;
  136. }