Announcement

Collapse
No announcement yet.

I need to terminate a running program from the tray

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

  • I need to terminate a running program from the tray

    I started in the PBCC area, and thought I would move the question
    here. I have a program in PBDLL, it is written and working.
    Problem, does not show on the task bar (I like that) but it does
    no DIALOG at all, just file IO in the background.

    I would like to externally kill this program. A tray program,
    that I launch from this program, that find and kill the this
    program and unload itself (Now, I control-alt-del and find it
    and kill it by hand). This would serve more that just me I am
    sure. Since anyone can kill there code via the tray by launching
    this program. Does anyone have such a program, or can anyone
    help me with the parts. The tray sample, I have no idea how to
    drop in my code.

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

  • #2
    It's fairly simple to arrange it so that you can stop the program using the tray example code.
    (Much better and cleaner than trying to kill the process with Task Manager).

    Anyway, I quickly modified the tray code to use a thread which then signals the main code to
    close, using a global variable as the signal bearer.

    This should get you started... significant sections are in bold.

    Code:
    '------------------------------------------------------------------------------
    '
    '  System Tray Example for PB/DLL 6.0.
    '  Copyright (c) 1999 PowerBASIC, Inc.
    '
    '  Shows how to create and change an icon in the Windows system tray.
    '
    '------------------------------------------------------------------------------
    
    #COMPILE EXE
    #RESOURCE "TRAY.PBR"
    #INCLUDE "WIN32API.INC"
    
    %WM_TRAYICON = %WM_USER + 400
    
    GLOBAL hInst AS LONG
    
    GLOBAL DoStop&
    FUNCTION PBMAIN
        hInst = GetModuleHandle(BYVAL 0&)
        
        THREAD CREATE MyTray(0) TO tResult&
        THREAD CLOSE tResult& TO tResult&
        
        WHILE ISFALSE DoStop&
            '... main computational code goes here
        WEND
    END FUNCTION
     
    FUNCTION MyTray(BYVAL x AS LONG) AS LONG
      DialogBox hInst, BYVAL 100&, %HWND_DESKTOP, CODEPTR(DialogProc)
    END FUNCTION
        
     
    FUNCTION DialogProc(BYVAL hDlg AS LONG, BYVAL wMsg AS LONG, BYVAL wParam AS LONG, BYVAL lParam AS LONG) AS LONG
    
      STATIC hMenu AS LONG
      STATIC ti    AS NOTIFYICONDATA
      STATIC p     AS POINTAPI
    
      SELECT CASE wMsg
    
        CASE %WM_INITDIALOG
          ' Get Menu Handle
          hMenu = GetSubMenu(LoadMenu(hInst, "POPUPMENU"), 0)
    
          ' Add tray icon
          ti.cbSize           = SIZEOF(ti)
          ti.hWnd             = hDlg
          ti.uID              = hInst
          ti.uFlags           = %NIF_ICON OR %NIF_MESSAGE OR %NIF_TIP
          ti.uCallbackMessage = %WM_TRAYICON
          ti.hIcon            = LoadIcon(hInst, "FACE1")
          ti.szTip            = "Task Tray Example"
          Shell_NotifyIcon %NIM_ADD, ti
          DestroyIcon ti.hIcon
    
          FUNCTION = 1
    
        CASE %WM_TRAYICON
    
          SELECT CASE LOWRD(lParam)
    
            ' Left button press
            CASE %WM_LBUTTONDOWN
              IF IsWindowVisible(hDlg) = %FALSE THEN
                ShowWindow hDlg, %SW_SHOW
              END IF
    
            ' Right button press
            CASE %WM_RBUTTONDOWN
              IF IsWindowVisible(hDlg) = %FALSE THEN
                SetForegroundWindow hDlg
                GetCursorPos p
                TrackPopupMenu hMenu, 0, p.x, p.y, 0, hDlg, BYVAL %NULL
                Postmessage hDlg, %WM_NULL, 0, 0
              END IF
    
          END SELECT
    
        CASE %WM_DESTROY
          ' ** Remove the tray icon if the application is closed
          Shell_NotifyIcon %NIM_DELETE, ti
    
        CASE %WM_COMMAND
          SELECT CASE LOWRD(wParam)
    
            CASE 101, 102, 103, 104, 105
               ' Change tray icon
               ti.hIcon            = LoadIcon(hInst, "FACE" & FORMAT$(LOWRD(wParam) - 100) )
               Shell_NotifyIcon %NIM_MODIFY, ti
               DestroyIcon ti.hIcon
    
            CASE %IDOK
              ' Display the about box
              DialogBox hInst, BYVAL 101&, %HWND_DESKTOP, CODEPTR(AboutProc)
    
            CASE %IDCANCEL
              ' Make sure they want to exit
              IF MessageBox(hDlg,"Close Tray Example and remove its icon from the task tray?" , "Tray Example", %MB_ICONEXCLAMATION OR %MB_OKCANCEL) = %IDOK THEN
                EndDialog hDlg, 0
                DoStop& = -1 ' signal the main code
                FUNCTION = 1
              END IF
    
          END SELECT
    
        CASE %WM_SYSCOMMAND
    
          ' If either the minimize or close buttons are pressed, hide the
          ' window so it doesn't appear in the task bar.
    
          SELECT CASE LOWRD(wParam)
    
            CASE %SC_MINIMIZE
              ShowWindow hDlg, %SW_HIDE
              FUNCTION = 1
              EXIT FUNCTION
    
            CASE %SC_CLOSE
              ShowWindow hDlg, %SW_HIDE
              FUNCTION = 1
              EXIT FUNCTION
    
          END SELECT
    
      END SELECT
    
    END FUNCTION
     
    FUNCTION AboutProc(BYVAL hDlg AS LONG, BYVAL wMsg AS LONG, BYVAL wParam AS LONG, BYVAL lParam AS LONG) AS LONG
    
      SELECT CASE wMsg
    
        CASE %WM_INITDIALOG
    
          FUNCTION = 1
    
        CASE %WM_COMMAND
    
          IF LOWRD(wParam) = %IDOK THEN
            EndDialog hDlg, 0
            FUNCTION = 1
          END IF
    
      END SELECT
    
    END FUNCTION

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

    Comment


    • #3
      Lance,

      Thanks for the fast response.

      I added a open file, write to file sleep where you have
      add code here.

      And when I kill the ICON from the tray, it does not kill the
      write and sleep routine? Was it supposed to?

      I am still tring to figure out how to kill the 5 icon pick thing
      too with no luck, but I know RC and RBRES now Slow but sure..


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

      Comment


      • #4
        The concept is that your code needs to check the value of DoStop& periodically, and when it becomes non-zero, exit PBMAIN.


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

        Comment


        • #5
          Got that part, I have not moved real code. Tring to get
          rid of the pick the face?.ico part now. I think I can get the
          code moved in OK though. ALSO, it did start dieing, not sure
          why it did not the first time, maybe be not checking the
          DoStop.

          I will watch that variable and die one it.

          Guess I need to understand the RC file now.

          I removed 100 from it, and left 101 and 102 and that really messed it
          up. Thinking it would skip the first dialog.


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

          Comment


          • #6
            Larry,

            I use system tray programs that I write *EXTENSIVELY*. If you wish,
            send me your e-mail address to my e-mail address listed below, and
            I will attach one of my BAS programs so you can see how to do it.
            My programs, at least some of them, have no need to use RC's - they
            use the MENU statements to generate the popup menu's before placing
            the icon in the tray. Also, none of my programs require a startup
            dialog, as does DialogBox when used as it is in the TRAY sample.
            I've been using my technique for a LONG time, and I pretty much
            have it down.

            Regards,

            CORRECTION: *ALL* of my system tray programs require an RC, but the
            MAJORITY of them only need it to embed the icon that the tray program
            uses. MOST of the programs create their own menu's with the
            MENU statements.

            ------------------
            Clay C. Clear

            Clay Clear's Software
            mailto:[email protected][email protected]</A>

            [This message has been edited by Clay Clear (edited September 20, 2001).]

            Comment


            • #7
              Larry,

              On second thought, I think you should use Lance's code. While
              I've thought of a possible way to do what you need, Lance is a much
              more accomplished programmer than I, and I think his idea of using
              a thread is the best idea ('though I've never, ever used threads,
              myself ).

              As far as your RC, I'd say, for a need such as yours, you should
              only include the icon itself in your RC, and build your menu's
              before putting the icon in the tray. Build your menu's with
              PB/DLL's builtin MENU statements. That's what I do for my programs
              that don't have "deep" menu requirements. If you think that your
              program requires it, an RC is the best way (*I* think) for the more
              complex menu structures. In fact, for ALL of the programs I've
              written that are for my own, private use, I use a custom DLL that
              is nothing but a resource DLL, and use LoadLibrary to load the
              applicable icons and menu's. If you need help with such a thing,
              or anything like it, feel free to e-mail me. Keep in mind that
              I have NEVER used threads in my programs, and couldn't do much for
              you in that respect, until I did some research on the matter. For
              that, you should ask Lance for clarifications.

              Keep in mind that a system tray program does NOT necessarily
              need a startup dialog. As stated in my first posting, none of my
              programs use/require them. Lance (and others) can give you some
              of the more "conventional" methods for doing this; my methods
              are more "iconoclastic." (but, they work just FINE! )

              Regards,


              ------------------
              Clay C. Clear

              Clay Clear's Software
              mailto:[email protected][email protected]</A>

              Comment


              • #8
                larry,

                have a look at http://www.powerbasic.com/support/pb...ad.php?t=15267

                regards
                peter

                ------------------
                [email protected]
                [email protected]
                www.dreammodel.dk

                Comment


                • #9
                  I wrote these awhile ago. Haven't fully tested it but these
                  should make using System Tray Icons easier

                  Code:
                  SUB DESTROY_SYSTRAY(hWnd&)
                          DIM nfIconData AS NOTIFYICONDATA
                          DIM szIcon AS ASCIIZ * 255
                          szIcon = Icon$
                          hInstance& = GETMODULEHANDLE(BYVAL %NULL)
                          nfIconData.hwnd = hWnd&
                          nfIconData.uID = hInstance&
                          SHELL_NOTIFYICON %NIM_DELETE, nfIconData
                  END SUB
                  
                  SUB SHOW_SYSTRAY(hWnd&, CallBackMSG&, Icon$, ToolTip$)
                          DIM nfIconData AS NOTIFYICONDATA
                          DIM szIcon AS ASCIIZ * 255
                          szIcon = Icon$
                          hInstance& = GETMODULEHANDLE(BYVAL %NULL)
                          nfIconData.hwnd = hWnd&
                          nfIconData.uID = hInstance&
                          nfIconData.uFlags = %NIF_ICON OR %NIF_MESSAGE OR %NIF_TIP
                          nfIconData.uCallbackMessage = CallBackMSG&
                          nfIconData.hIcon = LOADICON(hInstance&, szIcon)
                          nfIconData.szTip = ToolTip$
                          nfIconData.cbSize = SIZEOF(nfIconData)
                          SHELL_NOTIFYICON %NIM_ADD, nfIconData
                          DestroyIcon nfIconData.hIcon
                  END SUB
                  Here's how to use it

                  Code:
                  Call this in PBMAIN:
                  
                  SHOW_SYSTRAY hWnd&, %WM_USER + 1000, "#101", "Test"        
                                                          |----- This is the icon identifier 
                                                                 Resource
                  
                  in callback function place this stuff
                  CASE %WM_USER + 1000
                               SELECT CASE LOWRD(lParam&)
                                   CASE %WM_LBUTTONDOWN
                                       MSGBOX "Left"
                                   CASE %WM_RBUTTONDOWN
                                       MSGBOX "Right"
                                END SELECT     
                  CASE %WM_DESTROY
                        DESTROY_SYSTRAY hWnd&   'you can use CBHNDL too
                  ..
                  ..
                  Hope this helps everyone, I might posted this in the Source Code
                  forum after I test everything again.

                  ------------------
                  -Greg

                  [This message has been edited by Gregery D Engle (edited September 21, 2001).]
                  -Greg
                  [email protected]
                  MCP,MCSA,MCSE,MCSD

                  Comment

                  Working...
                  X