unit UHook;
interface
uses Forms, Dialogs, Classes, SysUtils, Windows;
type
TSaveRedir =
packed record
Addr: Pointer;
Bytes:
array[0..4]
of Byte;
end;
PSaveRedir = ^TSaveRedir;
implementation
procedure RedirectCall(FromAddr, ToAddr: Pointer; SaveRedir: PSaveRedir);
var
OldProtect: Cardinal;
NewCode:
packed record
JMP: Byte;
Distance: Integer;
end;
begin
if not VirtualProtect(FromAddr, 5, PAGE_EXECUTE_READWRITE, OldProtect)
then
RaiseLastOSError;
if Assigned(SaveRedir)
then
begin
SaveRedir^.Addr := FromAddr;
Move(FromAddr^, SaveRedir^.Bytes, 5);
end;
NewCode.JMP := $E9;
NewCode.Distance := PChar(ToAddr) - PChar(FromAddr) - 5;
Move(NewCode, FromAddr^, 5);
if not VirtualProtect(FromAddr, 5, OldProtect, OldProtect)
then
RaiseLastOSError;
end;
procedure UndoRedirectCall(
const SaveRedir: TSaveRedir);
var
OldProtect: Cardinal;
begin
if not VirtualProtect(SaveRedir.Addr, 5, PAGE_EXECUTE_READWRITE, OldProtect)
then
RaiseLastOSError;
Move(SaveRedir.Bytes, SaveRedir.Addr^, 5);
if not VirtualProtect(SaveRedir.Addr, 5, OldProtect, OldProtect)
then
RaiseLastOSError;
end;
procedure DoTest;
begin
ShowMessage('
Läuft');
end;
function MyDeleteFile1(
const FileName:
string): Boolean;
begin
DoTest
end;
function MyDeleteFile2(lpFileName: PAnsiChar): LongBool;
begin
DoTest
end;
function MyDeleteFileA(lpFileName: PAnsiChar): LongBool;
begin
DoTest
end;
function MyDeleteFileW(lpFileName: PWideChar): LongBool;
begin
DoTest
end;
var
S1,S2,S3, S4: TSaveRedir;
initialization
RedirectCall(@Sysutils.DeleteFile, @MyDeleteFile1, @S1);
RedirectCall(@Windows.DeleteFile, @MyDeleteFile2, @S2);
RedirectCall(@Windows.DeleteFileA, @MyDeleteFileA, @S3);
RedirectCall(@Windows.DeleteFileW, @MyDeleteFileW, @S4);
finalization
UndoRedirectCall(S1);
UndoRedirectCall(S2);
UndoRedirectCall(S3);
UndoRedirectCall(S4);
end.