Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
DelphiRAD Studio

Quickly And Easily Hook Delphi And Windows API Functions With The DDetours Library


Hooking includes a range of methods for changing or expanding the behavior of an operating system, application, or other software parts by intercepting API function calls, messages, or events passed between software components. The code that controls such interception is called a hook.

DDetours is a library that allows you to insert and remove the hook from the function. It supports both (x86 and x64) architecture. The basic idea of this library is to replace the prologue of the target function by inserting unconditional jump instruction to the intercepted function.

Hooking rules

To run your hook correctly, you must follow the following rules:

  • Intercept procedure must have the same original procedure signature. That means the same parameters (types) and same calling convention.
  • When hooking Delphi Object/COM Object, the first parameter of the InterceptProc/Trampoline must be a pointer to that object, that’s what we call Self.
  • When doing multi-hooks, each hook needs its NextHook/Trampoline function.
  • When building a multi-thread application, inserting/removing hooks should be done inside the BeginTransaction/EndTransaction frame.
  • Never try to call the original function directly inside the Intercept function as this may result in a recursive call. Always use NextHook/Trampoline function.
  • Use EnterRecursiveSection/ExitRecursiveSection when calling a function that may call internally the original function.

Here is some sample source code from the example:

Check out and download the powerful DDetour library over on Github!

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

Software Developer | B.Tech-CSE at AUT | Delphi/C++ Builder Enthusiast | Microsoft Learn Student Ambassador | Microsoft Azure Certified | Udacity Alumni
Related posts

The Future of Startups Demands Massive Productivity

C++DelphiInterBaseRAD Studio

RAD Server CRUD Procedures - Part 3

C++CodeDatabaseDelphiRAD Studio

TCoffeeAndCode: Data Visualization – Components, Libraries, Tools and Services


This Native High-Accuracy Walking Profiler Can Measure Reality

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.