#pragma once#include <windows.h>#include "psapi.h"#include"stdio.h"#include <tlhelp32.h>#include <string>#include "TLog.h"#include <iostream>#include <atlconv.h>using namespace std;static wchar_t* CharToWchar(const char* ch){size_t len = strlen(ch) + 1;size_t convertedChars = 0;wchar_t* wch = new wchar_t[len];mbstowcs_s(&convertedChars, wch, len, ch, _TRUNCATE);return wch;}static bool FindProcess(string exeName){int i = 0;PROCESSENTRY32 pe32;pe32.dwSize = sizeof(pe32);HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);if (hProcessSnap == INVALID_HANDLE_VALUE){i += 0;}bool bMore = ::Process32First(hProcessSnap, &pe32);wchar_t* wExeName = CharToWchar(exeName.c_str());while (bMore){//printf(" 进程名称:%s \n", pe32.szExeFile);if (wcscmp(wExeName, pe32.szExeFile) == 0){//发现程序运行delete wExeName;return true;}bMore = ::Process32Next(hProcessSnap, &pe32);}delete wExeName;return false;}static void CloseProcess(string exeName){std::string temp = std::string("TASKKILL /F /IM ") + exeName;system(temp.c_str());}static void OpenProcess(string exePath, string param = ""){STARTUPINFO startupInfo = { 0 };PROCESS_INFORMATION processInformation = { 0 };wchar_t* path = CharToWchar(exePath.c_str());wchar_t* cmd = CharToWchar(param.c_str());bool bSuccess = CreateProcess(path, cmd, NULL, NULL, FALSE, NULL, NULL, NULL, &startupInfo, &processInformation);delete path;delete cmd;if (bSuccess){TLOG(INFO, "%s open success", exePath.c_str());}else{TLOG(Error, "%s open Failed", exePath.c_str());}}
这里要注意 openprocess 如果要通过param传入命令行参数 那么需要在命令前面加上空格 例如” ?test=aa”
