18.1 DLL Overview

Like a standard (object-code) library, a DLL contains procedures that one or more programs can call. Yet unlike standard-library procedures, DLL procedures are never copied into an application's executable file. They reside only on disk in the DLL file.

DLLs have several advantages:

Dynamic link libraries save significant space since the DLL's code and data exist in only one place, no matter how many different programs call the DLL. Applications that need a particular DLL can share it.

In contrast, a standard library routine (the printf function in C, for example) becomes part of the executable code for each application that uses it. For example, if three different programs use the statically linked printf function, three copies of the printf code are on disk. Furthermore, if all three programs run at once, the printf code occurs three times in memory. If the same function were part of a DLL, it would exist in only one location on disk and in memory.

Dynamic linking makes applications and libraries more independent, and therefore they are easier to maintain. You can update a DLL without having to relink any of the programs that use it.

Applications link faster because the executable code for a dynamic link function is not copied into the application's .EXE file. Instead, only an import definition is copied.

The purpose of a DLL is to supply (“export”) procedures and data to client programs at run time. Items not exported are visible only within the DLL.

Summary: Exported procedures are visible to the client program.

The concept of exporting is analogous to the action of the PUBLIC directive, but goes further. A public item is available only to other source modules within the same program or DLL. An exported item is available to all programs running on the system. In addition to global procedures and data, a DLL can contain other procedures and data definitions to support the operations of exported procedures.

Finally, a DLL can contain initialization and termination code to allocate and release resources needed by the procedures. Resources are typically files or dynamic memory. System services for OS/2 and Windows are provided through DLLs.