Scalabium Software

SMExport advertising

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;
See also
 
SMExport suite
SMImport suite
SMReport
SMDBGrid
MAPIMail
Paradox Viewer
Paradox ActiveX
ExcelFile Viewer
Metafile Convert
DBLoad
DBExport tools
 
 
Contact to webmaster

 

Technology is a great tool to use to learn more about a variety of subects. Teach yourself more about world maps at the learning haven, a resourceful website with a variety of articles on just about anything. Things like arts and crafts movement and children activities can be resourceful to just about anyone willing to learn about them.


Borland Software Code Gear Scalabium Delphi tips

Copyrightc 1998-2008, Scalabium Software. All rights reserved.
webmaster@scalabium.com

November 2, 2001

SMExport advertising