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 6213853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:46:17+00:00 2026-05-24T06:46:17+00:00

I have a third party function function DataCompare(const S1, S2: string; APartial: Boolean): Boolean;

  • 0

I have a third party function

function DataCompare(const S1, S2: string; APartial: Boolean): Boolean;
begin
   ...
end;

It is used in another third party unit.

I wish to replace the body of the function at runtime with another new implementation.

Is this possible? I guess there will be a need of some hack (ala VirtualMemoryUnprotect). A non-assembler solution is very welcome.

  • 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-05-24T06:46:18+00:00Added an answer on May 24, 2026 at 6:46 am

    Yes you can do that, using the ReadProcessMemory and WriteProcessMemory functions to patch the code of the current process. Basically, you get the address of the procedure or function to patch and then insert a Jump instruction to the address of the new procedure.

    Check this code

    Uses
      uThirdParty; //this is the unit where the original DataCompare function is declarated
    
    type
      //strctures to hold the address and instructions to patch
      TJumpOfs = Integer;
      PPointer = ^Pointer;
    
      PXRedirCode = ^TXRedirCode;
      TXRedirCode = packed record
        Jump: Byte;
        Offset: TJumpOfs;
      end;
    
      PAbsoluteIndirectJmp = ^TAbsoluteIndirectJmp;
      TAbsoluteIndirectJmp = packed record
        OpCode: Word;
        Addr: PPointer;
      end;
    
    var
     DataCompareBackup: TXRedirCode; //Store the original address of the function to patch
    
    
    //this is the implementation of the new function
    function DataCompareHack(const S1, S2: string; APartial: Boolean): Boolean;
    begin
      //here write your own code
    end;
    
    //get the address of a procedure or method of a function 
    function GetActualAddr(Proc: Pointer): Pointer;
    begin
      if Proc <> nil then
      begin
        if (Win32Platform = VER_PLATFORM_WIN32_NT) and (PAbsoluteIndirectJmp(Proc).OpCode = $25FF) then
          Result := PAbsoluteIndirectJmp(Proc).Addr^
        else
          Result := Proc;
      end
      else
        Result := nil;
    end;
    
    //patch the original function or procedure
    procedure HookProc(Proc, Dest: Pointer; var BackupCode: TXRedirCode);
    var
      n: {$IFDEF VER230}NativeUInt{$ELSE}DWORD{$ENDIF};
      Code: TXRedirCode;
    begin
      Proc := GetActualAddr(Proc);
      Assert(Proc <> nil);
      //store the address of the original procedure to patch
      if ReadProcessMemory(GetCurrentProcess, Proc, @BackupCode, SizeOf(BackupCode), n) then
      begin
        Code.Jump := $E9;
        Code.Offset := PAnsiChar(Dest) - PAnsiChar(Proc) - SizeOf(Code);
        //replace the target procedure address  with the new one.
        WriteProcessMemory(GetCurrentProcess, Proc, @Code, SizeOf(Code), n);
      end;
    end;
    //restore the original address of the hooked function or procedure
    procedure UnhookProc(Proc: Pointer; var BackupCode: TXRedirCode);
    var
      n: {$IFDEF VER230}NativeUInt{$ELSE}Cardinal{$ENDIF};
    begin
      if (BackupCode.Jump <> 0) and (Proc <> nil) then
      begin
        Proc := GetActualAddr(Proc);
        Assert(Proc <> nil);
        WriteProcessMemory(GetCurrentProcess, Proc, @BackupCode, SizeOf(BackupCode), n);
        BackupCode.Jump := 0;
      end;
    end;
    
    //Patch the original procedure or function
    procedure HookDataCompare;
    begin
      //look how is passed the address of the original procedure (including the unit name)
      HookProc(@uThirdParty.DataCompare, @DataCompareHack, DataCompareBackup);
    end;
    
    //restore the address of the original procedure or function
    procedure UnHookDataCompare;
    begin
      UnhookProc(@uThirdParty.DataCompare, DataCompareBackup);
    end;
    
    
    initialization
     HookDataCompare;
    finalization
     UnHookDataCompare;
    end.
    

    Now every time you execute your app and a call to the DataCompare function was made, the jump instruction (to he new address) will be executed causing which the DataCompareHack function will be called instead.

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

Sidebar

Related Questions

I have some third-party Javascript that has statements like this: FOO = function() {
I have an issue with a third-party COM+ DLL meant to be used from
I have a third-party function which gives me a filtered queryset (e.g. records with
I have a third-party js function that dynamically adds/removes options to a SELECT element
I have wrote a wrapper through FFI for a shared library function(third party function).
I have a function in a third-party library written in C: char* fix_filename_slashes(char* path)
I have a third party library that internally constructs and uses the SqlConnection class.
I have a third-party app that creates HTML-based reports that I need to display.
I have a third party .NET Assembly and a large Java application. I need
I have an third-party applet that requires JRE v1.5_12 to work correctly. THe user

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.