Calling EXE as a DLL
Posted: 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 :
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
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