Calling EXE as a DLL

just write whatever you want
Post Reply
obones
Posts: 66
Joined: Fri May 15, 2009 11:47 am

Calling EXE as a DLL

Post by obones »

Hi all,

I'm trying to call some code inside an exe from another exe via a LoadLibrary call, all of this in Delphi.
I've had a look at the following discussion on experts-exchange :

http://www.experts-exchange.com/Program ... 63413.html

and it seems it's quite possible to call _InitLib, provided one finds a way to pass the proper parameters.
Here is what I have written so far, in the loaded Exe:

type
TInitProc = procedure;

function GetInitLibAddr: TInitProc;
asm
mov eax, offset SysInit.@InitLib
add eax, $007A0000
end;

procedure Init; stdcall;
var
InitProc: TInitProc;
begin
InitProc := GetInitLibAddr();

asm
push 0 // Reserved
push DLL_PROCESS_ATTACH
push Hinst
push ebp
mov ebp, esp
mov eax, $004537E8
call InitProc
pop ebp
end;
end;

exports Init;

With this I can call the Init exported procedure from another exe which loads the one above via LoadLibrary.
However, it does not work as expected because it gives me an access violation. Value "$007A0000" is taken from the EE discussion mentioned above and the "$004537E8" value is taken from a disassembly that I made myself.

However, this does not work at all.
I know that I could create a true DLL called by both exes, but I'm still curious as to how I could achieve the above in Delphi. I know it can be done with a very basic C program, but then again, I want to use Delphi.

Any help greatly appreciated.
Cheers
Olivier
madshi
Site Admin
Posts: 10749
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Wow, that discussion on EE is 5 years old! I don't even remember it. After rereading some parts of it, it seems to me that I didn't really think this would work from the get go. And I still think it's a bad idea. So I'm sorry, but I won't invest any time on making this work.
obones
Posts: 66
Joined: Fri May 15, 2009 11:47 am

Post by obones »

No worries, it was more out of curiosity than anything else. I'll see if I can get it to work, but for my "production" I went another way, using two exes that call the same DLL.
Thanks anyway.
Post Reply