在Qt中获取Windows中进程的PID

主要是用到了系统自带的工具【tasklist.exe】

利用 QProcess调用这个tasklist有一点坑,已经在代码中指出了。

指定为csv格式输出的话,在后处理时比较方便。

cpp 复制代码
QList<quint64> listProcessIdentifier(QString processName)
{
    QProcess process;

    QStringList mList;
    // 这里的 "IMAGENAME eq %1" 不知道为啥不能写成 "\"IMAGENAME eq %1\"",明明在cmd窗口输入时需要加引号
    mList << "/FI" << QString("IMAGENAME eq %1").arg(processName) << "/FO" << "CSV";
    process.start("tasklist.exe", mList);

    process.waitForStarted();
    process.waitForFinished();

    QString ret = process.readAll();
    ret = ret.trimmed();

    QList<quint64> pidList;
    QStringList resultList = ret.split("\r\n");
    resultList.removeFirst(); // 去掉表头
    if(resultList.length() > 0)
    {
        foreach (const QString &str, resultList)
        {
            QString pidStr = str.split(",").at(1);
            pidStr.replace("\"", ""); // 清除双引号
            pidList << pidStr.toUInt();

            // qDebug() << str << pidStr;
        }
    }

    return pidList;
}

可以看到,得到的结果与在任务管理器中看到的是一致的。

相关推荐
临床数据科学和人工智能兴趣组2 小时前
在R语言中, 使用 as.factor() 函数转换数值型变量
开发语言·r语言
andyweike5 小时前
php笔记
开发语言·笔记·php
段一凡-华北理工大学6 小时前
大模型应用开发 100 天:Python + LLM 从入门到精通 day26~Prompt 调试与优化——A/B 测试与效果评估
windows·python·大模型·prompt·智能体·提示词工程·高炉智能化
Circ.6 小时前
Python 调试 Litellm 本地大模型接口:模型列表与对话接口实操踩坑记录
开发语言·python
fox_charon7 小时前
Windows 下 CLI 参数的引号陷阱:为什么 --resume 'uuid' 会失败
windows·shell·cmd·cli·引号
现任明教教主~8 小时前
Thinkphp站群蜘蛛池SaaS系统YanyvSEO含多用户/积分/六大引擎计费/易支付对接
java·开发语言·spring
Zhou1411368 小时前
SpringSecurity_02_授权与高级功能
开发语言·python
染指11109 小时前
128.Agent-LangChain核心组件-中间件-调用模型的时候
人工智能·windows·中间件·langchain·agent
码匠许师傅9 小时前
【C++三方组件】cpp-httplib:一个头文件起 HTTP 服务
开发语言·c++·http