What is hook, when and why its' used ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: What is hook, when and why its' used ?

  1. #1
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    What is hook, when and why its' used ?

    Hello to all expert programmer, i hope someone can clear my confusion.
    Thanks for your help.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,274

    Re: What is hook, when and why its' used ?

    It might be good to give some context. FOLDOC has a possible explanation of hook (adapted from an explanation by Eric Raymond's The Jargon File). In terms of software one example would be the use of the non-virtual interface idiom, where the virtual function is a hook that allows derived classes to add to the behaviour of the base class non-virtual member function.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: What is hook, when and why its' used ?

    It is a type of Callback.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: What is hook, when and why its' used ?

    AFAIK, it's an entry point to program which developer is allow to insert some code.

    Do i correct ?
    Thanks for your help.

  5. #5
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: What is hook, when and why its' used ?

    Quote Originally Posted by Peter_APIIT View Post
    AFAIK, it's an entry point to program which developer is allow to insert some code.

    Do i correct ?
    No you are not correct.

    An entry point is an address that something calls to begin execution. For a classic C++ program main() is the entry point (from an application perspective, the actual EXE entry point will be a routine to setup the environment.

    A callback is the exqact opposite. It is where you provide an address to some other code, enabling that code to call you at an appropriate time. [the callback target address is the entry point into your routine.

    You can think of it as looking at things either normally or in a mirror image...

    A Hook, is a callback system that usually (but not always) has the following characteristics.

    1) There is an API for setting and removing entries.
    2) The callbacks are chained (typically with an option to call/not call the next element in the chain
    3) The add/remove calls are disassociated from the actual invokaction action.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: What is hook, when and why its' used ?

    I would not necessarily call it an entry point (such as the classic entry point of an application) but hooks can be used for various purposes (even malicious ones). A simple search on the internet should actually provide you with enough material to read on such as:

    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  7. #7
    Join Date
    Nov 2003
    Posts
    1,405

    Re: What is hook, when and why its' used ?

    Quote Originally Posted by Peter_APIIT View Post
    Hello to all expert programmer, i hope someone can clear my confusion.
    Generally it means that there's something missing in some code which a user of the code is supposed to fill in.

    Specifically in OO a good example is the Template Method design pattern. The idea is that a base class provides a concrete implementation of some algorithm but makes one or a few calls to pure virtual functions in it. These are the hooks. A user then inherits the base class and defines the pure virtual functions (the hooks) to his liking.

    In this way the base class defines a template for an algoritm but leaves some hooks (calls to pure virtual functions), which are then given concrete implementations in derived classes to complete the algorithm.
    Last edited by _uj; December 29th, 2008 at 07:44 AM.

  8. #8
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: What is hook, when and why its' used ?

    Quote Originally Posted by _uj View Post
    Generally it means that there's something missing in some code which a user of the code is supposed to fill in..
    Not really. Consider all of the Windows API low level hooks. Nothing is "missing", rather there is an opportunity to "catch" activity and augment or replace the default behvaiour (which could be "do nothing").

    There is a story about the original use of this term. I do not know if it is true, but it serves as decent analogies...

    Fishing: You throw a hook in the water. When a fish swims by [which it would have done anyway] it bites the hook, and your can reel it on. At this point you can keep it [prevent the original behaviour] tag it [alter the existing behaviour], or simply throw it back [prevent the original behaviour]
    .
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: What is hook, when and why its' used ?

    This thread has open up discussion from expect and hope help newbie too. Thanks for all your help.
    Last edited by Peter_APIIT; December 29th, 2008 at 08:52 AM.
    Thanks for your help.

  10. #10
    Join Date
    Nov 2003
    Posts
    1,405

    Re: What is hook, when and why its' used ?

    Quote Originally Posted by TheCPUWizard View Post
    Fishing: You throw a hook in the water.
    Analogies have a tendency to obscure things rather than clarifying them. Here's the real thing:

    A hook is a place in some code where a user can inject own code. Another way to express this is to say that a hook represents missing user action. When the user supplies it, it isn't missing anymore.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,274

    Re: What is hook, when and why its' used ?

    Quote Originally Posted by _uj
    A hook is a place in some code where a user can inject own code.
    I'd say that that is more accurate than a definition that states that something is "missing". The non-virtual interface idiom/template method pattern is an example: the "hook" provided might not actually be needed, and the base class might provide an optional action by means of a defined pure virtual function. In either case, nothing is actually missing, but the user is able to inject or "plug in" his/her own code via the virtual member function.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Nov 2008
    Location
    Netherlands
    Posts
    77

    Re: What is hook, when and why its' used ?

    win32 hooks are not really hooks, they are callbacks.

    a hook is a hack to edit the entry point of an export function. by doing this you can point the entry point to your own code.

    for example, you can hook the CreateProcessExA / W function edit parameters and then call the adress to the real CreateProcessExA / W function.

    i have used these methods in the past to make bots for FPS games.

    you dont tell the water to pull your hook in, comparing win32 hooks to fishing is epic fail.
    Last edited by cj-wijtmans; December 29th, 2008 at 03:37 PM.

  13. #13
    Join Date
    Aug 2007
    Posts
    858

    Re: What is hook, when and why its' used ?

    Another way to express this is to say that a hook represents missing user action. When the user supplies it, it isn't missing anymore.
    Not necessarily. When you need to modify a program in memory (via DLL injection or etc), you'll probably end up inserting your own hooks (inserting a jmp instruction somewhere, overwriting a vtable entry, etc) because you simply need something to happen at that point, not because anything is missing.

  14. #14
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: What is hook, when and why its' used ?

    Based on all expert opinion, i think hook is an action to inject some code to another piece of code.
    Thanks for your help.

  15. #15
    Join Date
    Jun 2008
    Posts
    592

    Re: What is hook, when and why its' used ?

    I am going to talk about win32 hooks.

    There are many different types of hooks. Win32 hooks are for monitoring and/or modifying events.

    Win32 Hooks
    http://msdn.microsoft.com/en-us/library/ms997537.aspx

    A common hook is WH_KEYBOARD or WH_GETMESSAGE. You use SetWindowsHook. http://msdn.microsoft.com/en-us/library/ms644990(VS.85).aspx

    This hook triggers a callback function for each event which is being hooked for the current thread being monitored.

    For ex
    Code:
    LRESULT CALLBACK Messages( int nCode, WPARAM wParam, LPARAM lParam)
    {
         // monitor/edit here
    }
     SetWindowsHookEx( WH_CALLWNDPROC, &Messages, 0, GetCurrentThreadId() );
    In order for SetWindowHookEx, you must be in the same process in which you want to monitor
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center