InjectLibrary question

contains all delphi packages mentioned below
Post Reply
djsale
Posts: 21
Joined: Wed Dec 27, 2006 4:09 pm

InjectLibrary question

Post by djsale »

Hi folks, hi Madshi,

I use this code to inject my dll in one (already running) exe file:

Code: Select all

GetWindowThreadProcessID(FindWindow('WindowTitleXY',nil),ProcessID);
ProcessHandle:=OpenProcess(PROCESS_ALL_ACCESS,false,ProcessID); 
if injectLibrary(ProcessHandle,'mylib.dll') then ...
The question: Does injectLibrary check, if the dll is already injected (e.g. if I call it a 2nd time) or do I have take care of this? How can I check if the dll is injected in the process? I tried it with GetModuleHandle (which works fine on system wide injects), but it return always 0. Any ideas? The inject itself works without any problems.
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

madCodeHook does not check if the dll is already injected, but it doesn't hurt much to inject it twice. That's identical to call "LoadLibrary" twice. The DLL load counter will be increased, but that's all that will happen.
tiks
Posts: 10
Joined: Mon May 08, 2006 2:34 am

Post by tiks »

madshi, i got a problem with that, if i inject twice i got a crash in the program, i guess its because the duplicate ipc, theres some way to check if the dll is already injected or avoid the duplicate IPC?

Thanks!
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

You get a crash if you inject twice? That's extremely strange! There is no duplicate IPC if you inject twice. The double injection only has these effects:

(1) Your DLL (the one which is already injected) gets called with DllMain -> DLL_THREAD_ATTACH + DLL_THREAD_DETACH. But this also happens everytime the target process creates and stops a new secondary thread.

(2) The load counter of your DLL is increased by one.

I don't see why you should get a crash. Which program crashes? Your own one which calls InjectLibrary? Or the target process into which you're injecting?
tiks
Posts: 10
Joined: Mon May 08, 2006 2:34 am

Post by tiks »

the target process, i'm going to write in a ini file to avoid inject twice [:P]
Post Reply