1. #pragma once
    2. #include <windows.h>
    3. #include "psapi.h"
    4. #include"stdio.h"
    5. #include <tlhelp32.h>
    6. #include <string>
    7. #include "TLog.h"
    8. #include <iostream>
    9. #include <atlconv.h>
    10. using namespace std;
    11. static wchar_t* CharToWchar(const char* ch)
    12. {
    13. size_t len = strlen(ch) + 1;
    14. size_t convertedChars = 0;
    15. wchar_t* wch = new wchar_t[len];
    16. mbstowcs_s(&convertedChars, wch, len, ch, _TRUNCATE);
    17. return wch;
    18. }
    19. static bool FindProcess(string exeName)
    20. {
    21. int i = 0;
    22. PROCESSENTRY32 pe32;
    23. pe32.dwSize = sizeof(pe32);
    24. HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    25. if (hProcessSnap == INVALID_HANDLE_VALUE)
    26. {
    27. i += 0;
    28. }
    29. bool bMore = ::Process32First(hProcessSnap, &pe32);
    30. wchar_t* wExeName = CharToWchar(exeName.c_str());
    31. while (bMore)
    32. {
    33. //printf(" 进程名称:%s \n", pe32.szExeFile);
    34. if (wcscmp(wExeName, pe32.szExeFile) == 0)
    35. {
    36. //发现程序运行
    37. delete wExeName;
    38. return true;
    39. }
    40. bMore = ::Process32Next(hProcessSnap, &pe32);
    41. }
    42. delete wExeName;
    43. return false;
    44. }
    45. static void CloseProcess(string exeName)
    46. {
    47. std::string temp = std::string("TASKKILL /F /IM ") + exeName;
    48. system(temp.c_str());
    49. }
    50. static void OpenProcess(string exePath, string param = "")
    51. {
    52. STARTUPINFO startupInfo = { 0 };
    53. PROCESS_INFORMATION processInformation = { 0 };
    54. wchar_t* path = CharToWchar(exePath.c_str());
    55. wchar_t* cmd = CharToWchar(param.c_str());
    56. bool bSuccess = CreateProcess(path, cmd, NULL, NULL, FALSE, NULL, NULL, NULL, &startupInfo, &processInformation);
    57. delete path;
    58. delete cmd;
    59. if (bSuccess)
    60. {
    61. TLOG(INFO, "%s open success", exePath.c_str());
    62. }
    63. else
    64. {
    65. TLOG(Error, "%s open Failed", exePath.c_str());
    66. }
    67. }

    这里要注意 openprocess 如果要通过param传入命令行参数 那么需要在命令前面加上空格 例如” ?test=aa”