window下使用进程io判断进程是否存在

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <sstream>


std::string get_cmd_result(const std::string& str_cmd)
{
    std::stringstream ss;
    FILE* pf = NULL;

    if ((pf = _popen(str_cmd.c_str(), "r")) == NULL){
        return "";
    }

    char buf[10] = { 0 };
    while (fgets(buf, sizeof(buf), pf)){
        ss << buf;
    }

    _pclose(pf);

    std::string str_result = ss.str();
    return str_result;
}

int main()
{

    std::string cmd = "wmic process get name,processid|findstr YoudaoNote.exe";

    std::string ret = get_cmd_result(cmd);
}

发布了63 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_33048069/article/details/103798961