var origSleep: Pointer;
procedure NewSleep(dwMilliseconds: Cardinal);
stdcall;
begin
MessageBox(0, '
Hallo aus der neuen Prozedur.', '
NewSleep', MB_ICONINFORMATION);
asm
jmp origSleep
end;
end;
function DetourHook(lpModule, lpRoutine: PChar; pNewAddr: Pointer):Pointer;
type
TDetourRec =
packed record
bJmpOpcode: Byte;
dwAddress: DWord;
end;
var
lpDetourCode: TDetourRec;
lpGatewayCode: TDetourRec;
pTargetAddr: Pointer;
pJmpGateway: Pointer;
dwTargetProtect: DWord;
const
DETOUR_JMP = $E9;
DETOUR_SIZE = $05;
begin
result :=
nil;
pTargetAddr := GetProcAddress(GetModuleHandle(lpModule), lpRoutine);
if pTargetAddr =
nil then exit;
pJmpGateway := VirtualAlloc(0, DETOUR_SIZE, MEM_COMMIT
or MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if pJmpGateway <>
nil then
begin
lpDetourCode.bJmpOpcode := DETOUR_JMP;
lpDetourCode.dwAddress := DWord(pNewAddr) - DWord(pJmpGateway) - DETOUR_SIZE;
CopyMemory(pJmpGateway, @lpDetourCode, DETOUR_SIZE);
lpGatewayCode.bJmpOpcode := DETOUR_JMP;
lpGatewayCode.dwAddress := DWord(pJmpGateway) - DWord(pTargetAddr) - DETOUR_SIZE;
if VirtualProtect(pTargetAddr, DETOUR_SIZE, PAGE_EXECUTE_READWRITE, dwTargetProtect)
then
begin
CopyMemory(pTargetAddr, @lpGatewayCode, DETOUR_SIZE);
result := Pointer(DWord(pTargetAddr) + DETOUR_SIZE);
end;
end;
end;
begin
origSleep := DetourHook('
kernel32.dll', '
Sleep', @NewSleep);
if origSleep =
nil then ExitProcess(0);
Sleep(2007);
MessageBox(0, '
I slept 2007 milliseconds', '
EntryPoint', MB_ICONINFORMATION);
end.