Search code examples
delphiassemblycompilationdelphi-7vcl

How can I place code before SysInit._InitExe call?


I've put asm INT 3 end; in the very first line of my main procedure.

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  asm INT 3 end;

  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Yet, the SysInit._InitExe call is placed before it. Refer to the below image. Removing VCL units from uses clause is not an option.

enter image description here


Solution

  • There's no way to do what you need using the built-in tooling. You'll need to do some post compilation modification of the executable file.

    1. Include the code that you need to execute first in your source file so that it is compiled into the executable, but don't call it.
    2. Modify the entry point in the PE header to point to your new entry point code.
    3. Arrange that your new entry point code jumps to the original entry point when it is done.