Lo prometido es deuda:
Código Delphi
[-]
function HookFunction(ModName, ProcName: PChar; Nuevo: Pointer): Pointer;
var
i: Integer;
hProcess: THandle;
hModules: array[0..1024] of HMODULE;
cbNeeded: DWORD;
hMod: HMODULE;
ImportDesc: PIMAGE_IMPORT_DESCRIPTOR;
Size: Cardinal;
szModName: PChar;
Thunk: PPointer;
MBI: MEMORY_BASIC_INFORMATION;
begin
Result:= nil;
hMod:= GetModuleHandle(ModName);
if hMod <> 0 then
begin
Result:= GetProcAddress(hMod, ProcName);
if Result <> nil then
begin
hProcess:= OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
FALSE, GetCurrentProcessId);
if hProcess <> 0 then
begin
if EnumProcessModules(hProcess, @hModules, Sizeof(hModules), cbNeeded) then
for i:= 0 to (cbNeeded div Sizeof(HMODULE)) - 1 do
begin
ImportDesc:= ImageDirectoryEntryToData(Pointer(hModules[i]),
TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, Size);
if ImportDesc <> nil then
begin
while ImportDesc.Name > 0 do
begin
szModName:= PChar(hModules[i] + ImportDesc.Name);
if StrIComp(szModName,ModName) = 0 then
begin
Thunk:= Pointer(hModules[i] + ImportDesc.FirstThunk);
while Thunk^ <> nil do
begin
if Thunk^ = Result then
begin
OutputDebugString(PChar(String(ProcName) + ': ' + 'Hookeado'));
VirtualQuery(Thunk,MBI,Sizeof(MEMORY_BASIC_INFORMATION));
VirtualProtect(MBI.BaseAddress,MBI.RegionSize,PAGE_READWRITE,
MBI.Protect);
Thunk^:= Nuevo;
VirtualProtect(mbi.BaseAddress,mbi.RegionSize,mbi.Protect,
MBI.Protect);
end;
inc(Thunk);
end;
end;
inc(ImportDesc);
end;
end;
end;
end;
end;
end;
end;
Espero que se entienda ...