Announcement

Collapse
No announcement yet.

SHELLs compliment function

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

  • SHELLs compliment function

    Is there a function that Quits an application from another app?

    I guess there must be cos when you shut the machine down windows issues QUIT commands to all the running apps.

    Ive looked in Poffs and the help file and windows but I dont know what its called so its hard to find.

    What i want to do is issue a QUIT command to an app in case it is running right before I SHELL to it.

    ------------------
    Kind Regards
    Mike

  • #2
    If the target app is one of your own creations, then you can PostMessage() a custom message to signal the app to close. For example, %WM_USER + 999&.

    (Provided the app has a window that is!) You can use EnumWindows() to cycle through the current windows list to track down the target app.

    Most importantly, there is no guaranteed method that will work for every conceivable application, since the programmer may have designed the app to twart any type of message-based shutdown, however the commonly advised method is to post a %WM_CLOSE or maybe a %WM_SYSCOMMAND|%SC_CLOSE message to the app's window. This queued message will take time to get processed, so a delay should be performed and then the app's existance rechecked. If the app still has not closed, try sending a %WM_QUIT message and repeat the delay+retest.

    If neither work, then it is possible the app has either not processed the messages yet (say, because it is too busy to read messages from it's queue), or it is refusing to terminate for other reasons.

    This leaves the TerminateProcess API, but that is NOT a recommended method since it will shut the app down without regard to data integrity, etc. Basically, with TerminateProcess() the app will not receive any notification - it just gets terminated by the O/S.

    There are further complications to using TerminateProcess(), since your app may need an appropriate security/privilege token to even use it (NT, etc). Probably other problems exist too... Off-hand I don't recall, but this has been discussed on the BBS before. IMHO, stay well away from TerminateProcess().

    I hope this helps...

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Thx Lance - very interesting.

      As I wrote both apps I can use the %WM_USER concept. I have written two little apps to test the idea.

      When the timer expires the 2nd app exits cleanly. When it receives a %WM_USER command it does not. I have to CTRL+ALT+Delete to force quit it, why?

      #COMPILE EXE
      #REGISTER NONE
      #DIM ALL
      #INCLUDE "win32api.inc"

      %EndNow = %WM_USER + 2060 ' Send Quit message
      '------------------------------------------------------------

      FUNCTION PBMAIN
      LOCAL hWnd AS LONG
      hWnd = FindWindow ("", "Testing")
      IF hWnd > 0 THEN
      MSGBOX "Found window"
      DIALOG SEND hWnd ,%EndNow ,0 , 0
      END IF
      END FUNCTION

      '****************************************************************


      #COMPILE EXE
      #REGISTER NONE
      #DIM ALL
      #INCLUDE "win32api.inc"

      %EndNow = %WM_USER + 2060 ' Recieve message

      '------------------------------------------------------------------------------------------
      CALLBACK FUNCTION MainDlgProc
      SELECT CASE CBMSG
      CASE %WM_INITDIALOG
      SetTimer CBHNDL, 1, 8000, BYVAL %NULL ' 8 sec delay

      CASE %WM_TIMER
      KillTimer CBHNDL, 1
      DIALOG END CBHNDL ' kill window
      EXIT FUNCTION

      CASE %EndNow ' sent from another app
      KillTimer CBHNDL, 1
      DIALOG END CBHNDL ' kill window
      EXIT FUNCTION ' doesnt seem to do this
      END SELECT
      END FUNCTION

      '--------------------------------------------------------------------------------
      FUNCTION PBMAIN
      LOCAL hDlg AS LONG
      DIALOG NEW 0 , "Testing", 500, 2, 200, 80, %WS_CAPTION OR %WS_SYSMENU TO hDlg
      DIALOG SHOW MODAL hDlg CALL MainDlgProc ' Show window
      MSGBOX "exiting pbmain"
      END FUNCTION



      ------------------
      Kind Regards
      Mike

      Comment


      • #4
        Dialog End CbHndl in CASE %EndNow will not work in similar situation.
        Replace it by PostMessage CbHndl, %WM_SYSCOMMAND, %SC_CLOSE, 0

        ------------------
        E-MAIL: [email protected]

        Comment


        • #5
          So if I have a mutex in my program how do I get the handle of the window or do this same thing via mutex?


          Scott

          ------------------
          Scott
          Scott Turchin
          MCSE, MCP+I
          http://www.tngbbs.com
          ----------------------
          True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

          Comment


          • #6
            Scott, even if your program can determine that another program is using your mutex (probably another instance of your program), there's no real way to determine which program is using the mutex. How about using FindWindow() ?


            ------------------
            -

            Comment

            Working...
            X