Let's say you have both procedure and function in your DLL you want to load from memory to use them:
** GLOBAL ** (both, exe -> dll)
type
TTest1Proc = record
Proc: procedure; StdCall;
hLib: THandle;
end;
TTest2Func = record
Func: function: Boolean; StdCall;
hLib: THandle;
end;
** DLL **
procedure Test1; StdCall;
begin
ShowMessage('Test proc');
end;
function Test2: Boolean; StdCall;
begin
Result := True;
end;
exports
Test1.
Test2;
This is how you could load dll and use both methods (procedure and function) in your .EXE project:
procedure Test1;
var
Test1Proc: TTest1Proc;
begin
with Test1Proc do
begin
hLib := LoadLibrary(PChar('DLL_PATH.dll'));
if hLib <> 0 then
begin
@Proc := GetProcAddress(hLib, 'Test1');
if Assigned(Proc) then
try
Proc; //will execute dll procedure Test1
finally
FreeLibrary(hLib);
end
else
ShowMessage('Procedure not found on DLL');
end
else
ShowMessage('DLL not found.');
end;
end;
And as function:
function Test2: Boolean;
var
Test2Func: TTest2Func;
begin
Result := False;
with Test2Func do
begin
hLib := LoadLibrary(PChar('DLL_PATH.dll'));
if hLib <> 0 then
begin
@Func := GetProcAddress(hLib, 'Test2');
if Assigned(Func) then
try
Result := Func; //will execute the function Test2
finally
FreeLibrary(hLib);
end
else
ShowMessage('Function not found on DLL.');
end
else
ShowMessage('DLL not found.');
end;
end;