Friday, February 24, 2006

Load Dynamic Dll


function LoadAndRunDLLProcedure(sDLL,sFunc : string):boolean;
type
TFunc_Start = procedure;
var
Func_Start : TFunc_Start;
hDll : THandle;
FuncPtr : TFarProc;
sMsg : string;
begin
Result := False;
hDll := LoadLibrary(PChar(sDLL));
if(hDll > 32)then
begin
FuncPtr := GetProcAddress(hDll,PChar(sFunc));
@Func_Start := FuncPtr;
if(nil <> @Func_Start)then
begin
Func_Start;
Result := True;
end
else
begin
sMsg := 'DLL entry point ' + sFunc + ' not found';
MessageBox(0, PChar( sMsg ), 'Error',MB_OK );
end;
FreeLibrary( hDll );
end
else
begin
sMsg := 'File ' + sDLL + ' not found';
MessageBox(0, PChar( sMsg ), 'Error',MB_OK );
end;
end;

example :
LoadAndRunDLLProcedure('MyStuff.DLL','HelloWorld');