WxService.cpp 3.8 KB

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