How to detect a specific running application/service from within my QtGui-based app?



  • From my GUI-based application (on Windows yet) I need to look for a specific running application/service.

    For this I found the wmic.exe on Windows.

    In the code below, QProcess doesn't terminate with the 'wmic' call, however the 'cmd.exe ...' call does!?

    Does anybody know how to fix this, or maybe a more generic getAllRunningProcesses() methods which also

    works on Linux/OSX.

    @void MainWindow::getListOfPids()
    {
    QProcess process;
    process.setReadChannel(QProcess::StandardOutput);
    process.setReadChannelMode(QProcess::MergedChannels);
    // process.start("cmd.exe /C echo test");
    process.start("wmic.exe /OUTPUT:STDOUT PROCESS get Caption");

    process.waitForStarted(1000);
    //process.waitForFinished(1000);
    process.waitForFinished(-1);
    
    QByteArray list = process.readAll();
    qDebug() << "Read" << list.length() << "bytes";
    qDebug() << list;
    

    }
    @



  • Well, I don't know why with wmic doesn't work well.

    AFAIK you don't have, in Qt, a getAllRunningProcesses(), but I don't think it's so hard to write one by yourself.

    Win32: you can use "this":http://msdn.microsoft.com/en-us/library/ms682623(VS.85).aspx

    Linux: just use QDirIterator in "/proc", filter out your results with directories that have only a number as name (that's the PID of the process), and in the "exe" symlink that you find inside each directory you'll have the name of the executable.

    OS X: .... well, I found some time ago this code on the web:

    @
    struct kinfo_proc *result, *ptr;
    int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
    size_t length, i;

    result = NULL;

    length = 0;
    if (sysctl((int *) name,(sizeof(name) / sizeof(name)) - 1,NULL,&length,NULL,0) == -1)
    { /
    error */ };

    if ((result = (kinfo_proc ) malloc(length)) == NULL)
    { /
    error */ };

    if (sysctl((int *) name,(sizeof(name) / sizeof(name)) - 1,result,&length,NULL,0) == -1)
    { /
    error */ };

    for (i = 0, ptr = result; (i < (length / sizeof(kinfo_proc)); ++i, ++ptr)
    {
    // ptr->kp_proc.p_pid contains the pid
    // ptr->kp_proc.p_comm contains the name
    }

    free(result);
    @

    I use it in my projects and it works.

    Tony.



  • Dear Antonio, thanks for the Windows link.

    This seems interesting, however I don't get it running yet.

    I entered in mygui.pro file the following lines for linking the required psapi library, i.e.:

    @
    QT += core gui
    TARGET = MyProductController
    TEMPLATE = app
    #win32:LIBS += C:/Windows/system32/psapi.dll
    LIBS += -LC:/Windows/system32 -lpsapi
    @

    I still get the following link-errors (using the 2010.04 Qt SDK with QtCreator 2.0.0):

    @
    undefined reference to EnumProcesses@12' undefined reference toOpenProcess@12'
    undefined reference to EnumProcessModules@16' undefined reference toGetModuleBaseNameW@16'
    undefined reference to `CloseHandle@4'
    @

    Am I missing a specific Windows configuration setting in the .pro file?

    Thanks in advance for any help/suggestions!



  • Well, I think you should just add

    LIBS += -lpsapi

    If you look inside this dir: <Your Qt SDK dir>\mingw\lib, you can find a libpsapi.a.

    If you don't succeed, later I'll do some test by myself :)



  • Great: the @LIBS += -lpsapi@ was the problem, it works now!!

    Thanks again, you make my day!

    I thought the @LIBS += -LC:/Windows/system32 -lpsapi@ worked, since no missing libraries error was found?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.