Thread: What is a hook function?

  1. #1
    Registered User
    Join Date
    Jul 2005

    What is a hook function?

    Can someone give an example? Is it the same as a callback function? Is a signal handler a hook function? Thx.
    Last edited by hzmonte; 08-19-2007 at 09:47 PM.

  2. #2
    Registered Abuser
    Join Date
    Jun 2006
    Yes, a hook function essentially is a callback function. An extensible program will have several "hook" or "extend points" onto which a third-party programmer wishing to extend or "plug-in" capabilites may "hook" more functionality onto it.

    For example, if I create an employee management software package, unless told otherwise, it would be advantageous if I created a generic "base" program that encorperates all functionalities that one may want in such a system. One such function might be something like PayEmployee(). It is safe to say that almost every user will need to use this function, however likewise almost every user will have something different or custom that needs to be done as well. Since I don't know who will want to do what in the future, I will just create "hooks" in my program that others can hook onto. So my PayEmployee function might look like this:
    void PayEmployee(Employee anEmployee, int amount)
        void *(*hookList)(Employee, int) = getPaymentHookList();
        while(*hookList != NULL)
            (*hookList)(anEmployee, amount);
    void addPaymentHook(void (*paymentFunc)(Employee, int))
    You'll have to excuse me on my function pointer array, I'm not sure if I've gotten that completely right (check here for a proper example of this), but this was just to illustrate a concept.
    This function starts off with the generic code to transfer funds, but then calls every function in the hookList with the parameters it received. So now any third-party person looking to extend my funtion only has to add his/her own functions to the hookList (by calling addPaymentHook and supplying their function name).

    The other common use of "hook" is registering hooks for Windows API calls, which uses the same theory above but implemented at a much more complex level by the OS.

  3. #3
    Registered User
    Join Date
    Jul 2005
    In most of the examples for callback functions I saw on the Web, the callback function is passed as a parameter of the caller function. Now, in payEmployee(), the callback function is pre-stored in a global variable - a hook list. So, if one wants to make a distinction between a hook function and a callback function, is this it? And what is the pro/con and implication of each of these two approaches? How are they related to the way each is usually used, i.e. their application?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM