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

hook delphi and windows api functions with ddetours

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, which 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!

With the use of Windows IDE, you can quickly and easily hook Delphi and windows API functions with the ddetours library. Try your Free Trial here.

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 | CS(CyberSec) Undergrad at APU Malaysia | Delphi/C++ Builder Enthusiast | Microsoft Learn Student Ambassador | Microsoft Azure Certified

Leave a Reply

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