WxService.cpp 4.2 KB

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