Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Terminate an exe from another exe ?

Terminate an exe from another exe ?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
10 Posts 4 Posters 0 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.
  • A Offline
    A Offline
    Amarelia
    wrote on last edited by
    #1

    Hello Friends I have an application in which I need to start one exe from another one and at the end when the first one ends, it should automatically end the second one. To start I use ShellExecute() or WinExec() function. And to terminate the another exe I put code into the destructor of the first one from where I have started it. But the problem is I don't know the Handle of the new exe that I have started using ShellExecute() or WinEec()..... Is there any functions which can give me the Handle of a perticular window or is there any other way to stop the application without knowing the handle or process id ? Thankx in advance My Code Looks Like : ------------------- My first.exe file ================ MyClass() { // Do some work here........ WinExec("C:\\Temp\\second.exe",SW_SHOW); // Do some work here... }; ~MyClass() { Here I want to kill(Terminate) the second.exe } Any help plz ? Thankx } } Amarelia Maehsh Gujarat India

    M D 2 Replies Last reply
    0
    • A Amarelia

      Hello Friends I have an application in which I need to start one exe from another one and at the end when the first one ends, it should automatically end the second one. To start I use ShellExecute() or WinExec() function. And to terminate the another exe I put code into the destructor of the first one from where I have started it. But the problem is I don't know the Handle of the new exe that I have started using ShellExecute() or WinEec()..... Is there any functions which can give me the Handle of a perticular window or is there any other way to stop the application without knowing the handle or process id ? Thankx in advance My Code Looks Like : ------------------- My first.exe file ================ MyClass() { // Do some work here........ WinExec("C:\\Temp\\second.exe",SW_SHOW); // Do some work here... }; ~MyClass() { Here I want to kill(Terminate) the second.exe } Any help plz ? Thankx } } Amarelia Maehsh Gujarat India

      M Offline
      M Offline
      mark novak
      wrote on last edited by
      #2

      You could use CreateProcess() and TerminateProcess(), however TerminateProcess() ends a program so abruptly that some applications don't handle it well, it doesn't give them time to save their settings or other exit code. I'd recommend using FindWindowEx() and sending a WM_CLOSE to the main window.

      L A 2 Replies Last reply
      0
      • M mark novak

        You could use CreateProcess() and TerminateProcess(), however TerminateProcess() ends a program so abruptly that some applications don't handle it well, it doesn't give them time to save their settings or other exit code. I'd recommend using FindWindowEx() and sending a WM_CLOSE to the main window.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        PostQuitMessage is another way. The tigress is here :-D

        D A 2 Replies Last reply
        0
        • A Amarelia

          Hello Friends I have an application in which I need to start one exe from another one and at the end when the first one ends, it should automatically end the second one. To start I use ShellExecute() or WinExec() function. And to terminate the another exe I put code into the destructor of the first one from where I have started it. But the problem is I don't know the Handle of the new exe that I have started using ShellExecute() or WinEec()..... Is there any functions which can give me the Handle of a perticular window or is there any other way to stop the application without knowing the handle or process id ? Thankx in advance My Code Looks Like : ------------------- My first.exe file ================ MyClass() { // Do some work here........ WinExec("C:\\Temp\\second.exe",SW_SHOW); // Do some work here... }; ~MyClass() { Here I want to kill(Terminate) the second.exe } Any help plz ? Thankx } } Amarelia Maehsh Gujarat India

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Amarelia wrote: But the problem is I don't know the Handle of the new exe that I have started using ShellExecute()... Why not? It's in the PROCESS_INFORMATION structure that ShellExecute() populates.


          "One must learn from the bite of the fire to leave it alone." - Native American Proverb

          A 1 Reply Last reply
          0
          • L Lost User

            PostQuitMessage is another way. The tigress is here :-D

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Not a good idea if handling other messages during shutdown is a concern. Call PostMessage(WM_CLOSE) instead. PostQuitMessage() does not shut down an application properly as other messages might be initiated by the shutdown operation (e.g., save the document?) and those messages, along with any other messages that might be in the queue, will not be processed.


            "One must learn from the bite of the fire to leave it alone." - Native American Proverb

            L 1 Reply Last reply
            0
            • D David Crow

              Not a good idea if handling other messages during shutdown is a concern. Call PostMessage(WM_CLOSE) instead. PostQuitMessage() does not shut down an application properly as other messages might be initiated by the shutdown operation (e.g., save the document?) and those messages, along with any other messages that might be in the queue, will not be processed.


              "One must learn from the bite of the fire to leave it alone." - Native American Proverb

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Thank you David, that's well worth knowing. Elaine :rose: The tigress is here :-D

              1 Reply Last reply
              0
              • D David Crow

                Amarelia wrote: But the problem is I don't know the Handle of the new exe that I have started using ShellExecute()... Why not? It's in the PROCESS_INFORMATION structure that ShellExecute() populates.


                "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                A Offline
                A Offline
                Amarelia
                wrote on last edited by
                #7

                Thank you very much I did the same but bit differently. I used CreateProcess() and in that there is last parameter as [out] parameter which is PROCESS_INFORMATION structure and I use that to close application. thankx once again Amarelia Maehsh Gujarat India

                D 1 Reply Last reply
                0
                • L Lost User

                  PostQuitMessage is another way. The tigress is here :-D

                  A Offline
                  A Offline
                  Amarelia
                  wrote on last edited by
                  #8

                  Thankx for taking time to read my question... Amarelia Maehsh Gujarat India

                  1 Reply Last reply
                  0
                  • M mark novak

                    You could use CreateProcess() and TerminateProcess(), however TerminateProcess() ends a program so abruptly that some applications don't handle it well, it doesn't give them time to save their settings or other exit code. I'd recommend using FindWindowEx() and sending a WM_CLOSE to the main window.

                    A Offline
                    A Offline
                    Amarelia
                    wrote on last edited by
                    #9

                    Thankx a lot for giving attention to my question .... Amarelia Maehsh Gujarat India

                    1 Reply Last reply
                    0
                    • A Amarelia

                      Thank you very much I did the same but bit differently. I used CreateProcess() and in that there is last parameter as [out] parameter which is PROCESS_INFORMATION structure and I use that to close application. thankx once again Amarelia Maehsh Gujarat India

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      I'm glad you figured it out because my suggestion was wrong. I meant to say CreateProcess() not ShellExecute().


                      "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups