Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to detect a specific running application/service from within my QtGui-based app?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 9.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QtOtto
    wrote on last edited by
    #1

    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;
    

    }
    @

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tony
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      1
      • Q Offline
        Q Offline
        QtOtto
        wrote on last edited by
        #3

        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 to OpenProcess@12'
        undefined reference to EnumProcessModules@16' undefined reference to GetModuleBaseNameW@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!

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tony
          wrote on last edited by
          #4

          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 :)

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            QtOtto
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved