You are here:

C++/Hook vs Callback methods?

Advertisement


Question
Hello,

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

Thanks,
lzzzz

Answer
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

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2012 About.com, a part of The New York Times Company. All rights reserved.