1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- #include "precompile.h"
- #include "sp_runTask.h"
- #include <string>
- #include <vector>
- #include "sp_dir.h"
- #include "sp_dbg_export.h"
- #include <Windows.h>
- #include <TlHelp32.h>
- extern std::vector<std::string> getKillArr();
- extern std::vector<std::string> getStartArr();
- HANDLE FindProcessByName(const char* pProcessName)
- {
- PROCESSENTRY32 pe32;
- pe32.dwSize = sizeof(pe32);
- HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- BOOL bMore = ::Process32First(hProcessSnap, &pe32);
- while (bMore)
- {
- if (stricmp(pProcessName, pe32.szExeFile) == 0)
- {
- return OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID);
- }
- bMore = ::Process32Next(hProcessSnap, &pe32);
- }
- return NULL;
- }
- int sp_runtask_killprocess()
- {
- auto killArr = getKillArr();
- for each (auto it in killArr)
- {
- char cmdStr[MAX_PATH] = "";
- sprintf(cmdStr, "taskkill /f /im %s", it.c_str());
- sp_dbg_debug("run %s", cmdStr);
- WinExec(cmdStr, SW_HIDE);
- int i = 0;
- for (i = 0; i < 20; i++)
- {
- if (NULL == FindProcessByName(it.c_str()))
- break;
- Sleep(100);
- }
- if(i == 20)
- sp_dbg_warn("kill %s failed", it.c_str());
-
- }
- Sleep(1000);
- return 0;
- }
- bool checkFileExist(std::string fileName)
- {
- WIN32_FIND_DATA FindFileData;
- HANDLE hFind;
- hFind = FindFirstFile(fileName.c_str(), &FindFileData);
- if (hFind == INVALID_HANDLE_VALUE)
- return false;
- FindClose(hFind);
- return true;
- }
- int sp_runtask_startprocess()
- {
- auto startArr = getStartArr();
- for each (auto it in startArr)
- {
- if (!checkFileExist(it))
- {
- sp_dbg_debug("%s not exist", it.c_str());
- return 0;
- }
- sp_dbg_debug("run %s", it.c_str());
- WinExec(it.c_str(), SW_HIDE);
- }
- return 0;
- }
|