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}
   TDLLFunc = procedure(param1: Integer; param2: PChar);
 {assign a nil - not loaded function}
   DLLFunc: TDLLFunc = nil;
 {handle of loaded dll}
  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));
     { load an address of required procedure}
     @DLLFunc := GetProcAddress(DLLHandle, 'ExecScript');
     {if procedure is found in the dll}
     if Assigned(DLLFunc) then
       DLLFunc(5, 'LogToSystem');
     {unload a library}

Published: November 2, 2001

See also
Mail parser (ActiveX)
Word Web-stream
Clarion to Text converter
Fast Document Viewer
Paradox Password Recovery
Metafile Convert
ABA Document Convert
Clarion Viewer

Contact to webmaster


Borland Software Code Gear Scalabium Delphi tips

Copyright© 1998-2024, Scalabium Software. All rights reserved.

SMExport/SMImport suites