20.1 What Is a Dynamic-Link Library?

A dynamic-link library is an executable module containing functions that Windows applications can call to perform useful tasks. Dynamic-link libraries exist primarily to provide services to application modules. These libraries play an important role in Windows, which uses them to make its functions and resources available to Windows applications. All Windows libraries are dynamic-link libraries.

Dynamic-link libraries are similar to run-time libraries, such as the C run-time libraries. The main difference is that dynamic-link libraries are linked with the application at run time, not when you link the application files by using Microsoft Segmented Executable Linker (LINK). Linking a library with an application at run time is called dynamic linking; linking the library with an application by using the linker is called static linking.

One way to understand dynamic-link libraries is to compare them to static-link libraries. An example of a static-link library is MLIBCEW.LIB, the medium-model Windows C run-time library. MLIBCEW.LIB contains the executable code for C run-time functions such as strcpy and strlen. You use C run-time functions in your application without having to include the source code for those functions. When you link your C application, the linker incorporates information from the appropriate static-link library. Wherever the application's code uses a C run-time function, the linker copies that function to the application's executable (.EXE) file.

The primary advantage of static-link libraries is that they make a standard set of functions available to applications, and do not require the applications to include the original source code for those functions. Static-link libraries, however, can be inefficient in a multitasking system such as Windows. If two applications are running simultaneously and they use the same static-library function, there will be two copies of that function present in the system. This is an inefficient use of memory. It would be more efficient for both applications to share a single copy of the function, but static-link libraries provide no facility for sharing code between applications.

With dynamic-link libraries, on the other hand, several applications can
share a single copy of a function. Every standard Windows function, such as
GetMessage, CreateWindow, or TextOut, is in one of three dynamic-link libraries: either KRNL286.EXE or KRNL386.EXE, USER.EXE, and GDI.EXE. If two Windows applications are running at the same time and both use a particular Windows function, both share a single copy of the source code for that function.

In addition to being able to share code, applications using dynamic-link libraries can share other resources, such as data and hardware. For example, Windows fonts are text-drawing data that applications can share by means of dynamic-link libraries. Likewise, Windows device drivers are dynamic-link libraries that applications can use to share hardware resources.