sp_runTask.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "precompile.h"
  2. #include "sp_runTask.h"
  3. #include <string>
  4. #include <vector>
  5. #include "sp_dir.h"
  6. #include "sp_dbg_export.h"
  7. #include <Windows.h>
  8. #include <TlHelp32.h>
  9. extern std::vector<std::string> getKillArr();
  10. extern std::vector<std::string> getStartArr();
  11. HANDLE FindProcessByName(const char* pProcessName)
  12. {
  13. PROCESSENTRY32 pe32;
  14. pe32.dwSize = sizeof(pe32);
  15. HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  16. BOOL bMore = ::Process32First(hProcessSnap, &pe32);
  17. while (bMore)
  18. {
  19. if (stricmp(pProcessName, pe32.szExeFile) == 0)
  20. {
  21. return OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID);
  22. }
  23. bMore = ::Process32Next(hProcessSnap, &pe32);
  24. }
  25. return NULL;
  26. }
  27. int sp_runtask_killprocess()
  28. {
  29. auto killArr = getKillArr();
  30. for each (auto it in killArr)
  31. {
  32. char cmdStr[MAX_PATH] = "";
  33. sprintf(cmdStr, "taskkill /f /im %s", it.c_str());
  34. sp_dbg_debug("run %s", cmdStr);
  35. WinExec(cmdStr, SW_HIDE);
  36. int i = 0;
  37. for (i = 0; i < 20; i++)
  38. {
  39. if (NULL == FindProcessByName(it.c_str()))
  40. break;
  41. Sleep(100);
  42. }
  43. if(i == 20)
  44. sp_dbg_warn("kill %s failed", it.c_str());
  45. }
  46. Sleep(1000);
  47. return 0;
  48. }
  49. bool checkFileExist(std::string fileName)
  50. {
  51. WIN32_FIND_DATA FindFileData;
  52. HANDLE hFind;
  53. hFind = FindFirstFile(fileName.c_str(), &FindFileData);
  54. if (hFind == INVALID_HANDLE_VALUE)
  55. return false;
  56. FindClose(hFind);
  57. return true;
  58. }
  59. int sp_runtask_startprocess()
  60. {
  61. auto startArr = getStartArr();
  62. for each (auto it in startArr)
  63. {
  64. if (!checkFileExist(it))
  65. {
  66. sp_dbg_debug("%s not exist", it.c_str());
  67. return 0;
  68. }
  69. sp_dbg_debug("run %s", it.c_str());
  70. WinExec(it.c_str(), SW_HIDE);
  71. }
  72. return 0;
  73. }