-
December 24th, 2008, 01:39 AM
#1
What is hook, when and why its' used ?
Hello to all expert programmer, i hope someone can clear my confusion.
Thanks for your help.
-
December 24th, 2008, 01:52 AM
#2
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.
-
December 24th, 2008, 01:47 PM
#3
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,2010
In theory, there is no difference between theory and practice; 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
-
December 28th, 2008, 12:45 AM
#4
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.
-
December 28th, 2008, 11:35 AM
#5
Re: What is hook, when and why its' used ?
Originally Posted by Peter_APIIT
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,2010
In theory, there is no difference between theory and practice; 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
-
December 28th, 2008, 11:41 AM
#6
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:
-
December 29th, 2008, 08:38 AM
#7
Re: What is hook, when and why its' used ?
Originally Posted by Peter_APIIT
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 08:44 AM.
-
December 29th, 2008, 09:43 AM
#8
Re: What is hook, when and why its' used ?
Originally Posted by _uj
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,2010
In theory, there is no difference between theory and practice; 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
-
December 29th, 2008, 09:47 AM
#9
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 09:52 AM.
Thanks for your help.
-
December 29th, 2008, 01:57 PM
#10
Re: What is hook, when and why its' used ?
Originally Posted by TheCPUWizard
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.
-
December 29th, 2008, 02:06 PM
#11
Re: What is hook, when and why its' used ?
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.
-
December 29th, 2008, 04:33 PM
#12
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 04:37 PM.
-
December 29th, 2008, 05:41 PM
#13
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.
-
December 29th, 2008, 09:46 PM
#14
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.
-
December 29th, 2008, 10:14 PM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|