Announcement

Collapse
No announcement yet.

Legal Use of CallBack Function?

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

  • Legal Use of CallBack Function?

    I was looking at the serial port sample from PowerBASIC Inc and noticed several callback functions in use:

    Code:
    CallBack Function Dialog_Callback () As Long
    CallBack Function Send_Callback () As Long
    CallBack Function SendFile_Callback() As Long
    CallBack Function ReceiveFile_Callback () As Long
    CallBack Function Quit_Callback () As Long
    I was surprised because only one of them is associated with the dialog. The others are simply called like any other function. Also, the extra callback functions use CB.HNDL (but none of the other CB statements).

    I didn't know you could use a callback not tied to a dialog/control, nor did I know that an "un-tied" callback would be given the CB values - but from which message? And suppose there were 2 dialogs in the apps, what would CB.hndl return?

    Can someone clarify why/how a Callback is used in this fashion?

  • #2
    The other ones are more than likely tied to controls. See Control Add Button in the help file. It should explain the optional Call keyword usage there as well as on other controls.

    In a nutshell you can assign a callback to individual controls as well as the dialog.
    Scott Slater
    Summit Computer Networks, Inc.
    www.summitcn.com

    Comment


    • #3
      Yep that was it. I knew controls could have callbacks too, but I thought I saw code which called the Callback functions directly. I was wrong - my eyes told my brain something that wasn't true.

      Comment


      • #4
        No it is true.

        For example
        Code:
                            eStream.pfnCallback = CODEPTR(RichEditStreamInString)                                     'pointer to RichEdit callback procedure
                            gPos = 1
                            gTxt = BUILD$(RtfHeader, StaticRtfText, RtfEnd)
        'if len(gTxt) > = 65535 then msgbox "Hold It, I am at 65535"
                            gPtr = STRPTR(gTxt)
                            SendMessage HwndRichEdit, %EM_STREAMIN, %SF_RTF, VARPTR(eStream)                          'Send Message to Window
        and
        Code:
        '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
        ' Rich Edit stream in callback - for streaming in string contents
        '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
             FUNCTION RichEditStreamInString (BYVAL dwCookie AS DWORD, BYVAL pbBuff AS BYTE PTR, BYVAL cb AS LONG, pcb AS LONG) AS DWORD
                  pcb = MIN&(cb, LEN(gTxt) - (gPos - 1)) 'number of bytes to copy
                  IF pcb > 0 THEN 'copy block from global string directly into Richedit's buffer.
                          CopyMemory pbBuff, (gPtr + gPos - 1), pcb 'could use POKE$ too, but this is a bit faster
                          gPos = gPos + pcb 'incr pos for next callback position.
                 ELSE
                      FUNCTION = %True    'Break the action
                 END IF
             END FUNCTION
        Engineer's Motto: If it aint broke take it apart and fix it

        "If at 1st you don't succeed... call it version 1.0"

        "Half of Programming is coding"....."The other 90% is DEBUGGING"

        "Document my code????" .... "WHYYY??? do you think they call it CODE? "

        Comment


        • #5
          but I thought I saw code which called the Callback functions directly. I was wrong - my eyes told my brain something that wasn't true.
          Well, you may well have seen that, and done carefully it will work OK, but I can't accept directly calling a callback function of any type (not just a PB "CALLBACK FUNCTION...") as good programming practice.

          AFAIK, the PB syntax "CALLBACK FUNCTION FOO()" is just a kind of shorthand for....
          Code:
          FUNCTION FOO (BYVAL hWnd AS LONG, BYVAL uMSG AS LONG, _ 
                               BYVAL wParam AS LONG, BYVAL lParam AS LONG ) AS LONG
          ...with the additional proviso that the "CALLBACK FUNCTION FOO()" syntax must be used to describe the callback functions used in any DIALOG SHOW or CONTROL ADD statement.

          MCM
          Last edited by Michael Mattias; 15 Sep 2009, 11:50 AM.
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment

          Working...
          X