Title: How to call a DLL dynamically
Question: With the function LoadLibrary you can also choose a dll at runtime - but also other things have to be done.
Answer:
If you want to choose a DLL at runtime you have to use LoadLibrary and GetProcAddress. Before you have to define the type of procedure you want to call.
Let's say the DLL looks like this:
library MathFunc;
function MyCalc(x,y: integer): integer; export;
begin
result:= x+y;
end;
exports
MyCalc;
begin
end.
In the main program you have to define the type of procedure:
type TMyFunc = function(x,y: integer): integer;
Calling the function works like this:
var MyFunc: TMyFunc;
FuncPtr: TFarProc;
DLLHandle: THandle;
begin
DLLHandle:= LoadLibrary(PChar(Edit1.Text));
FuncPtr:= GetProcAddress(DLLHandle, 'MyCalc');
if FuncPtr NIL then
begin
@MyFunc:= FuncPtr;
Edit2.Text:= IntToStr(MyFunc(2,3));
FuncPtr:= NIL;
end;
FreeLibrary(DLLHandle);
end;
Well, the effort is much higher then when working with static DLL binding.