Okay, continuing question http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21142297.html
here. I have two executables:
type TSum = function( A, B: Integer ): Integer;
A, B: Integer;
Handle := LoadLibrary( Pchar( 'DLL.exe' ) );
if ( Handle > 0 ) then begin
Sum := TSum( GetProcAddress( Handle, 'Sum' ) );
if Assigned( Sum ) then begin
A := Random( 10 ) + 1;
B := Random( 10 ) + 1;
WriteLn( A, ' + ', B, ' = ', Sum( A, B ) );
WriteLn( 'Failed to find the sum routine.' );
CloseHandle( Handle );
WriteLn( 'Failed to load DLL.' );
Above executable is loading another executable, thus "abusing" it as if it's a DLL. Now the code for this other executable.
uses Windows, untInit in 'untInit.pas';
function Sum( A, B: Integer ): Integer;
Result := A + B;
MessageBox( GetDesktopWindow, 'Here we go', 'DLL', MB_OK );
Oh, it uses an unit. Here's the code for the unit:
MessageBox( GetDesktopWindow, 'Hello', 'DLL', MB_OK );
MessageBox( GetDesktopWindow, 'Goodbye', 'DLL', MB_OK );
Okay. DLL.EXE will run perfectly as a stand-alone executable. And EXE.EXE will load DLL.EXE and call the exported method too. However, there are a few flaws. (Otherwise I would not be asking this.)
The problem is simple. When I load the executable as a DLL, it doesn't initialize anything. None of the messageboxes will show, thus nothing within the DLL is initialized. Not even the global system variables. And this makes it quite difficult for me to use this technique for what I've planned.
DLL.EXE will be a simple console application that analyzes a bunch of data and then return a summary. Since it's a console application, it will just display this summary to the console before it closes again. My second application wants to use the same summary information but while I could let it grab the summary from the console, I want a more direct connection. I could use inter-process communication techniques and it would not be too difficult for me, except that I think it just complicates things too much. I could also put the summary calculator in a separate DLL but it just means that I need to create a third project.
I'm not in a hurry with this, though. This is just an experiment. As long as I don't have a working solution, I just work with the DLL option, having a console and a Forms application load this DLL, execute the method and then free the DLL again. I don't want any other alternative solutions either. All I want is to load one executable in the process space of another executable, have it initialized correctly so it operates as a normal Delphi DLL and get it all to work nicely.
And for those who wonder how I will prevent the DLL to execute like an executable when I load it as a DLL, well, simple. That's what you use mutexes for. Set a mutex in the executable and if the DLL tries to set it too, it notices it is already in use, thus it knows it should behave like a DLL. That is, if it ever gets initialized...
It's not urgent but still extremely difficult and educational. Answers like "It cannot be done" will not be rewarded.