unit InjectCode;
interface
uses
Windows, ShellAPI, Dialogs;
procedure Inject(ProcessHandle: longword; EntryPoint: pointer);
procedure FindAndInject(WindowTitle: PChar; EntryPoint: pointer);
implementation
procedure Inject(ProcessHandle: longword; EntryPoint: pointer);
var
Module, NewModule: Pointer;
Size, BytesWritten, TID: longword;
begin
Module := Pointer(GetModuleHandle(
nil));
Size := PImageOptionalHeader(Pointer(integer(Module) + PImageDosHeader(Module)._lfanew + SizeOf(dword) +SizeOf(TImageFileHeader))).SizeOfImage;
VirtualFreeEx(ProcessHandle, Module, 0, MEM_RELEASE);
NewModule := VirtualAllocEx(ProcessHandle, Module, Size, MEM_COMMIT
or MEM_RESERVE, PAGE_EXECUTE_READWRITE);
WriteProcessMemory(ProcessHandle, NewModule, Module, Size, BytesWritten);
CreateRemoteThread(ProcessHandle,
nil, 0, EntryPoint, Module, 0, TID);
end;
procedure FindAndInject(WindowTitle: PChar; EntryPoint: pointer);
var
ProcessHandle, PID: longword;
Active : Integer;
begin
Active := FindWindow(
nil, WindowTitle);
if Active = 0
then
Exit;
GetWindowThreadProcessId(Active, @PID);
ProcessHandle := OpenProcess(PROCESS_ALL_ACCESS, False, PID);
Inject(ProcessHandle, EntryPoint);
CloseHandle(ProcessHandle);
end;
end.