Using DLLs

Note: This is just a very basic introduction. If you need more details please refer to the Dephi help system. There is a good series of articles on it.

A DLL is - like an executable - a compiled program module. However, it cannot be run by itself but rather is a collection of functions that are called by outside programs. The abbriviation DLL stands for Dynamic Link Library. It is not compiled into the EXE-file at compile-time but linked into it at run-time.

Because DLLs are independend of the programming language you use (well, almost) there are a couple of things you should be aware of:

Importing functions - a simple example

There is a Windows DLL called USER32.DLL which includes the function MessageBox. To use this function in your program you have to tell the compiler to import it from the DLL:

function MessageBox(HWnd: Integer; Text, Caption: PChar;
 Flags: Integer): Integer; stdcall;
 external 'user32.dll' name 'MessageBoxA';

Open the file Win32.hlp in your Delphi\Help director and take a look at the description of the function. You will see how the parameters are matched by Delphi types. The word "stdcall" after the normal declaration of the function causes the compiler to use C calling conventions. "external" tells the compiler not to look for the function in a unit but rather the specified outside file (a DLL in this case). After external you pass the name of the DLL. Please note that it has to be in the current, \Windows, or \Windows\System directory. If the name you use in Pacal is different form the name of the function in the DLL you have to specify the real name of the function. Please be aware that C is case sensitive and that your function name in Pascal has to be in the correct case if you don't use the name directive.

Writing DLLs in Delphi

Writing your own DLLs in Delphi is very easy. There are only a few things you have to consider, which will probably become clear in this example:

library Test;

uses
  MyUnit;

exports
  MyFunction name 'MyFunction';

begin
end.

Of course this only works if MyFunction is declared in MyUnit.

back to top

Eighth DayFinal Words
Back to tutorial main page.


Please e-mail me with any comments!
© 27.12.96 Sebastian Boßung Home