Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill process by process name in Qt

I'm writing desktop application for windows in Qt.
I have name of 3 processes that if they are running I want to kill them in the begining of my application.
What the best way to do it?(get the status of process by using the process name, and kill it if it's open).

code example can help me a lot. Thanks!

like image 712
user758795 Avatar asked Jul 08 '12 08:07

user758795


People also ask

How do I kill a process in Windows Terminal?

a) Type the following command into the command prompt, to kill only the one Process, and press Enter Key. For Example - To kill Notepad, run the command as, taskkill /IM notepad.exe /F , where /F is used to kill the process forcefully.


2 Answers

You can use Qprocess for this purpose. At the start of your application,Do

Qprocess p;
p.start("pkill processname1");
p.waitForFinished();
p.start("pkill processname2");
p.waitForFinished();
p.start("pkill processname2");
p.waitForFinished();

Or you can use system call directly..

system("pkill processname1");
system("pkill processname2");
system("pkill processname3");

In Windows environment, you can use following commands to kill process

process -k “Process ID”
process -k “Process Name”

You can read more of these here.

like image 172
ScarCode Avatar answered Oct 17 '22 08:10

ScarCode


Under Windows use taskkill command You can call it using

QProcess::execute("taskkill /im <processname> /f");

Or

system("taskkill /im <processname> /f");
like image 20
creekorful Avatar answered Oct 17 '22 08:10

creekorful