DEV Community

Cover image for Dynamic Link Library Basics
Evan Loria
Evan Loria

Posted on

Dynamic Link Library Basics

What are Dynamic Link Libraries

Dynamic Link Libraries (DLLs) were created by Microsoft, and are usually written using C++. A dynamic link library is a type of file that allows resources, such as functions and other types of data, to be shared across multiple applications. They offer a number of benefits to Microsoft’s programs, but they can also induce errors within programs.

What They Do

DLLs offer multiple benefits for Microsoft’s applications to run. DLLs reduce the size of files, which in turn improves the performance of the program. Because several programs are able to use a DLL at the same time, the same block of code is able to be used by multiple programs.

In addition, the DLL code is able to be updated independently from the other programs, and the programs will have access to the updated code. This is much easier than having to update code in each individual program.

However, there are dangers to be aware of when using DLLs across multiple programs. Updates can cause a program's code to break due to compatibility issues. DLLs can also leave systems vulnerable to malware attacks.

Static Library

'Static libraries refer to a set of routines, external functions, and variables compiled and linked into an executable file at compile time.' (Vijay Kanade, What Is a Dynamic Link Library (DLL)? Meaning, Types, and Advantages) These types of libraries cause files to become larger because it appears as if the library code is part of the program itself. These libraries are more beneficial when creating a smaller library, and they are able to cut down the time is takes to execute a function.

Static vs Dynamic

Dynamic Library Differences

Unlike static libraries, dynamic libraries are only loaded to memory at the execution of a function within the library. The DLL is referenced within the memory of a computer when a program is started. Once the program requests the use of a function, then the library is loaded into memory. This library is better served when creating larger files and programs.

Steps For DLL

  1. When a program needs a function: The program searches for the DLL in memory. If it is not found, then the library is loaded into memory, and the resources added to the memory space of the program.
  2. Link to DLL: A function is called to link the DLL to the program. A dynamic link method is then used to link the library and the program. A dynamic link being used means that only a portion of the DLL file will be loaded into the program memory. This reduces file size, but increases the time it takes to run the program.
  3. Once the first two steps are completed the program has access to all the resources within the DLL.
  4. The DLL is offloaded from the computer's memory once the program has finished using it.

Advantages

Code Reuse: Dynamic libraries enable programs to reuse code. This eliminates duplicate code, enabling programs with matching functionality to use the same block of code.
Better Performance: Because DLL's control the size of files, and only load the DLL into memory when needed, the program can better execute due to the reduced loading time.
Encourages Modularity: Modularity is beneficial to creating programs because it allows component's to focus on smaller tasks. DLLs encourage modularity by allowing each component full access to the resources within.

Conclusion

DLLs offer many benefits to programs and operating systems in general. These libraries play a huge role in the overall performance of a computer’s programs. Although the use of DLLs is not a perfect way to enhance a program's efficiency, by limiting file size, encouraging modularity, and eliminating duplicate code DLLs allow for more efficient program execution.

Sources

Learn Microsoft
Medium
Spice Works

Top comments (0)