系统相关
首页 > 系统相关> > Qt启动/杀死外部进程

Qt启动/杀死外部进程

作者:互联网

参考1

启动外部线程

主要有:

(1) QProcess ::execute() 方法

	QProcess::execute("C:\\Environment\\influxDB_1_5_2\\influxd.exe")

(2) QProcess ::start() 方法

	QProcess  proc;
	proc.start("C:\\Environment\\influxDB_1_5_2\\influxd.exe");

(3) QProcess ::startDetached() 方法 (推荐!)

    if (QProcess::startDetached("C:\\Environment\\influxDB_1_5_2\\influxd.exe"))
        qDebug()  <<"Running...";
    else
        qDebug()  <<"Failed";

判断进程是否在运行

bool IsProcessExist(const QString &processName)// 返回 true/false
{
    QProcess proc;
    proc.start("tasklist");
    proc.waitForFinished();// 等待 tasklist 启动

    QByteArray result = proc.readAllStandardOutput();
    QString str = result;
    if(str.contains(processName))
    {
        qDebug() << processName <<"is Running";
        return true;
    }
    else
    {
        qDebug() << "Can't find " << processName;
        return false;
    }
}
bool IsProcessExist(const QString &processName)// 返回 true/false
{
    QString strInfo = QString::number(QProcess::execute("tasklist", QStringList()<<"-fi"<<"imagename eq influxd.exe"));
    if(strInfo .contains(processName))
    {
        qDebug() << processName <<"is Running";
        return true;
    }
    else
    {
        qDebug() << "Can't find " << processName;
        return false;
    }
}
void getProcessInfo() // 返回信息(需要对返回信息进行判断)
{
	QProcess::execute("tasklist", QStringList()<<"-fi"<<"imagename eq prog.exe");
}

终止外部进程

	// 通过进程ID结束进程(该进程由proc对象打开前提是通过 proc)
    QProcess::startDetached("taskkill -t  -f /pid " + QString::number(proc.processId()));
	// 通过进程名字结束进程
    QProcess::startDetached("taskkill -t  -f /IM " + QString("influxd.exe"));

标签:execute,Qt,外部,QProcess,startDetached,QString,进程,杀死,proc
来源: https://blog.csdn.net/CXYYL/article/details/121540577