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. QProcess close child processes issue
Forum Updated to NodeBB v4.3 + New Features

QProcess close child processes issue

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 1 Posters 785 Views
  • 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.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by Cobra91151
    #1

    Hi! I want to close/terminate the Inno Setup installation process using my application. I have tried these methods of QProcess:

    close(), terminate(), kill() but it only closes the 1 process and all child Inno Setup processes continue running. How to close all Inno Setup processes? Any ideas? Thanks.

    1 Reply Last reply
    0
    • Cobra91151C Offline
      Cobra91151C Offline
      Cobra91151
      wrote on last edited by Cobra91151
      #2

      I fixed it using the Win API.

      Code:

            DWORD pid = NULL;
            HWND hWnd = FindWindow(NULL,  QString("SetupTitle").toStdWString().c_str());
            GetWindowThreadProcessId(hWnd, &pid);
            HANDLE handle = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE, TRUE, pid);
            DWORD res = TerminateProcess(handle, NULL);
      
            if (res == NULL) {
                QMessageBox::critical(this, QObject::tr("Error"), QObject::tr("Process is not found."), QMessageBox::Ok);
            } 
      
            CloseHandle(handle);
      

      Info:

      1. It gets the pid from the window title
      2. Creates the handle with the process pid
      3. Terninates the process using TerminateProcess function
      4. Closes the handle

      Also I added the additional check if process is not detected. The issue is resolved.

      1 Reply Last reply
      0
      • Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by Cobra91151
        #3

        This solution works but it has some cons. For example, you need manually specify the window title in the FindWindow function, add some checking if it's localized in the different languages etc.

        So, I have checked the Microsoft Spy++ app, read some docs, then I have created another solution.

        Code:

                DWORD pid = NULL;
                HWND hWnd = FindWindow(QString("TWizardForm").toStdWString().c_str(), NULL);
                std::wstring title;
                title.reserve(GetWindowTextLength(hWnd) + 1);
                GetWindowText(hWnd, const_cast<WCHAR *>(title.c_str()), title.capacity());
                HWND wizardhWnd = FindWindowEx(NULL, NULL, QString("TWizardForm").toStdWString().c_str(), title.c_str());
                GetWindowThreadProcessId(wizardhWnd, &pid);
                
                HANDLE handle = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE, TRUE, pid);
                DWORD res = TerminateProcess(handle, NULL);
        
                if (res != NULL) {
                    QMessageBox::information(this, "Information", "Process has been closed!", QMessageBox::Ok);
                } else {
                    QMessageBox::critical(this, "Error", "Process is not found!", QMessageBox::Ok);
                }
        
                CloseHandle(handle);
        

        Info:

        1. I get TWizardForm class from Microsoft Spy++ app (TWizardForm class is used by Inno Setup)
        2. Get the window title using GetWindowText method
        3. FindWindowEx now will find the process for the class (TWizardForm) and appropriate window title
        4. Get the process pid using GetWindowThreadProcessId function
        5. Terminate the process
        6. Close the handle

        It will detect the appropriate process and terminate it. I think it's better solution than the previous one.

        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