Windows Dynamic Link Libraries Overview

The term dynamic link libraries (DLL) refers to the general notion of a library which is not part of any program image, but rather is loaded as a separate entity when a program is run. Typical usage scenarios for DLLs include:

Both scenarios eliminate the need for duplicate code and reduce the total system memory required for the task in question. Static libraries may also eliminate the need for duplicate code by compiling one version of source code into an object file which gets linked into multiple program images.

There are two methods for accessing routines in a DLL. These are implicit loading and explicit loading. In the former case, an executable is linked with a .LIB file which contains sufficient information to allow the operating system to automatically ensure the necessary DLLs are loaded, and resolve references to functions provided by the DLL, before the executable begins running.

In the second case, the application developer includes explicit calls inside the program to LoadLibrary, GetProcAddress and FreeLibrary, which, respectively, ensure a DLL is loaded into memory, resolve the address of functions provided by the DLL, and indicate to Windows the DLL is no longer needed by a particular thread.

In Windows methods, the DLL is simply a code container and is not an independent program. There are no processes or threads associated with a DLL itself. Code within a DLL always runs in the context of a calling thread/process. The entry point in a DLL (DllMain) is called whenever a new thread/process attaches to a DLL, and in most cases when a process detaches from a DLL. When processes terminate abnormally DllMain is not guaranteed to be called. For more information on the exact semantics of DLL attach and detach please refer to the MSDN documentation.

An introduction to Windows DLLs can be found on Windows MSDN (www.msdn.com). It is strongly recommended developers familiarize themselves with the concepts of Windows DLLs.

Related topics: