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::terminate can not stop Windows console application with SetConsoleCtrlHandler
Forum Updated to NodeBB v4.3 + New Features

QProcess::terminate can not stop Windows console application with SetConsoleCtrlHandler

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 8.3k 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.
  • S Offline
    S Offline
    skorodum
    wrote on 22 May 2013, 08:03 last edited by
    #1

    I try to control Windows non-Qt console application from Qt desktop application.
    Qt documentation says, that on Windows QProcess::terminate posts WM_CLOSE message. MSDN says that WM_CLOSE message is translated to CTRL_CLOSE_EVENT for console application.
    So, in console application I call WinAPI function SetConsoleCtrlHandler, after that the application successfully intercept events like CTRL-C press (CTRL_C_EVENT) and Alt-F4 press (CTRL_CLOSE_EVENT).
    From Qt application I successfully start console application using QProcess. Then I wait for application started using waitForStarted and try to gentle finish him using QProcess::terminate method, but nothing happens. In the same time QProcess kill works as expected.

    What should I change in the console application or Qt application to receive event from QProcess::terminate?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      skorodum
      wrote on 22 May 2013, 10:15 last edited by
      #2

      More info: on WinXP I can intercept CTRL_CLOSE_EVENT in console app, on Win7 can not. Microsoft changed behavior for CTRL_CLOSE_EVENT somewhere between WinXP and Vista.
      Nevertheless even on WinXP console application did not receive CTRL_CLOSE_EVENT when Qt application call QProcess::terminate. Any idea?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MuldeR
        wrote on 22 May 2013, 10:58 last edited by
        #3

        If terminate() doesn't work, try kill() ;-)

        It probably is the same as kill(), on Win32, but you can also try:
        @TerminateProcess(myProcess->pid()->hProcess, 42);@

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply
        0
        • S Offline
          S Offline
          skorodum
          wrote on 22 May 2013, 11:06 last edited by
          #4

          Of course I can use QThread::kill, but I need a chance to do some sort of a clean up in the console application.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MuldeR
            wrote on 22 May 2013, 11:09 last edited by
            #5

            Then terminating or killing the process from outside probably is not the way to go. Instead, you should make your Console application read messages from its STDIN, so you can send commands to it. QProcess can write to the child process' STDIN, simply use the write() method. This way, you could send some string like, for example, "CMD_EXIT", which tells the application that it should shutdown "properly" by itself as soon as possible.

            Console applications do NOT normally run an event loop to process Window messages. That's why sending a WM_CLOSE usually doesn't do anything. Therefore the Qt documentation explicitly states:
            [quote]Console applications on Windows that do not run an event loop, or whose event loop does not handle the WM_CLOSE message, can only be terminated by calling kill().[/quote]

            --

            BTW: Killing an application via terminate() is never "safe", as the app may very well ignore the WM_CLOSE message - intentionally or by malfunction. You should send a kill(), if it doesn't terminate within a few seconds...

            My OpenSource software at: http://muldersoft.com/

            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

            Go visit the coop: http://youtu.be/Jay...

            1 Reply Last reply
            0

            1/5

            22 May 2013, 08:03

            • Login

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