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 kill yourself in qt program?
QtWS25 Last Chance

How to kill yourself in qt program?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 263 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.
  • M Offline
    M Offline
    mirro
    wrote 25 days ago last edited by
    #1

    Why does the following method not work?

    int res = QProcess::execute("kill", QStringList() << QString::number(QCoreApplication::applicationPid()));
    123456.png

    1 Reply Last reply
    0
    • M mirro
      24 days ago

      @JonB
      Why cannot I respond to the SERVICE_CONTROL_STOP message event when using the ControlService(scService, SERVICE_CONTROL_STOP, &status) method?

      SC_HANDLE scManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_CREATE_SERVICE);
      if (!scManager) {
          qDebug() << "Failed to open SCManager. Error:" << GetLastError();
          return false;
      }
      //
      std::wstring serviceNameW = serviceName.toStdWString();
      SC_HANDLE scService = OpenServiceW(scManager, serviceNameW.c_str(), DELETE);
      if (!scService) {
          qDebug() << "Failed to open service. Error:" << GetLastError();
          CloseServiceHandle(scManager);
          return false;
      }
      //
      SERVICE_STATUS status;
      ControlService(scService, SERVICE_CONTROL_STOP, &status);
      
      J Online
      J Online
      JonB
      wrote 24 days ago last edited by
      #10

      @mirro
      I already answered:

      @JonB said in How to kill yourself in qt program?:

      @mirro Why not tell us whether in that case your SERVICE_CONTROL_STOP code is actually hit, or would you like us to guess?

      Do you not think we need to know whether your function is being hit? Just pasting more code and not telling us what does/does not happen is not helpful when asking a question.

      Also this now a Windows programming question, nothing to do with Qt.

      1 Reply Last reply
      1
      • C Offline
        C Offline
        ChrisW67
        wrote 25 days ago last edited by
        #2

        @mirro said in How to kill yourself in qt program?:

        int res = QProcess::execute("kill", QStringList() << QString::number(QCoreApplication::applicationPid()));

        I assume that res is -1 or -2 but you never check.

        Your code is plainly written for Windows (OpenServiceW, DeleteService, CloseServiceHandle) and there is no such beast as kill in that environment by default.
        That call would only work on a Linux box if kill is in the executable's PATH at the time of this call and the SIGTERM signal is not being ignored or otherwise intercepted/blocked. If it worked then you would never expect to execute the return true line.

        Why do you not just use one of:

        • QCoreApplication::exit()
        • QCoreApplication::quit()
        M 1 Reply Last reply 24 days ago
        5
        • C ChrisW67
          25 days ago

          @mirro said in How to kill yourself in qt program?:

          int res = QProcess::execute("kill", QStringList() << QString::number(QCoreApplication::applicationPid()));

          I assume that res is -1 or -2 but you never check.

          Your code is plainly written for Windows (OpenServiceW, DeleteService, CloseServiceHandle) and there is no such beast as kill in that environment by default.
          That call would only work on a Linux box if kill is in the executable's PATH at the time of this call and the SIGTERM signal is not being ignored or otherwise intercepted/blocked. If it worked then you would never expect to execute the return true line.

          Why do you not just use one of:

          • QCoreApplication::exit()
          • QCoreApplication::quit()
          M Offline
          M Offline
          mirro
          wrote 24 days ago last edited by mirro
          #3

          @ChrisW67
          The QCoreApplication::quit() method is invalid, and this process program still exists.

          222.png
          4F418030-770A-4edb-A92E-5708920858F6.png

          A B J 3 Replies Last reply 24 days ago
          0
          • M mirro
            24 days ago

            @ChrisW67
            The QCoreApplication::quit() method is invalid, and this process program still exists.

            222.png
            4F418030-770A-4edb-A92E-5708920858F6.png

            A Offline
            A Offline
            Axel Spoerl
            Moderators
            wrote 24 days ago last edited by
            #4

            @mirro said in How to kill yourself in qt program?:

            @ChrisW67
            The QCoreApplication::quit() method is invalid

            Have you read the documentation of the method?

            and this process program still exists

            That happens, when something in your code prevents the application from shutting down gracefully. E.g. a window that can’t be closed, a thread that doesn’t return. Could also be a memory leak. Definitively something in your application, that needs to be fixed.

            You can directly call the exit() slot. That’s shoots the problem down in most cases - instead of solving it. You risk that your application crashes on shutdown, unless the problem is solved.

            Software Engineer
            The Qt Company, Oslo

            1 Reply Last reply
            1
            • M mirro
              24 days ago

              @ChrisW67
              The QCoreApplication::quit() method is invalid, and this process program still exists.

              222.png
              4F418030-770A-4edb-A92E-5708920858F6.png

              B Offline
              B Offline
              Bonnie
              wrote 24 days ago last edited by Bonnie
              #5

              @mirro From your code, uninstallService() is called without app.exec() being called so it is not suitable to use QCoreApplication::quit() or QCoreApplication::exit() since there's no eventloop here.
              By the way, is your program a console project? If yes it should print qDebug messages but I don't see any output (or do you redirect them in messageHandler? In that case you need also post them).
              This line may even never been reached. There's big chance that somewhere in your code before this line is hanging.
              I think you should use debugger to check where does it stuck.
              If it can reach the end of uninstallService(), there's no need to call any quit() or exit() because it returns in main().

              1 Reply Last reply
              2
              • M mirro
                24 days ago

                @ChrisW67
                The QCoreApplication::quit() method is invalid, and this process program still exists.

                222.png
                4F418030-770A-4edb-A92E-5708920858F6.png

                J Online
                J Online
                JonB
                wrote 24 days ago last edited by JonB
                #6

                @mirro
                Your code gets the PID of itself and tries to issue an OS command of kill <pid>. Don't know where you got the idea of this from, Windows does not come with a kill command, so unless you have added one it won't find the command to run.

                I don't know why you are trying to kill yourself, but if you really want to do this you might as well call exit() or _exit() directly. That is not advisable/tidy, but it's still neater than trying to issue an OS command to kill yourself.

                As @Bonnie has noted, if you simply allow your uninstallService() to run to its end execution will return into main() where it goes return 0 and so will exit anyway. We do not know why you do not allow it simply to do that.

                The exit, or your kill, will only happen if you invoke your program with a command line argument. The uninstallService() will only be hit if you invoke it as PixelStreamServerTalkTest --uninstall. Note that has two hyphens, --uninstall. The only way you should see your program continuing to run is if you run it with no command line arguments, i.e. plain PixelStreamServerTalkTest. This is common for a Windows service: with command line arguments, like --install/--uninstall it does a corresponding action and exits, but if no arguments are passed it runs all the time doing whatever it does as a Windows service. Show how you actually invoke your program to run.

                M 1 Reply Last reply 24 days ago
                0
                • J JonB
                  24 days ago

                  @mirro
                  Your code gets the PID of itself and tries to issue an OS command of kill <pid>. Don't know where you got the idea of this from, Windows does not come with a kill command, so unless you have added one it won't find the command to run.

                  I don't know why you are trying to kill yourself, but if you really want to do this you might as well call exit() or _exit() directly. That is not advisable/tidy, but it's still neater than trying to issue an OS command to kill yourself.

                  As @Bonnie has noted, if you simply allow your uninstallService() to run to its end execution will return into main() where it goes return 0 and so will exit anyway. We do not know why you do not allow it simply to do that.

                  The exit, or your kill, will only happen if you invoke your program with a command line argument. The uninstallService() will only be hit if you invoke it as PixelStreamServerTalkTest --uninstall. Note that has two hyphens, --uninstall. The only way you should see your program continuing to run is if you run it with no command line arguments, i.e. plain PixelStreamServerTalkTest. This is common for a Windows service: with command line arguments, like --install/--uninstall it does a corresponding action and exits, but if no arguments are passed it runs all the time doing whatever it does as a Windows service. Show how you actually invoke your program to run.

                  M Offline
                  M Offline
                  mirro
                  wrote 24 days ago last edited by
                  #7

                  @JonB
                  Why does the SERVICE_CONTROL_STOP message not respond when SetServiceStatus is set to the SERVICE_STOPPED state?

                      serviceStatus.dwCurrentState = SERVICE_STOPPED;
                      SetServiceStatus(serviceStatusHandle, &serviceStatus);
                  

                  xxxx.png

                  J 1 Reply Last reply 24 days ago
                  0
                  • M mirro
                    24 days ago

                    @JonB
                    Why does the SERVICE_CONTROL_STOP message not respond when SetServiceStatus is set to the SERVICE_STOPPED state?

                        serviceStatus.dwCurrentState = SERVICE_STOPPED;
                        SetServiceStatus(serviceStatusHandle, &serviceStatus);
                    

                    xxxx.png

                    J Online
                    J Online
                    JonB
                    wrote 24 days ago last edited by
                    #8

                    @mirro Why not tell us whether in that case your SERVICE_CONTROL_STOP code is actually hit, or would you like us to guess?

                    M 1 Reply Last reply 24 days ago
                    0
                    • J JonB
                      24 days ago

                      @mirro Why not tell us whether in that case your SERVICE_CONTROL_STOP code is actually hit, or would you like us to guess?

                      M Offline
                      M Offline
                      mirro
                      wrote 24 days ago last edited by mirro
                      #9

                      @JonB
                      Why cannot I respond to the SERVICE_CONTROL_STOP message event when using the ControlService(scService, SERVICE_CONTROL_STOP, &status) method?

                      SC_HANDLE scManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_CREATE_SERVICE);
                      if (!scManager) {
                          qDebug() << "Failed to open SCManager. Error:" << GetLastError();
                          return false;
                      }
                      //
                      std::wstring serviceNameW = serviceName.toStdWString();
                      SC_HANDLE scService = OpenServiceW(scManager, serviceNameW.c_str(), DELETE);
                      if (!scService) {
                          qDebug() << "Failed to open service. Error:" << GetLastError();
                          CloseServiceHandle(scManager);
                          return false;
                      }
                      //
                      SERVICE_STATUS status;
                      ControlService(scService, SERVICE_CONTROL_STOP, &status);
                      
                      J 1 Reply Last reply 24 days ago
                      0
                      • M mirro
                        24 days ago

                        @JonB
                        Why cannot I respond to the SERVICE_CONTROL_STOP message event when using the ControlService(scService, SERVICE_CONTROL_STOP, &status) method?

                        SC_HANDLE scManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_CREATE_SERVICE);
                        if (!scManager) {
                            qDebug() << "Failed to open SCManager. Error:" << GetLastError();
                            return false;
                        }
                        //
                        std::wstring serviceNameW = serviceName.toStdWString();
                        SC_HANDLE scService = OpenServiceW(scManager, serviceNameW.c_str(), DELETE);
                        if (!scService) {
                            qDebug() << "Failed to open service. Error:" << GetLastError();
                            CloseServiceHandle(scManager);
                            return false;
                        }
                        //
                        SERVICE_STATUS status;
                        ControlService(scService, SERVICE_CONTROL_STOP, &status);
                        
                        J Online
                        J Online
                        JonB
                        wrote 24 days ago last edited by
                        #10

                        @mirro
                        I already answered:

                        @JonB said in How to kill yourself in qt program?:

                        @mirro Why not tell us whether in that case your SERVICE_CONTROL_STOP code is actually hit, or would you like us to guess?

                        Do you not think we need to know whether your function is being hit? Just pasting more code and not telling us what does/does not happen is not helpful when asking a question.

                        Also this now a Windows programming question, nothing to do with Qt.

                        1 Reply Last reply
                        1
                        • M mirro has marked this topic as solved 22 days ago

                        1/10

                        14 Apr 2025, 05:33

                        • Login

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