Qt achieve judgment on whether external (third party) program has been launched, the start-up and shut down

I. Overview
          This article describes the implementation of the determination whether the external application has started, the startup and shutdown by QProcess class.

Second, determine whether the external program has been launched
        Qt actually read all the information about the program is running on the system (including the program name, etc.) to start the program through the windows built-in tasklist QProcess objects in the windows, and then by comparing read all the information out there is whether to include, as the code name of the program that we want to evaluate the programs implemented in the first parameter start function is the application name, the second parameter is a startup parameter start input parameters, / FI represents the subsequent use criteria to filter all the matching process information, imagename eq appName indicates a match to all strings appName of information. Complete parameters / FI imagename eq appName, express screening process includes all appName string. (For tasklist not much to do introduction, there are many detailed introduction on Baidu)

bool CheckAppStatus(const QString &appName) 
{
#ifdef Q_OS_WIN      //表示如果在windows下
QProcess process;
process.start("tasklist" ,QStringList()<<"/FI"<<"imagename eq " +appName);   //执行tasklist程序
process.waitForFinished(5000);    //阻塞5秒等待tasklist程序执行完成,超过五秒则直接返回
QString outputStr = QString::fromLocal8Bit(process.readAllStandardOutput()); //把tasklist程序读取到的进程信息输出到字符串中
if(outputStr.contains(appName))
{
process.close() //用完记得把process关闭了,否则如果重新调用这个函数可以会失败
return true;
}
else
{
  process.close();
  return false;
}
#endif
}

Third, the launcher
        QProcess class provides three functions to launch external programs, namely start, startDetached and execute. These three functions can launch external programs, but there are corresponding differences:

start: This function starts the external program will follow our program exits and exit this function in the previous example to detect whether the program has been started already used before.

startDetached: After this function Start external program the program just like our program does not matter, its parent process is a system process, so we quit the program, will continue to run an external program.

execute: this function until the function returns, this needs to be suitable for further processing to return the results of the use of clogged scene after the implementation, this will close the external program to introduce in the back

Code implementation examples:

startApp(const QString&name)       //name可以是程序名也可以程序所在的完整路径(如C:\myapp.exe)
{
#ifdef Q_OS_WIN
QProcess process;
process.startDetached(QString("\"%1\"").arg(name));
process.close();
#endif
}

Fourth, turn off the external program
        to close the program also calls the windows taskkill program comes close, where / f represents a forced termination, / im represents the specified process name (more parameters to Baidu friends)

Directly on the code:

void killApp(const QString& appName)
{
#ifdef Q_OS_WIN
QProcess process;
QString command="taskkill /im" + appName + "/f";
process.execute(command);                 //execute执行后会一直阻塞到返回外部程序退出的代码,比如无法关闭会返回-2
process.close();
#endif
}

 

Guess you like

Origin blog.csdn.net/u014746838/article/details/92964599