Hooking

In programming, the term referred Hook (also called insertion method ) an interface that can be integrated into an existing application with the foreign program code to extend it, to modify their expiration or intercept certain events. This can happen either in the code that is modified accordingly, configuration files, which change the sequence of a complete program, or calling functions, where the executing program code will be given in any form. In general, the default behavior of insertion methods, nothing to do.

Hooks can be made available by the operating system to intercept messages. Thus, for example, system-wide hotkeys can be implemented. So the keyboard messages can be independent of the program, which currently has the focus, for example, received over a hook.

Implementation

There are several ways to implement hooks.

If the source code of the program to be expanded is present, can in principle always MAKE IMPROVEMENTS. However, if the code is not exactly known, this is complicated because this extension may require extensive changes, which can have undesirable side effects on top of that and also would then be retreated with each new version.

Therefore, some programs define specific locations that are explicitly intended to be extended by others. This can be done by calling a function that is implemented yet in the original program, so does nothing. This can then be diverted safely to a foreign implemented function. The initially empty feature is the hook.

In object-oriented programming this is done by inheritance or by delegation:

  • In the case of inheritance, the hooks are empty methods that can be ausimplementiert in subclasses. An example is the design pattern of the template method; here several methods are successively called as hooks to specialize algorithms Fixed sequence frames can.
  • In the case of delegation, a hook is passed in the form of an object as an attribute to the caller. This object must implement a specific interface. An example is the strategy design pattern dar. If it is expected that the hook will be used by multiple different service providers, the caller can implement the hook in a list instead of a simple attribute, so that multiple implementations of the hook in order to be called can. This happens for example in the case of a listener to intercept user interaction. If the list is only as far as processed until the first implemented Hook a specific return a result, you have a chain of responsibility.

Operating system hooks under Windows are implemented in a separate DLL which is called by the operating system without a program context. Thus must be a program that implements a hook (eg for notification of an event ), its results to learn more about other mechanisms of the code contained in the DLL ( eg by means of inter-process communication).

Configuration

There remains the question of how the calling program obtains knowledge of the parts of the program that want to use the hooks provided.

In the simplest case the code is directly modified in order to gain access to the hooks.

Or will it be integrated program code configurations. This can be done in procedural programming via function pointer by an invoked subroutine is entered with its start address. In object-oriented programming, however, a class is loaded dynamically using its name; this class implements then as a class or as a delegate object called by the hook code. Is Dependency Injection is used, can be here on also easily configure hooks.

A third possibility is to put an object or a function which should be called by the hook on an access function. This is then made ​​if the hook is provided by a toolkit that is used by the same program section, which will make use of the hooks. The installation is used for example when the listener for user interaction, which was already mentioned above.

Notes

299397
de