Hook vs callback methods?

Last Edited By Krjb Donovan
Last Updated: Mar 11, 2014 07:51 PM GMT

QuestionEdit

Hello,

May I ask you one question? Do you know what is the differences between Hook and Callback methods?

Thanks, lzzzz

AnswerEdit

A callback involves passing code to be executed (e.g. a function pointer) to other code.

That is it is by design. "You pass me a function and I'll call it when appropriate"

Hooking a function, or other entity (e.g. events or messages) may employ callbacks - an API may be provided but it may also be done using less up front means - e.g. by reverse engineering code and replacing calls to functions with calls to other functions.

In the dim and distant past on old 16-bit systems I have 'hooked' at a low level using assembler to chain my code to the existing code by finding the existing system-known entry point address of the handler code in question, stashing it as the target to jump to following execution of my extension code, and replacing the system entry point address with the address to my code - effectively linking in my code before the previously installed code.

I have also had to hook - or intercept - calls to far malloc and free functions in C code designed to only execute in a single 64KB segment (again in the days of 16 bit Intel x86 based PCs and MS-DOS) as a library I was using made explicit calls to these functions rather than honouring the memory model employed and using the near model equivalents. In this case I wrote wrappers for the replacement far malloc and free functions that redirected calls to their near counterparts and linked them into my code before the C library.

I am not sure either of the two examples above count as 'callback' but would count as 'hooking'.

I suggest you employ some time checking out the Internet for such information - it took me only a couple of minutes with a search engine to locate Wikipedia articles on both Callback and Hooking for example:

   http://en.wikipedia.org/wiki/Hooking
   http://en.wikipedia.org/wiki/Callback_%28computer_science%29

If you do not understand far and near addresses for 16-bit x86 systems see for example the section on Segmentation at:

   http://en.wikipedia.org/wiki/X86

Hope this helps

Advertisement

©2025 eLuminary LLC. All rights reserved.