C语言获取当前系统运行中进程,打开或杀死外部进程

#include 
#include 
#include 
#include 
using namespace std;

vector splitN(string str, char sep, int n = -1) {
    vectorres;
    int pre = 0, i = 0;
    for (; i < str.size(); i++) {
        if (str[i] == sep) {
            res.push_back(str.substr(pre, i - pre));
            pre = i + 1;
            if (res.size() == n) {
                return res;
            }
        }
    }
    res.push_back(str.substr(pre, i - pre));
    return res;
}

string trimColon(string str) {
    return str.substr(1, str.size() - 2);
}

map getCurProcess() {
    mapm;
    char buffer[256];
    FILE* fp = popen("tasklist /FI \"STATUS eq running\" /FO csv /NH", "r");
    if (fp == NULL) {
        cout << "Failed to run tasklist\n";
        return m;
    }
    while (fgets(buffer, sizeof(buffer), fp)) {
        vectors = splitN(buffer, ',', 3);
        string name = trimColon(s[0]), pid = trimColon(s[1]);
        m[name] = pid;
    }
    pclose(fp);
    return m;
}

void startProcess(string file) {
    ShellExecute(NULL, LPCTSTR("open"), LPCTSTR(file.c_str()), LPCTSTR(""), NULL, SW_HIDE);
}

void killProcess(string pid) {
    system(("taskkill /F /PID " + pid).c_str());
}

int main() {
    while (1) {
        mapm = getCurProcess();
        string yuanShen = m["YuanShen.exe"], op = m["opHelper.exe"];
        string starRail = m["StarRail.exe"], xt = m["xtHelper.exe"];
        if (yuanShen != "" && op == "") { // 启动op
            startProcess("./YuanShen/opHelper.exe");
        } else if (op != "" && yuanShen == "") { // 关op
            killProcess(op);
        } else if (starRail != "" && xt == "") { // 开xt
            startProcess("./XingTie/xtHelper.exe");
        } else if (xt != "" && starRail == "") { // 关xt
            killProcess(xt);
        }
        Sleep(15000);
    }
    return 0;
}

你可能感兴趣的:(c语言,开发语言)