Dynamic Linking and DLL (SPPU – 8 Marks)
Question: Describe the concept of DLL? How dynamic linking can be done with or without import. (8 Marks)
1) Definition of DLL (2M)
• DLL (Dynamic Link Library): A module containing functions, data, or resources that can be
shared by multiple programs at runtime.
• Unlike static libraries (linked at compile time), DLLs are linked dynamically when the program
executes.
• Purpose: Code reusability, modularity, reduced memory usage, and easier updates.
Example: kernel32.dll , user32.dll in Windows contain system-level APIs used by many programs.
2) Concept of Dynamic Linking (2M)
Dynamic linking is the process of linking external modules/libraries at run time instead of compile time.
The program contains only reference information (like DLL name and symbol table entries), and the
loader resolves these references when the program is loaded or executed.
• Static linking: Object code of library is copied into the executable.
• Dynamic linking: Only reference is stored; actual library code is loaded and linked at runtime.
Benefits: - Saves disk space and memory (common code shared). - Easy library upgrades (replace DLL, no
need to recompile program).
3) Dynamic Linking with Import (2M)
• Uses an Import Address Table (IAT).
• At load time, the OS loader checks the IAT to locate DLLs and resolve addresses of imported
functions.
• Functions from DLL are directly available to the program after loading.
• Example (Windows): #include <windows.h> and linking with .lib stub file ensures imports
are listed in the IAT.
Steps: 1. Compiler/Linker creates IAT entries for functions. 2. At runtime, loader replaces entries with actual
addresses from DLL. 3. Program calls functions through these resolved addresses.
1
4) Dynamic Linking without Import (Explicit Linking) (2M)
• The program loads the DLL manually at runtime using API calls.
• Example (Windows APIs):
• LoadLibrary("mylib.dll") → loads the DLL.
• GetProcAddress(handle, "functionName") → retrieves address of the required function.
• FreeLibrary(handle) → unloads DLL when no longer needed.
• This gives more control, since only required functions are loaded.
5) Key Differences (Import vs Without Import)
Aspect With Import (Implicit) Without Import (Explicit)
Loading Automatic by OS loader Manual using API calls
Setup Uses IAT created by linker Uses LoadLibrary , GetProcAddress
Ease of use Easier, direct calls Flexible but more complex
Control Less runtime control Full runtime control
6) Exam-Oriented Conclusion (1M)
DLLs enable modular and reusable code through dynamic linking. Dynamic linking may be done with
import (implicit linking via IAT) or without import (explicit linking via runtime API). This provides
flexibility, reduces memory/disk usage, and allows easy library updates.