WxService.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. // 添加用户;
  34. tb_customer customer;
  35. customer.set_customer_phone(_T("15089231318"));
  36. customer.set_customer_password(_T("15089231318"));
  37. customer.set_customer_name(_T("jeff"));
  38. customer.set_customer_gender(true);
  39. customer.set_customer_gen_time(CWxAdoImpl::GetCurrentLocalTime().c_str());
  40. g_pAdoPool->AddCustomer((LPVOID)&customer);
  41. customer.Clear();
  42. g_pAdoPool->QueryUserDetail(_T("15089231318"), (LPVOID)&customer);
  43. // 添加回复类型;
  44. tb_type type;
  45. type.set_customer_id(customer.customer_id().c_str());
  46. type.set_type_name(_T("电脑类"));
  47. type.set_type_gen_time(CWxAdoImpl::GetCurrentLocalTime().c_str());
  48. g_pAdoPool->AddType((LPVOID)&type);
  49. // 添加回复详情;
  50. tb_reply reply;
  51. reply.set_customer_id(type.customer_id().c_str());
  52. reply.set_type_id(type.type_id().c_str());
  53. reply.set_reply_name(_T("电脑主板价格"));
  54. reply.set_reply_content(_T("当前价格100元人民币"));
  55. reply.set_reply_gen_time(CWxAdoImpl::GetCurrentLocalTime().c_str());
  56. g_pAdoPool->AddReply((LPVOID)&reply);
  57. #endif
  58. WinService::GetDebugPriv();
  59. CMainProcess::GetInstance()->StartWork();
  60. }
  61. // 工作结束回调函数;
  62. void CALLBACK WorkEndof()
  63. {
  64. FreelyfzodbcLibrary();
  65. CMainProcess *pObj = CMainProcess::GetInstance();
  66. if (pObj)
  67. {
  68. pObj->EndofWork();
  69. delete pObj;
  70. pObj = NULL;
  71. }
  72. }
  73. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  74. {
  75. int nRetCode = 0;
  76. _CrtSetBreakAlloc(165);
  77. // 直接获得前景窗口的句柄;
  78. HWND hwnd = GetForegroundWindow();
  79. SendMessage(hwnd , WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL,MAKEINTRESOURCE(IDI_MICON)));
  80. HMODULE hModule = ::GetModuleHandle(NULL);
  81. // 初始化 MFC 并在失败时显示错误
  82. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  83. {
  84. // TODO: 更改错误代码以符合您的需要
  85. _tprintf(_T("错误: MFC 初始化失败\n"));
  86. nRetCode = 1;
  87. }
  88. else
  89. {
  90. Global::GetIniInfo();
  91. // 进程存在,直接退出;
  92. TCHAR szExeName[MAX_PATH] = {0};
  93. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  94. TCHAR szDir[_MAX_DIR] = { 0 };
  95. TCHAR szFna[_MAX_DIR] = { 0 };
  96. TCHAR szExt[_MAX_DIR] = { 0 };
  97. _tsplitpath_s(Global::g_szModuleFileName, szDrive, szDir, szFna, szExt);
  98. _stprintf_s(szExeName, _T("%s%s"), szFna, szExt);
  99. if ( Global::FindProcessCount(szExeName) > 1)
  100. {// 进程计数大于1,退出;
  101. return 0;
  102. }
  103. #ifdef _DEBUG
  104. WorkStart();
  105. system("pause");
  106. WorkEndof();
  107. #else
  108. // 设置服务名和描述;
  109. _stprintf_s(WinService::g_scVariant.scName, _T("WxService"));
  110. _stprintf_s(WinService::g_scVariant.scDisplayName, _T("WxService"));
  111. _stprintf_s(WinService::g_scVariant.scDescription, _T("微信服务"));
  112. // 1.先判断服务是否存在;
  113. if ( WinService::IsInstalled())
  114. {
  115. // 卸载服务;
  116. if ( argc > 1)
  117. {
  118. if (_tcscmp((TCHAR *)argv[1], _T("/uninstall")) == 0)
  119. WinService::DoDeleteSvc(WinService::g_scVariant.scName);
  120. else
  121. printf(_T("未有卸载服务参数行\n"));
  122. system("pause");
  123. return 0;
  124. }
  125. else
  126. {
  127. WinService::SetCallBack(WorkStart, WorkEndof);
  128. SERVICE_TABLE_ENTRY st[] =
  129. {
  130. { WinService::g_scVariant.scName, (LPSERVICE_MAIN_FUNCTION)WinService::ServiceMain },
  131. { NULL, NULL }
  132. };
  133. if (!::StartServiceCtrlDispatcher(st)) // 在直接双击.exe时,这步会运行失败;
  134. {
  135. WinService::SvcReportEvent(_T("Register Service Main Function Error!"));
  136. printf(_T("双击无法安装\n"));
  137. system("pause");
  138. return 0;
  139. }
  140. }
  141. }
  142. // 2.不存在,安装服务;
  143. WinService::SetCallBack(WorkStart, WorkEndof);
  144. SERVICE_TABLE_ENTRY st[] =
  145. {
  146. { WinService::g_scVariant.scName, (LPSERVICE_MAIN_FUNCTION)WinService::ServiceMain },
  147. { NULL, NULL }
  148. };
  149. WinService::Install();
  150. #endif
  151. }
  152. return nRetCode;
  153. }