Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8967009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:09:01+00:00 2026-06-15T17:09:01+00:00

I am trying to hook a function within a application compiled with microsoft visual

  • 0

I am trying to hook a function within a application compiled with microsoft visual studio 2010, with __fastcall enabled from a delphi 2010 dll, but I am not to skilled to figure out how to detour the following problem:

The C++ function is:

     void __fastcall function(int arg1; char* arg2);

I was trying something like that (using uallHook):

    var FTextMessage : procedure(Modo: integer; Msg: pchar); register;
    procedure onTextMessage(Modo: integer; Msg: pchar); register;
    begin
      ShowMessage(inttostr(Modo) + ' - ' + string(Msg));
      FTextMessage(Modo, Msg);
    end;
    begin
    HookCode(Ptr($574210), @onTextMessage, @FTextMessage);
    end.

This is leading to debugs/crashes.

So, I found that:

“Microsoft or GCC[4] __fastcall[5] convention (aka __msfastcall) passes the first two arguments (evaluated left to right) that fit into ECX and EDX. Remaining arguments are pushed onto the stack from right to left.”

Borland fastcall
Evaluating arguments from left to right, it passes three arguments via EAX, EDX, ECX. Remaining arguments are pushed onto the stack, also left to right.[6]
It is the default calling convention of the 32 bit compiler of Embarcadero Delphi, where >it is known as register.

Taken from:
http://en.wikipedia.org/wiki/X86_calling_conventions

This is the problem, borland fastcall and microsoft __fastcall are different.

So I’m thinking that I will need to use a piece of assembly code inside the Hook function to align registers or something, but I’m not able to figure this yet.

Any help would be appreciated.

Edit1:
David Heffernan Answer works partially. (it only works until showmessage)

     var FTextMessage : procedure(Modo: integer;Msg: PAnsiChar); register;

     procedure onTextMessage(Modo: integer;Msg: PAnsiChar); register;
     begin
       ShowMessage(inttostr(Modo) + ' - ' + string(Msg));
       asm
         MOV ECX,Modo
         MOV EDX,Msg
         JMP FTextMessage
       end;  // I got a crash trying or without trying to restore the registers before executing the JMP FTextMessage
     //  FTextMessage(Modo,Msg); // < Also this method doesnt work either, if I call the Original function from here (without executing asm code above) i got a crash too. (this is what i was meaning with "to correct the registers")
     end;

     procedure onTextMessageWrapper(Modo: Integer;Msg: PAnsiChar); register;
     asm
       MOV EDX,ECX  // < Moving ECX to EDX, I'm able to Access Modo and Msg correctly in onTextMessage procedure.
       JMP onTextMessage
     end;

     begin
       HookCode(Ptr($574210), @onTextMessageWrapper, @FTextMessage);
     end.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:09:02+00:00Added an answer on June 15, 2026 at 5:09 pm

    The C++ function is passing parameters in ECX and EDX. So you just need to copy them to the registers that Delphi is expecting: EAX and EDX respectively. Since the second parameter is already in the right register all you need is this:

    procedure onTextMessage(Modo: Integer; Msg: PAnsiChar); register;
    begin
      ...
    end;
    
    procedure onTextMessageWrapper(Modo: Integer; Msg: PAnsiChar); register;
    asm
      MOV EAX,ECX
      JMP onTextMessage
    end;
    

    I’ve done this in two functions so that I can write a pure asm only function that does the register swapping. We want pure asm to avoid all preamble. So, onTextMessageWrapper is your hook function.

    Note also that since you are using a Unicode Delphi, you will want PAnsiChar to match up with char*.

    The best reference I know for calling conventions is by Agner Fog. You’ll find all the gory details there. One important point is that, on Windows x86, the EAX, ECX and EDX registers are volatile, or scratch registers. Which means that callees can modify those registers without having to restore them. Which is why I modify EAX above and do not bother to put it back.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a Delphi XE application I am trying to set up a global hook
I am trying to hook a function to cmd.exe process the dll is injected
Trying to hook into the function comment_text() supplied by Wordpress API to wrap the
I'm trying to hook the SetCooperativeLevel function in the DirectDraw object. My goal is
I am trying to retrieve messages for another application with a Windows hook. I
Trying to find a way to execute a function within BASH after changing into
I'm trying to hook function callback when iScroll container reaches end of page, at
i am trying to hook CopyItems function ,its working fine but when i am
I'm trying to add a function I created to a hook, but the obvious
I'm trying to hook the sys_execve() function on the Linux 3.x kernel by modifying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.