Results 1 to 6 of 6

Thread: Question about Callbacks.

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    45

    Talking

    I'm trying to figure out what a callback procedure is exactly and why it is useful. It looks to me to be something like this:

    Total = GetTotal (x as integer, y as integer, FigureTax) as Single
    X + Y = Total
    End Function

    Where GetTotal is just a general procedure and AFTER the GetTotal general procedure executes it will automatically run the FigureTax procedure. So you get the total of X+Y figured by GetTotal and then the FigureTax applies a tax formula to create the final value for Total.

    Is this accurate?
    -Gregg
    -NoOBie At LaRg3

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Callback - is a function that receives messages from the operating system. Callback functions are application-defined. In other words callback function is telling the program what to do with intercepted events.

    If you look from VB point of view, every event you have available in VB (MouseDown, MouseUp, Click, Paint etc) are events that are being raised by Callback function.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    45

    Okay....

    So in my example if the FigureTax part of my procedure ran and fetched the time (for a totally nonsense example) then that would make it a callback? If it used the "time" to figure the tax. (yes, the example makes no sense)
    -Gregg
    -NoOBie At LaRg3

  4. #4
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    No. The only callback functionality is available in VB is an AddressOf operator.

    Here is a little article from MSDN:
    AddressOf - A unary operator that causes the address of the procedure it precedes to be passed to an API procedure that expects a function pointer at that position in the argument list.

    The required procedurename specifies the procedure whose address is to be passed. It must represent a procedure in a standard module module in the project in which the call is made.

    Remarks

    When a procedure name appears in an argument list, usually the procedure is evaluated, and the address of the procedure’s return value is passed. AddressOf permits the address of the procedure to be passed to a Windows API function in a dynamic-link library (DLL), rather passing the procedure's return value. The API function can then use the address to call the Basic procedure, a process known as a callback. The AddressOf operator appears only in the call to the API procedure.

    Although you can use AddressOf to pass procedure pointers among Basic procedures, you can't call a function through such a pointer from within Basic. This means, for example, that a class written in Basic can't make a callback to its controller using such a pointer. When using AddressOf to pass a procedure pointer among procedures within Basic, the parameter of the called procedure must be typed As Long.

    Warning Using AddressOf may cause unpredictable results if you don't completely understand the concept of function callbacks. You must understand how the Basic portion of the callback works, and also the code of the DLL into which you are passing your function address. Debugging such interactions is difficult since the program runs in the same process as the development environment. In some cases, systematic debugging may not be possible.

    Note You can create your own call-back function prototypes in DLLs compiled with Microsoft Visual C++ (or similar tools). To work with AddressOf, your prototype must use the __stdcall calling convention. The default calling convention (__cdecl) will not work with AddressOf.

    Since the caller of a callback is not within your program, it is important that an error in the callback procedure not be propagated back to the caller. You can accomplish this by placing the On Error Resume Next statement at the beginning of the callback procedure.


  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2000
    Posts
    45

    What about....

    The DTPicker control uses a callback function without the AddressOf keyword. Is that a case of a second type of callback or is the callback encapsulated in the ActiveX control to hide the implementation from the developer?
    -Gregg
    -NoOBie At LaRg3

  6. #6
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Callback functions are not exposed to the developer to simplify access to the control's events. Usually, if ActiveX controls are created with C++ then you can be sure that all events there, are a result of the callback function intercepted a message that was sent to the control.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width