Announcement

Collapse
No announcement yet.

Terminating an application

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Terminating an application

    Hi all,

    Is there a way for one application to terminate another one, without the need for user intervention to do it?

    If I have two (written in BP/Win) applications running (for example, MyApp1 and MyApp2), I would like MyApp2 to be able to terminate MyApp1, just as if a user had clicked on the red "close" X at the top-right of the App1 program window.

    Easy, difficult, or nearly impossible?

    If it matters, we are using PB/Win 8 on Windows XP Pro.

    Thanks in advance to anyone that can help with this one.

  • #2
    Here's one way.
    Code:
    $MyApp1 = "The caption bar text"
    ..
    hWin = FindWindow (ByVal %Null, $MyApp1)  ' find handle of target window
     
    SendMessage hWin, %WM_SYSCOMMAND, %SC_CLOSE, ByVal %Null
    Rgds, Dave

    Comment


    • #3
      Just remember the 'red "close" X' ...

      A. May not terminate the application (eg notepad, "Save changes before exit?" )
      B. May be termporarily disabled so it cannot be clicked at that time so the application will NOT receive WM_SYSCOMMAND/SC_CLOSE notifications because it would screw up something in that application.


      MCM
      Michael Mattias
      Tal Systems Inc. (retired)
      Racine WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        I haven't down it with PB, but with Visual Basic I wrote any app which actually ran other apps. You use FindWindow to find the window of the app you need to control. Then I used VB's sendkeys command to automate the other app. I used it to pass data from one app to another (a custom job tracking program which automated putting data into an off the shelf accounting program). I see no reason why you couldn't do the same, including closing the app.

        I don't know off hand the API version of VB's sendkeys command, but I am sure someone else could help there.
        Chris Boss
        Computer Workshop
        Developer of "EZGUI"
        http://cwsof.com
        http://twitter.com/EZGUIProGuy

        Comment


        • #5
          Originally posted by Michael Mattias View Post
          Just remember the 'red "close" X' ...
          ...
          A. May not terminate the application
          ...
          B. May be termporarily disabled so it cannot be clicked
          ...
          MCM
          In this case, both apps are written in PB, and not other apps such as notepad, word, etc, so I shouldn't have this issue. The one to be terminated is only a viewer and can't save anything, so it should not have any issues with data loss caused by it being closed by the second app.

          Regards,
          Chris.

          Comment


          • #6
            >In this case, both apps are written in PB,

            I guess I just 'assumed' these were not cooperating applications. That's what about 97 percent of like postings are all about.

            Of course with cooperating applications, you could send a PRIVATE message to the program to be terminated.

            e.g., if "program A" wants to terminate "program B", it may be of interest to "Program B" to know it is being terminated by "Program A" rather than by the user clicking the "X".

            MCM
            Michael Mattias
            Tal Systems Inc. (retired)
            Racine WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Not sure what you mean by "PRIVATE message" - I've never looked into two apps sending data to each other before, so I've got no idea how one would go about something like that.

              Thanks to the code provided by Dave Biggs (thanks!), this worked:

              Code:
              '/*   If the serial monitor is active, kill it **   */
              'Find the "handle" of the app (it has a caption bar called "Com1Monitor")
              'Returns 0 if the form is inactive, something else if active
              
              LOCAL MonitorHandle AS INTEGER
              LOCAL MonitorHandleString AS STRING
              
              MonitorHandle = FindWindow (BYVAL %Null, "com1monitor") 'Find handle
              MonitorHandleString=STR$(MonitorHandle)                 'Convert to string
              
               SELECT CASE MonitorHandle                              'Kill it if it's active
                CASE 0:
                CASE ELSE: SendMessage MonitorHandle, %WM_SYSCOMMAND, %SC_CLOSE, BYVAL %Null
               END SELECT
              
              'Text box for debugging in case this doesn't work
              'MSGBOX MonitorHandleString, 8192, "Data"
              Is this doing it correctly, or was it a bit of a fluke?
              It seems to work OK, but I've only given it limited testing.

              Thanks,
              Chris

              Comment


              • #8
                Not sure what you mean by "PRIVATE message" - I've never looked into two apps sending data to each other before, so I've got no idea how one would go about something like that.
                There's a difference between "sending terminate command" and "sending data to each other."

                For the simpler case (the first) sending WM_SYSCOMMAND/SC_CLOSE is just fine, assuming WM_SYSCOMMAND will always terminate the process (see above for when it won't). Then again, in this case the receiving process ("B") can't tell if the message came from the user (clicked "X") or from application "A". It may matter; it may not.

                If it does matter, instead of "A" sending WM_SYSCOMMAND/SC_CLOSE to "B", it can send a message known only to "A" and "B" by using the RegisterWindowMessage() function. (See in your SDK reference and search here, I know there are examples).

                If "A" wants to send DATA to "B", the WM_COPYDATA message can be used (ditto re further info). There are also several other ways to accomplish Interprocess Communication: pipes, mailslots, events, disk files, TCP, DDE.... . What is best depends on the application, the environment and the nature of that data.

                MCM
                Michael Mattias
                Tal Systems Inc. (retired)
                Racine WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  Thanks, I'll have a look at that in the SDK. Presumably it's a method of sending data, for example a short string, to the other app which has to be coded to perform some action when it received that data.

                  In this case, it didn't matter if the first app "knew" what closed it.

                  Thanks for the info.

                  Chris

                  Comment

                  Working...
                  X