Delphi
--
Questions
--
Followers
Top Experts
if Address = OldAddress then
begin
EnterCriticalSection(CritS
if VirtualProtect(Address, sizeof(Address),PAGE_EXECU
try
WriteProcessMemory(GetCurr
finally
VirtualProtect(Address, sizeof(Address),dwOldProte
LeaveCriticalSection(CritS
end;
end;
Inc(ImportCode);
end;
Inc(ImageImportEntry);
end;
end;
Is there something I can test to make sure its writing correctly to right address?
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
(1) For what purpose is that critical section? I don't see much sense in that.
(2) If VirtualProtect fails, the critical section will never be unlocked again, which will probably sooner or later deadlock your program.
(3) Why using WriteProcessMemory? Why not directly writing to the memory? You want to overwrite memory in your own process, don't you? WriteProcessMemory is for writing into other processes. Sure, you can use it for your own process, too, but what is the sense of it?
You say "writing to the IAT seems to be a problem". What problem? Do you get access violations? Or does the hook simply not work? Or what? Could you please be a bit more specific?
--------------------------
Anyway, if you just want a quick and easy solution, have a look at my Delphi package "madCodeHook". It's free for non-commercial usage and works equally fine in win9x and winNT. Here's the latest beta version:
http://madshi.net/madCollectionBeta.exe
Some demos are included.
The hook simply does not work. It finds all the imported modules even seems to be writing something because Ive done
byteswritten :=0
VirtualProtect(Address, sizeof(Address),PAGE_EXECU
WriteProcessMemory(GetCurr
if byteswritten > 0 then
messagebox(0,'written','te
(and i get the msg box.)
I have not seen another api for writing to the IAT accept CopyMemory(). I have tried that too but the params are wrong because it crashes.
CopyMemory(Address, NewAddress,5); ???
Yes Ive used your collection and it works perfectly but its just a little bulky and I need something smaller and simple.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
to find out why it wasnt loading my function. Took it out and no runtime but function stil not loaded. I put a msg box in myfindnextfileW to tell me if it gets loaded. But i never got the msg box. I think its failing in same area...
while p1^ <> nil do begin
if (p1^ = old) and VirtualProtect(p1, 4, PAGE_EXECUTE_READWRITE, @c1) then
p1^ := new;
Without knowing more details about which APIs you want to hook for what purpose, I can't help any further.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
I don't know any details about what you're doing and what exactly fails.
How exactly did you find out that it does not work? You hooked the API in your own process? Or system wide? And executed the API in your own process? Or in another process?
Give me some more infos, currently I have not the slightest idea how I can help you, since I have no idea what goes wrong. Neither do I know what exactly you are doing (process wide hooking or system wide hooking etc).






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
"Now Aphex made a hooking unit(afxCodeHook) in delphi but sadly it only works on NT systems" - meaning it works on NT.
"I can inject the library fine in win9x using elicz elirt library. But actually writing to the import address table seems to be a problem...Its just some things ive have been trying to make work on 9x. "- meaning it doesnt work on windows 98, and ME.
"I put a msg box in myfindnextfileW(with your code) to tell me if it gets loaded. But i never got the msg box. I think its failing in same area..." Example:
function FindNextFileWHookProc(hFin
begin
MessageBox(0, 'my function ran','test',0);
Result := FindNextFileWNextHook(hFin
end;
So if the function is loaded im gonna get the msg box. But i dont on either os (with your code).
Now with afxCodeHook this is how you implement:
HookCode(@FindNextFileW, @FindNextFileWHookProc, @FindNextFileWNextHook);(w
With yours I did :
kmodule:=GetModuleHandle(n
PatchImportTable(kmodule,@
Okay, the reason for the failure is very probably that you missed what I said here:
"For successful IAT patching you need to patch each and every module in your process. However, in win9x you're not allowed to patch system modules (modules whose handle is bigger than $80000000)"
Patching just the main module (GetModuleHandle(nil)) isn't enough. You have to loop through all modules and patch each and every one (except the system modules in win9x).
Aphex code must do that internally, too, if it works correctly. So I guess you can use his dll enumeration code together with my patching function.

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
madCodeHook uses a completely different approach.
Ok well im going to try and combine your code and aphexs to see if it works. Have you seen his unit before? Maybe i should have told you to just take a look at it before. I believe everything is done correctly until writeprocessmemory().
His code looks more or less alright. The WriteProcessMemory call is a bit strange, but it should work nevertheless.
There are 2 possible reasons why his unit fails on 9x. Either you need to hook dynamically loaded dlls, too. Or import table patching is just not good enough in this specific situation.
Try this:
type
TDAModule = array of cardinal;
// returns all modules of the current process
function GetModuleList : TDAModule;
var p1, p2 : pointer;
mbi : TMemoryBasicInformation;
arrCh : array [0..MAX_PATH] of char;
i1 : integer;
begin
SetLength(result, 10);
i1 := 0;
p1 := nil;
p2 := nil;
while VirtualQueryEx(GetCurrentP
if (mbi.State = MEM_COMMIT) and
(mbi.AllocationBase <> p2) and (mbi.AllocationBase = mbi.BaseAddress) and
(GetModuleFileName(dword(m
if i1 = Length(result) then
SetLength(result, i1 * 2);
result[i1] := dword(mbi.AllocationBase);
inc(i1);
end;
p2 := mbi.AllocationBase;
dword(p1) := dword(p1) + mbi.RegionSize;
end;
SetLength(result, i1);
end;
procedure PatchImportTables(old, new: pointer);
var i1 : integer;
modules : TDAModule;
begin
modules := GetModuleList;
for i1 := 0 to high(modules) do
if (GetVersion and $80000000 = 0) or (modules[i1] < $80000000) then
PatchImportTable(modules[i
end;
Then call PatchImportTables instead of PatchImportTable. This will hook all modules of the current process (excluding the system modules in 9x).
P.S: Please instead of "@FindNextFileW" use "GetProcAddress(kmodule, 'FindNextFileW')". Furthermore in win9x please run your exe *outside* of the IDE. In XP you can run it inside or outside.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
If I ever release any of this code so others can use it or learn from it I will be sure to give you credits.
Delphi
--
Questions
--
Followers
Top Experts
Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Development™. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.