123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- #include "StdAfx.h"
- #include ".\mainwork.h"
- HANDLE g_hCtrlThread = NULL;
- CRITICAL_SECTION g_CSLimitMod;
- DWORD g_nTickCount = 0;
- char g_szCurIniName[MAX_PATH];
- CMainWork::CMainWork(void)
- {
- m_hMWConvertThread = NULL;
- InitializeCriticalSection(&g_CSLimitMod);
- g_hCtrlThread = CreateEvent(NULL,TRUE,FALSE,NULL);
- }
- CMainWork::~CMainWork(void)
- {
- if (g_hCtrlThread)
- SetEvent(g_hCtrlThread);
- if (WaitForSingleObject(g_hCtrlThread,INFINITE) != WAIT_TIMEOUT)
- {
- CloseHandle(m_hMWConvertThread);
- m_hMWConvertThread = NULL;
- }
- if (g_hCtrlThread != NULL)
- {
- CloseHandle(g_hCtrlThread);
- g_hCtrlThread = NULL;
- }
- DeleteCriticalSection(&g_CSLimitMod);
- WSACleanup();
- }
- void CMainWork::MainWorkStart()
- {
- sprintf_s(g_szCurIniName,260,"%s\\ControlService.ini",g_szModulePath);
- m_hMWConvertThread = CreateThread(NULL,0,ModeThreadProc,this,0,NULL);
- if (m_hMWConvertThread == NULL)
- {
- LOG4C((LOG_NOTICE,"创建模式线程失败"));
- return ;
- }
- }
- DWORD CMainWork::ModeThreadProc(LPVOID lpvoid)
- {
- CMainWork *pThis = (CMainWork*)lpvoid;
- do
- {
- pThis->StartSpecialService();
- pThis->StartSpecialProcess();
- } while (WaitForSingleObject(g_hCtrlThread, 150) == WAIT_TIMEOUT);
- return 0;
- }
- void CMainWork::StartSpecialService()
- {
- CIniReader IniReader(g_szCurIniName);
- IniReader.GetSectionData("Service");
- int iCount = IniReader.GetKeyCount();
- IniReader.GetSectionNames();
-
- for(int i = 0; i < iCount; i++)
- {
- CString strKey = "";
- strKey.Format("Svr%d",i+1);
- CString strKeyValue = "";
- strKeyValue = IniReader.GetKeyValueText(strKey,"Service");
- CString strSvrProName = GetIniSvrProName(strKeyValue);
- if ( !m_Procsee.SrvRunState(strSvrProName) )
- {
- LOG4C((LOG_NOTICE,"启动服务%s",strSvrProName));
- m_Procsee.SC_StartSvr(strSvrProName);
- Sleep(150);
- }
- }
- }
- void CMainWork::StartSpecialProcess()
- {
- CIniReader IniReader(g_szCurIniName);
- IniReader.GetSectionData("Process");
- int iCount = IniReader.GetKeyCount();
- IniReader.GetSectionNames();
-
- for(int i = 0; i < iCount; i++)
- {
- CString strKey = "";
- strKey.Format("Pro%d",i+1);
- CString strKeyValue = "";
- strKeyValue = IniReader.GetKeyValueText(strKey,"Process");
- CString strSvrProPath = GetIniSvrProPath(strKeyValue);
- CString strSvrProName = GetIniSvrProName(strKeyValue);
-
- if ( !m_Procsee.IsProExist(strSvrProName) )
- {
- LOG4C((LOG_NOTICE,"启动进程%s",strSvrProName));
- m_Procsee.StartProcess(strSvrProPath);
- Sleep(1500);
- }
- }
- }
- CString CMainWork::GetKeyValueTime(CString strKeyValue)
- {
- CString strTiem;
- int nIndex = strKeyValue.Find('|');
- strTiem = strKeyValue.Right(strKeyValue.GetLength() - nIndex -1);
- return strTiem;
- }
- CString CMainWork::GetKeyValueFileName(CString strKeyValue)
- {
- CString strFileName;
- int nIndex = strKeyValue.Find('|');
- strFileName = strKeyValue.Left(nIndex);
- return strFileName;
- }
- void CMainWork::CloseSpecialService()
- {
- CIniReader IniReader(g_szCurIniName);
- IniReader.GetSectionData("Service");
- int iCount = IniReader.GetKeyCount();
- IniReader.GetSectionNames();
-
- for(int i = 0; i < iCount; i++)
- {
- Sleep(1000);
- CString strKey = "";
- strKey.Format("Svr%d",i+1);
- CString strKeyValue = "";
- strKeyValue = IniReader.GetKeyValueText(strKey,"Service");
- CString strSvrProName = GetIniSvrProName(strKeyValue);
-
- m_Procsee.SC_StopSvr(strSvrProName);
-
-
-
-
- if(!m_Procsee.SetSrvState(strSvrProName,SERVICE_DEMAND_START,NULL))
- LOG4C((LOG_NOTICE,"服务设置 失败 = %s",strSvrProName));
- }
- }
- void CMainWork::CloseSpecialProcess()
- {
- CIniReader IniReader(g_szCurIniName);
- IniReader.GetSectionData("Process");
- int iCount = IniReader.GetKeyCount();
- IniReader.GetSectionNames();
-
- for(int i = 0; i < iCount; i++)
- {
- Sleep(1000);
- CString strKey = "";
- strKey.Format("Pro%d",i+1);
- CString strKeyValue = "";
- strKeyValue = IniReader.GetKeyValueText(strKey,"Process");
- CString strSvrProName = GetIniSvrProName(strKeyValue);
-
- if(!m_Procsee.CloseProcess(strSvrProName))
- LOG4C((LOG_NOTICE,"结束进程 失败 = %s",strSvrProName));
- }
- }
- void CMainWork::InstallSpecialService()
- {
- CIniReader IniReader(g_szCurIniName);
- IniReader.GetSectionData("Service");
- int iCount = IniReader.GetKeyCount();
- LOG4C((LOG_NOTICE,"iCount = %d",iCount));
- IniReader.GetSectionNames();
-
- for(int i = 0; i < iCount; i++)
- {
- Sleep(1000);
- CString strKey = "";
- strKey.Format("Svr%d",i+1);
- CString strKeyValue = "";
- strKeyValue = IniReader.GetKeyValueText(strKey,"Service");
- CString strSvrProName = GetIniSvrProName(strKeyValue);
- CString strSvrProPath = GetIniSvrProPath(strKeyValue);
-
-
- if(!m_Procsee.StartProcess(strSvrProPath))
- LOG4C((LOG_NOTICE,"安装服务 失败 = %s", strSvrProPath));
-
- m_Procsee.SC_StartSvr(strSvrProName);
- }
- }
- CString CMainWork::GetIniSvrProName(CString strKeyValue)
- {
- CString strSvrProName;
- int nIndex = strKeyValue.Find('|');
- strSvrProName = strKeyValue.Left(nIndex);
- return strSvrProName;
- }
- CString CMainWork::GetIniSvrProPath(CString strKeyValue)
- {
- CString strSvrProPath;
- int nIndex = strKeyValue.Find('|');
- strSvrProPath = strKeyValue.Right(strKeyValue.GetLength() - nIndex -1);
- return strSvrProPath;
- }
|