Following "LoadAndRunDLLProcedure()" function will let you pass the name of the DLL you want to connect to and the name of the function you want to call. If everything goes well, it will load the DLL, call the function, and then unload the DLL.
function LoadAndRunDLLProcedure(
sDLL,
sFunc : string )
: boolean;
type
// define the type of "function"
// we're calling
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;
|
For example, let's say you want to call a procedure called "HelloWorld()" in a DLL named "MyStuff.DLL:"
LoadAndRunDLLProcedure( 'MyStuff.DLL', 'HelloWorld' ); |
Please note that HelloWorld() must be a procedure, for example, declared as:
procedure HelloWorld;
or in C:
void HelloWorld();