BlueOrange
BlueOrange

Reputation: 21

Create a Delphi Dll and load it with DllMain

Friends

Im with a little problem. Im trying to create a delphi Dll with a form in RAD Studio, but i don't know how to make it load with DllMain. I want to inject this Dll in a third-party process at runtime after.

I created the Dll project with the form without problems, but i can't find nothing good related to "how to load it with DllMain", or at least the tutorials/things i found doesn't helped me (or i'm just dumb). Can someone help me? Give me some hint or a site/video where i can learn it?

I really appreciate your time guys! =)

Upvotes: 2

Views: 889

Answers (1)

Samuel Andrade
Samuel Andrade

Reputation: 154

You could use assembly to inject the ebp-based stack into some variables. Here is an example:

library Project1;

uses
  System.SysUtils,
  Windows,
  System.Classes;

var
  hInstDLL: THandle;
  fdwReason: DWORD;
  lpReserved: DWORD;
begin
  asm
    push eax; // Save the current eax
    mov eax, [ebp+$8] // Put into eax the first argument of the current function (DLLMain)
    mov [hInstDLL], eax; // Put into hInstDLL this argument
    mov eax, [ebp+$c] // Load into eax the second argument
    mov [fdwReason], eax; // Save to fdwReason
    mov eax, [ebp+$10] // Put into eax the last argument
    mov [lpReserved], eax; // Put into lpReserved (unnecessery)
    pop eax; // Restore the original eax value
  end;

  if fdwReason = 1 {DLL_PROCESS_ATTACH} then
  begin
    // Do your stuff;
  end;
end.

Upvotes: 2

Related Questions