AfxCodeHook Example - Inject Library

posted 17 Oct 2010, 15:57 by Delphi Basics
{
  Inject Library Demo
  by Aphex

  This program demonstrates how to inject a DLL into another process.
}

program InjLib;

uses
  Windows,

var
  Process: dword;
  StartInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
  Path: array [0..MAX_PATH] of char;

begin
  //create process to inject into
  ZeroMemory(@StartInfo, SizeOf(TStartupInfo));
  StartInfo.cb := SizeOf(TStartupInfo);
  CreateProcess(nil, 'notepad.exe', nil, nil, False, 0, nil, nil, StartInfo, ProcInfo);

  Sleep(100);

  //inject the dll into the process
  Process := ProcInfo.hProcess;
  InjectLibrary(Process, 'test.dll');

  Sleep(3000);

  //kill the process
  TerminateProcess(Process, 0);
end.

test.dll
library test;

uses
  Windows;

procedure EntryPoint(Reason: dword); stdcall;
begin
  if Reason = DLL_PROCESS_ATTACH then MessageBox(0, 'hi from notepad :)', 'afxCodeHook', 0);
end;

begin
  DLLProc := @EntryPoint;
  EntryPoint(DLL_PROCESS_ATTACH);
end.
Comments