MainWork.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "StdAfx.h"
  2. #include ".\mainwork.h"
  3. HANDLE g_hCtrlThread = NULL;
  4. CRITICAL_SECTION g_CSLimitMod;
  5. DWORD g_nTickCount = 0;
  6. char g_szCurIniName[MAX_PATH]; // 当前配置文件名;
  7. CMainWork::CMainWork(void)
  8. {
  9. m_hMWConvertThread = NULL;
  10. InitializeCriticalSection(&g_CSLimitMod);
  11. g_hCtrlThread = CreateEvent(NULL,TRUE,FALSE,NULL);
  12. }
  13. CMainWork::~CMainWork(void)
  14. {
  15. if (g_hCtrlThread)
  16. SetEvent(g_hCtrlThread);
  17. if (WaitForSingleObject(g_hCtrlThread,INFINITE) != WAIT_TIMEOUT)
  18. {
  19. CloseHandle(m_hMWConvertThread);
  20. m_hMWConvertThread = NULL;
  21. }
  22. if (g_hCtrlThread != NULL)
  23. {
  24. CloseHandle(g_hCtrlThread);
  25. g_hCtrlThread = NULL;
  26. }
  27. DeleteCriticalSection(&g_CSLimitMod);
  28. WSACleanup();
  29. }
  30. void CMainWork::MainWorkStart()
  31. {
  32. sprintf_s(g_szCurIniName,260,"%s\\ControlService.ini",g_szModulePath);
  33. m_hMWConvertThread = CreateThread(NULL,0,ModeThreadProc,this,0,NULL);
  34. if (m_hMWConvertThread == NULL)
  35. {
  36. LOG4C((LOG_NOTICE,"创建模式线程失败"));
  37. return ;
  38. }
  39. }
  40. DWORD CMainWork::ModeThreadProc(LPVOID lpvoid)
  41. {
  42. CMainWork *pThis = (CMainWork*)lpvoid;
  43. do
  44. {
  45. pThis->StartSpecialService();
  46. pThis->StartSpecialProcess();
  47. } while (WaitForSingleObject(g_hCtrlThread, 150) == WAIT_TIMEOUT);
  48. return 0;
  49. }
  50. void CMainWork::StartSpecialService()
  51. {
  52. CIniReader IniReader(g_szCurIniName);
  53. IniReader.GetSectionData("Service");
  54. int iCount = IniReader.GetKeyCount();
  55. IniReader.GetSectionNames();
  56. // 遍历段下所有键值;
  57. for(int i = 0; i < iCount; i++)
  58. {
  59. CString strKey = "";
  60. strKey.Format("Svr%d",i+1);
  61. CString strKeyValue = "";
  62. strKeyValue = IniReader.GetKeyValueText(strKey,"Service");
  63. CString strSvrProName = GetIniSvrProName(strKeyValue);
  64. if ( !m_Procsee.SrvRunState(strSvrProName) )// 服务未启动;
  65. {
  66. LOG4C((LOG_NOTICE,"启动服务%s",strSvrProName));
  67. m_Procsee.SC_StartSvr(strSvrProName);
  68. Sleep(150);
  69. }
  70. }
  71. }
  72. void CMainWork::StartSpecialProcess()
  73. {
  74. CIniReader IniReader(g_szCurIniName);
  75. IniReader.GetSectionData("Process");
  76. int iCount = IniReader.GetKeyCount();
  77. IniReader.GetSectionNames();
  78. // 遍历段下所有键值;
  79. for(int i = 0; i < iCount; i++)
  80. {
  81. CString strKey = "";
  82. strKey.Format("Pro%d",i+1);
  83. CString strKeyValue = "";
  84. strKeyValue = IniReader.GetKeyValueText(strKey,"Process");
  85. CString strSvrProPath = GetIniSvrProPath(strKeyValue);
  86. CString strSvrProName = GetIniSvrProName(strKeyValue);
  87. // 启动指定进程;
  88. if ( !m_Procsee.IsProExist(strSvrProName) )
  89. {
  90. LOG4C((LOG_NOTICE,"启动进程%s",strSvrProName));
  91. m_Procsee.StartProcess(strSvrProPath);
  92. Sleep(1500);
  93. }
  94. }
  95. }
  96. // 获取 串时间;
  97. CString CMainWork::GetKeyValueTime(CString strKeyValue)
  98. {
  99. CString strTiem;
  100. int nIndex = strKeyValue.Find('|');
  101. strTiem = strKeyValue.Right(strKeyValue.GetLength() - nIndex -1);
  102. return strTiem;
  103. }
  104. // 获取 串文件名;
  105. CString CMainWork::GetKeyValueFileName(CString strKeyValue)
  106. {
  107. CString strFileName;
  108. int nIndex = strKeyValue.Find('|');
  109. strFileName = strKeyValue.Left(nIndex);
  110. return strFileName;
  111. }
  112. // 注意:IOServer.exe这个服务,在系统服务里,暂停是无效;
  113. // 所以,要停止IOServer这服务,只能从进程里结束;
  114. void CMainWork::CloseSpecialService()
  115. {
  116. CIniReader IniReader(g_szCurIniName);
  117. IniReader.GetSectionData("Service");
  118. int iCount = IniReader.GetKeyCount();
  119. IniReader.GetSectionNames();
  120. // 遍历段下所有键值;
  121. for(int i = 0; i < iCount; i++)
  122. {
  123. Sleep(1000);
  124. CString strKey = "";
  125. strKey.Format("Svr%d",i+1);
  126. CString strKeyValue = "";
  127. strKeyValue = IniReader.GetKeyValueText(strKey,"Service");
  128. CString strSvrProName = GetIniSvrProName(strKeyValue);
  129. // 停止指定服务;
  130. m_Procsee.SC_StopSvr(strSvrProName);
  131. //if (!m_Procsee.StopSrv(strSvrProName))
  132. //if (!m_Procsee.SC_StopSvr(strSvrProName))
  133. //LOG4C((LOG_NOTICE,"服务停止 失败 = %s" ,strSvrProName));
  134. // 并设置服务为手动,或禁止或卸载服务;
  135. if(!m_Procsee.SetSrvState(strSvrProName,SERVICE_DEMAND_START,NULL))
  136. LOG4C((LOG_NOTICE,"服务设置 失败 = %s",strSvrProName));
  137. }
  138. }
  139. void CMainWork::CloseSpecialProcess()
  140. {
  141. CIniReader IniReader(g_szCurIniName);
  142. IniReader.GetSectionData("Process");
  143. int iCount = IniReader.GetKeyCount();
  144. IniReader.GetSectionNames();
  145. // 遍历段下所有键值;
  146. for(int i = 0; i < iCount; i++)
  147. {
  148. Sleep(1000);
  149. CString strKey = "";
  150. strKey.Format("Pro%d",i+1);
  151. CString strKeyValue = "";
  152. strKeyValue = IniReader.GetKeyValueText(strKey,"Process");
  153. CString strSvrProName = GetIniSvrProName(strKeyValue);
  154. // 结束指定进程;
  155. if(!m_Procsee.CloseProcess(strSvrProName))
  156. LOG4C((LOG_NOTICE,"结束进程 失败 = %s",strSvrProName));
  157. }
  158. }
  159. void CMainWork::InstallSpecialService()
  160. {
  161. CIniReader IniReader(g_szCurIniName);
  162. IniReader.GetSectionData("Service");
  163. int iCount = IniReader.GetKeyCount();
  164. LOG4C((LOG_NOTICE,"iCount = %d",iCount));
  165. IniReader.GetSectionNames();
  166. // 遍历段下所有键值;
  167. for(int i = 0; i < iCount; i++)
  168. {
  169. Sleep(1000);
  170. CString strKey = "";
  171. strKey.Format("Svr%d",i+1);
  172. CString strKeyValue = "";
  173. strKeyValue = IniReader.GetKeyValueText(strKey,"Service");
  174. CString strSvrProName = GetIniSvrProName(strKeyValue);
  175. CString strSvrProPath = GetIniSvrProPath(strKeyValue);
  176. // .安装服务前,要检测下服务是否存在,这操作放在process类里做;
  177. // 安装指定服务(该安装方式是通过批处理安装的,非正常方式);
  178. if(!m_Procsee.StartProcess(strSvrProPath))
  179. LOG4C((LOG_NOTICE,"安装服务 失败 = %s", strSvrProPath));
  180. // 启动服务;
  181. m_Procsee.SC_StartSvr(strSvrProName);
  182. }
  183. }
  184. CString CMainWork::GetIniSvrProName(CString strKeyValue)
  185. {
  186. CString strSvrProName;
  187. int nIndex = strKeyValue.Find('|');
  188. strSvrProName = strKeyValue.Left(nIndex);
  189. return strSvrProName;
  190. }
  191. CString CMainWork::GetIniSvrProPath(CString strKeyValue)
  192. {
  193. CString strSvrProPath;
  194. int nIndex = strKeyValue.Find('|');
  195. strSvrProPath = strKeyValue.Right(strKeyValue.GetLength() - nIndex -1);
  196. return strSvrProPath;
  197. }