Calling EXE as a DLL

just write whatever you want

Calling EXE as a DLL

Postby obones » Fri May 15, 2009 12:11 pm

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 : ... 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:

TInitProc = procedure;

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

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

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

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.
Posts: 44
Joined: Fri May 15, 2009 11:47 am

Postby madshi » Wed May 20, 2009 7:03 am

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.
Site Admin
Posts: 9584
Joined: Sun Mar 21, 2004 5:25 pm

Postby obones » Wed May 20, 2009 10:14 am

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.
Posts: 44
Joined: Fri May 15, 2009 11:47 am

Return to fun talk

Who is online

Users browsing this forum: No registered users and 1 guest