6

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!

ymoreau
  • 3,402
  • 1
  • 22
  • 60
user758795
  • 429
  • 1
  • 9
  • 19
  • http://stackoverflow.com/questions/4538967/how-to-find-the-running-process-and-kill-the-process-in-qt?rq=1 –  Jul 08 '12 at 08:33

5 Answers5

12

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.

ScarCode
  • 3,074
  • 3
  • 19
  • 32
5

Under Windows use taskkill command You can call it using

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

Or

system("taskkill /im <processname> /f");
creekorful
  • 351
  • 4
  • 14
1

QT does not provide any API for killing processes that is not created by your QT project. If you are on Windows you can try following code as explained here

#include <windows.h>
#include <process.h>
#include <Tlhelp32.h>
#include <winbase.h>
#include <string.h>
void killProcessByName(const char *filename)
{
    HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
    PROCESSENTRY32 pEntry;
    pEntry.dwSize = sizeof (pEntry);
    BOOL hRes = Process32First(hSnapShot, &pEntry);
    while (hRes)
    {
        if (strcmp(pEntry.szExeFile, filename) == 0)
        {
            HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,
                                          (DWORD) pEntry.th32ProcessID);
            if (hProcess != NULL)
            {
                TerminateProcess(hProcess, 9);
                CloseHandle(hProcess);
            }
        }
        hRes = Process32Next(hSnapShot, &pEntry);
    }
    CloseHandle(hSnapShot);
}
int main()
{
    killProcessByName("notepad++.exe");
    return 0;
}
prashanthns
  • 339
  • 2
  • 12
1

How to Run App

bool ok = QProcess::startDetached("C:\\TTEC\\CozxyLogger\\CozxyLogger.exe");
qDebug() <<  "Run = " << ok;

How to Kill App

 system("taskkill /im CozxyLogger.exe /f");
 qDebug() << "Close";[enter image description here][1]

More at link ::

http://sittikron-big-rmutt.blogspot.com/2018/01/qt-start-and-kill.html

Sunil
  • 3,404
  • 10
  • 23
  • 31
-4

In Windows right click the taskbar, select Task Manager, on the Process tab, find the process by Name..End task.