Import Libraries and DLLs

Thus far, we have described two types of libraries: static-link libraries and DLLs. There is a third type of library that is important when working with DLLs: import libraries. An import library contains information which helps Windows locate code in a DLL.

During linking, the linker uses static-link libraries and import libraries to resolve references to external routines. When an application uses a routine from a static-link library, the linker copies the code for that routine into the application's .EXE file. However, when the application uses a routine from a DLL, the linker does not copy any code. Instead, it copies information from the import library which indicates where to find the desired code in the DLL at run time. During application execution, this relocation information creates a “dynamic link” between the executing application and the DLL.

Table 28.1 summarizes the uses of each of the three types of libraries.

Table 28.1 Uses of the Three Library Types

Library
Type
Linked at Link Time Linked at Run Time
Example Library
Example Routine

Static Yes No MLIBCEW.LIB strcpy
Import Yes No LIBW.LIB TextOut
Dynamic No Yes GDI.EXE TextOut

As this table indicates, when an application calls the strcpy function in the C run-time library, the linker links the application to the library by copying the code of the routine from the MLIBCEW.LIB run-time library into the application's .EXE file. But when the application calls the TextOut Windows GDI function, the linker copies location information for TextOut from the LIBW.LIB import library into the .EXE file. It does not copy the code of the function itself. Then, at run time, when the application makes the call to TextOut, Windows uses the location information in the .EXE file to locate TextOut in the dynamic-link library GDI.EXE. It then executes the actual TextOut function in GDI.EXE. In other words, import libraries provide the connection between application modules and DLL modules.