Scalabium Software

SMExport/SMImport suites
Knowledge for your independence'.
Home Delphi and C++Builder tips


#130: How can I use a procedure from dll in the Delphi?

Today I want to describe a method of dll-using. Of course, for some advanced users this tip is not useful but I hope that somebody will find it as interested.

In Delphi you have two mechanism of dll-loading: static and dynamic.

- the static method is when your application will be failed when dll is not present. Inside of application you'll define a function as external and in any code you'll use a function from dll as usual "native" function.

- the dynamic method allow to have a control under dll-load. For example, you can check an dll-existing and show a warning if dll is not exist (or try to load another dll, for example). Also to call a some function from such
> loaded dll, is more difficult in coding.

Small example.
Imagine that you have a dll with ExecScript procedure which have two parameters (UserID and ScriptName).

In static method you must declare a function as:

procedure ExecScript(UserID: Integer; ScriptName: PChar); stdcall; far; external 'yourDLLName.DLL';

and in the code you can call it as:
ExecScript(5, 'LogToSystem');

and it's all.

For dynamic mechanism you must:

 {define a procedure type with required parameters of your procedure in the DLL}
 type
   TDLLFunc = procedure(param1: Integer; param2: PChar);
 
 {assign a nil - not loaded function}
 const
   DLLFunc: TDLLFunc = nil;
 {handle of loaded dll}
 var
  DLLHandle: THandle;
 
 { load a library }
 DLLHandle := LoadLibrary(DLLName);
 
 if (DLLHandle < HINSTANCE_ERROR) then
     raise Exception.Create(DLLName + ' library can not be loaded or not found. ' + SysErrorMessage(GetLastError));
 
   try
     { load an address of required procedure}
     @DLLFunc := GetProcAddress(DLLHandle, 'ExecScript');
 
     {if procedure is found in the dll}
     if Assigned(DLLFunc) then
       DLLFunc(5, 'LogToSystem');
   finally
     {unload a library}
     FreeLibrary(DLLHandle);
   end;


Published: November 2, 2001

See also
 
Paradox Viewer
Mail parser (ActiveX)
SMImport suite
Clarion to Text converter/a>
SMMsg suite
SMReport
DBLoad
ABA Database Convert
Viewer for TNEF-files (winmail.dat)
Paradox to MS Access converter
 
 


Contact to webmaster

 

Borland Software Code Gear Scalabium Delphi tips

Copyright© 1998-2017, Scalabium Software. All rights reserved.
webmaster@scalabium.com

SMExport/SMImport suites