This chapter describes how to create, manage, and use dynamic-link libraries with Win32.
Dynamic linking provides a way for an application to call a function that is not part of its executable code. The executable code for the function is located in a dynamic-link library (DLL), which contains a collection of subroutines that are compiled, linked, and stored separately from the applications that will use them.
Calling a function in a DLL is not different from calling any other function. The DLL is executed in the context of the thread that calls it. This means that the DLL has access to the open handles of the thread's process; and that the DLL uses the user stack of the calling thread and address space of the calling process. Memory allocated by the DLL is in the address space of the calling process; and globally declared variables in the DLL can be read and written by the calling process.
Dynamic-link libraries differ from the more familiar object modules and object libraries which also contain separately compiled subroutines. With both dynamic-link libraries and object modules, the compiled code can be used by more than one application. If an application needs to call a function from an object module or library, it must link with the module or library when the application's executable module (the .exe file) is created. This causes a copy of the code to be included in the .exe file of each application that links to it. To call a function in a dynamic link library, however, the application does not need to have its own copy of the code. Instead, the application's .exe file includes information that tells it where to look for the DLL. The application uses this information to locate the code at run-time.
Dynamic linking allows several applications to use simultaneously a single copy of an executable module. The executable module is completely separate from the applications that use it. Several functions can be built into a dynamic-link library (DLL), and applications can use these functions as if they were part of the application's executable code. You can change the dynamically linked functions without recompiling or relinking the application. This differs from statically linked object code, where you must relink the application to update it when the functions change.
The Win32 application programming interface (API) is implemented as a set of dynamic-link libraries, so any application that uses Win32 functions uses dynamic linking.