11

How to check if a program is running, by its name, with Qt (C++).

Will QProcess::pid do the job? I don't know how to use it. Please suggest.

stema
  • 90,351
  • 20
  • 107
  • 135
Random78952
  • 1,520
  • 4
  • 26
  • 36
  • 2
    [This question](http://stackoverflow.com/questions/10422145/how-to-check-if-a-process-is-running-or-not) seems to fit your needs as long as you have the object. – chris Nov 29 '12 at 20:08
  • Yes, but i don't know how to use QProcess::state(), like that ? QProcess::state("chrome.exe"); ?? – Random78952 Nov 29 '12 at 20:10
  • 1
    Presumably you can get a `QProcess` object by the process name using some function and then call it on that. – chris Nov 29 '12 at 20:12
  • Can you give an exemple please ? and thanks you very much for yours answers :) – Random78952 Nov 29 '12 at 20:15
  • 1
    [This](http://stackoverflow.com/questions/2632594/get-all-running-processes-info-using-qprocess) could help, I guess. I haven't seen anything to get a process by name or to get a list of all running processes through use of a Qt function. – chris Nov 29 '12 at 20:18
  • If you are trying to see if a particular program, *not started from a QProcess*, is running, I don't think you can do this in Qt. But if you let us know on what platform(s) this needs to work, there is certainly a non-portable way of doing it. – Dave Mateer Nov 29 '12 at 20:50

3 Answers3

20

As far as I know QProcess won't allow you to do that (unless you've spawned the process yourself) and in fact nothing in Qt will. However Win32 API provides a way to achieve what you want through EnumProcesses function and a complete example of how to use it is provided at Microsoft website:

http://msdn.microsoft.com/en-us/library/ms682623.aspx

To get you need replace PrintProcessNameAndID with the following function:

bool matchProcessName( DWORD processID, std::string processName)
{
    TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");

    // Get a handle to the process.

    HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                   PROCESS_VM_READ,
                                   FALSE, processID );

    // Get the process name.

    if (NULL != hProcess )
    {
        HMODULE hMod;
        DWORD cbNeeded;

        if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
             &cbNeeded) )
        {
            GetModuleBaseName( hProcess, hMod, szProcessName, 
                               sizeof(szProcessName)/sizeof(TCHAR) );
        }
    }

    // Compare process name with your string        
    bool matchFound = !_tcscmp(szProcessName, processName.c_str() );

    // Release the handle to the process.    
    CloseHandle( hProcess );

    return matchFound;
}
jaho
  • 4,852
  • 6
  • 40
  • 66
  • 1
    +1 to compensate for the downvote; not sure why this was downvoted. I was going to include a link to the same page in my response. This is certainly the official way to do it. – Dave Mateer Nov 29 '12 at 21:51
11

A quick and dirty way to do it would be to just check the output of tasklist, something like:

bool isRunning(const QString &process) {
  QProcess tasklist;
  tasklist.start(
        "tasklist",
        QStringList() << "/NH" 
                      << "/FO" << "CSV" 
                      << "/FI" << QString("IMAGENAME eq %1").arg(process));
  tasklist.waitForFinished();
  QString output = tasklist.readAllStandardOutput();
  return output.startsWith(QString("\"%1").arg(process));
}

Using EnumProcesses is probably a better way (i.e. more "pure"; certainly more performant), but this may be "good enough" as long as this isn't being called in a big loop or something. The same idea could also be ported to other platforms as well, although obviously the command tool and parsing logic would be different.

Dave Mateer
  • 17,608
  • 15
  • 96
  • 149
-3
 //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

  • This is not the answer to the question "How to check **if a program is running...**. But thanks for your input anyhow. – Willy K. Mar 01 '19 at 09:23