Thread: Callback Functions

  1. #1
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507

    Callback Functions

    I keep hearing this word thrown out when I read about events in C#. Some reading says it has its roots in C and specifically used in the WinAPI. I guess its some sort of function pointer? Any examples or explanations? Thanks.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    In general - it is function called by pointer

    For example you create dll (like winsock dll) that should call some funtion in the exe - you cannot link your dll to the exe directly - because there is no exe at the moment when the dll is compiled.

    So dll gives the possibility to set the CB function - the exe provides the pointer to the function which should be called while initializing the dll

    so the dll can now call this CB function (having its address at the run-time)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Ok, I think i understand that. A big thing I didn't understand is why the line:
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

    returns a callback.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It doesn't return a callback. It should itself be a callback function (called by DispatchMessage and/or the like if I'm not mistaken).

    The WindowProc function is an application-defined callback function that processes messages sent to a window.
    ...
    Return Value
    The return value is the result of the message processing and depends on the message sent.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Ahh ok I see. I guess the last thing im wondering then is how these are associated with "events" in c# or other languages that use events. Is it due to the fact that something listens for an event and then calls a function through a pointer?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's probably due to that C# was built in C or C++, and thus the underlying implementation uses function pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    67
    Last edited by Greenhorn__; 07-31-2008 at 12:24 PM.

  8. #8
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    So all in all a callback function is just a way to store a function/method in a pointer so it can be called later? Maybe it is because I haven't used function pointers a lot, but I dont see the point of this...Why not just call the function normally?

    I guess I could see it come to think of it...Such as setting what this function to be called will be in a parameter or something. Maybe I should toy around with them more.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What if you don't know what function to call at compile time?
    Say you write a utility library with information that you want to call a function with to the one who uses the dll, but what's the function, what's it's name and location?
    And what if the user wants to use a different function and name?

    This is where function pointers come in handy. The user specifies the function which they want the dll to call.

    And for that point, calling the same function with the information might have disadvantages when the function is called from different places in the code. You might want different functions to perform different tasks with the information.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Ahh ok I see. Ill have to play around with them, they don't sound as hard as I was making them out to be ><

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    qsort() uses a callback function.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by valaris View Post
    I keep hearing this word thrown out when I read about events in C#. Some reading says it has its roots in C and specifically used in the WinAPI. I guess its some sort of function pointer? Any examples or explanations? Thanks.
    Callbacks are generally used to achieve decoupling and abstraction. From the standpoint of code structure, a callback is used to pass control "elsewhere," not necessarily along the neat up-and-down lines of a hierarchical design. It's a way to tell another piece of code to perform "something" at certain points without that code being aware of what "something" is.

    To make an analogy with C++, a callback is like a virtual function invoked through a base pointer/reference -- the caller does not specifically know which function is being invoked, but it does know that it adheres to some specific interface.

    Typically a callback is defined as a typedef of a function pointer. The abstract function takes some task-specific arguments and usually an opaque pointer to a piece of callback context -- the opaque pointer points to a black box chunk of memory which contains any resources specific to that callback -- again, in analogy with C++ this context pointer is similar to "this."

    A structure of a callback might look like this:

    Code:
    typedef struct callback
    {
        void *data;
        int (*func)(void *, ...);
    } callback;
    And a piece of code utilizing the callback might look like this:

    Code:
    void code_with_callback(callback *cb)
    {
        /* Do stuff... */
        /* Invoke the callback */
        cb->func(cb->data, x);
        /* More stuff */
        /* Maybe invoke it again */
        cb->func(cb->data, y);
        /* ... */
    }
    Because of the variable argument list "...", the callback is not really type-safe. It could be passed any number of arguments or none at all, aside from the context pointer. You could create a specific callback structure for every callback type you use, or you can accept the type-unsafety and use the same structure for everything.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  2. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  3. Replies: 1
    Last Post: 08-24-2008, 11:39 AM
  4. Callback functions from dll
    By Calthun in forum Windows Programming
    Replies: 2
    Last Post: 07-09-2004, 04:13 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM