Jump to content
xisto Community
Sign in to follow this  
imbibe

Demystifying Dlls What is?

Recommended Posts

What is DLL?

A dynamic link library is an executable file that acts as a shared library of functions. Dynamic linking provides a way for a process to call a function that is not part of its executable code. The executable code for the function is located in a DLL, which contains one or more functions that are compiled, linked, and stored separately from the processes that use them. DLLs also facilitate the sharing of data and resources. Multiple applications can simultaneously access the contents of a single copy of a DLL in memory.

Dynamic linking differs from static linking in that it allows an executable module (either a .dll or .exe file) to include only the information needed at run time to locate the executable code for a DLL function. In static linking, the linker gets all of the referenced functions from the static link library and places it with the code in the executable.

Even though DLLs and applications are both executable program modules, they differ in several ways. To the end user, the most obvious difference is that DLLs are not programs that can be directly executed. From the system's point of view, there are two fundamental differences between applications and DLLs.

An application can have multiple instances of itself running in the system simultaneously, whereas a DLL can have only one instance.

An application can own things such as a stack, global memory, file handles, and a message queue, but a DLL cannot.

What is Dynamic Linking ?

A Windows program is an executable file that generally creates one or more windows and uses a message loop to receive user input. Dynamic link libraries are generally do not receive messages. A dynamic link library is brought into action only when another module calls one of the functions in the library.

DLL contains three words with specific meanings:

Library - In general library means a depository built to contain books and other materials for reading and study. In computing library is a collection of subroutines and functions stored in one or more files, usually in compiled form, for linking with other programs. Libraries are one of the earliest forms of organized code reuse. They are often supplied by the operating system or software development environment developer to be used in many different programs. The routines in a library may be general purpose or designed for some specific function such as three dimensional animated graphics.

Link - A compiler generally converts the source code into the object code. Object files contain compact, pre-parsed code, often called binaries or machine code. The job of the linker is inclusion of one or more functions from pre compiled software libraries to the new program and produce the final executable. This process is termed as linking. The functions from pre compiled libraries can be embedded into the final program executable at compile time by the linker. This is termed to as static linking.

Dynamic - In Dynamic linking the functions from pre compiled libraries are not embedded into the final program executable at compile time by the linker. The library is loaded by the operating system's loader separately from the executable file at runtime. The executables contain a table called an import directory which is a variable length array of imports. Each element in the array contains a name of a library. The loader searches the hard disk for the needed library, loads it into memory at an unpredictable location and updates the executable with the library's location in memory. The executable then uses this information to call functions and access data stored in the library. This type of dynamic linking is used by most operating systems including Windows and Linux.

 

Using dynamic linking instead of static linking offers several advantages. DLLs save memory, reduce swapping, save disk space, upgrade easier, provide after-market support, provide a mechanism to extend Windows, support multi language programs, and ease the creation of international versions.

One of the largest disadvantages of dynamic linking is that the executables depend on the separately stored libraries in order to function properly. If the library is deleted, moved, renamed or replaced with an incompatible version, the executable could malfunction. The other disadvantage is the decreased efficiency because of the extra alogorithms of dynamic linking.

 

Types of Dynamic Linking

There are two ways in which Dynamic Linking can be done.

Implicit - occurs when an application's code calls an exported DLL function. When the source code for the calling executable is compiled or assembled, the DLL function call generates an external function reference in the object code. To resolve this external reference, the application must link with the import library (.LIB file) provided by the maker of the DLL. The import library only contains code to load the DLL and to implement calls to functions in the DLL. Finding an external function in an import library informs the linker that the code for that function is in a DLL. To resolve external references to DLLs, the linker simply adds information to the executable file that tells the system where to find the DLL code when the process starts up. If it cannot locate the DLL, the system terminates the process and displays a dialog box that reports the error. If the entry point function does not return True, the system terminates the process and reports the error.

Explicit - occurs when programs load and unload DLLs explicitly. Reasons to use explicit linking. The application does not know the name of a DLL that it will have to load until run time. The application might need to obtain the name of the DLL and the exported functions from a configuration file.

A process using implicit linking is terminated by the operating system if the DLL is not found at process startup. A process using explicit linking is not terminated in this situation and can attempt to recover from the error.

A process using implicit linking is also terminated if any of the DLLs it is linked to have a entry point function that fails. A process using explicit linking is not terminated in this situation.

An application that implicitly links to many DLLs can be slow to start because Windows loads all of the DLLs when the application loads. To improve startup performance, an application can implicitly link to those DLLs needed immediately after loading and wait to explicitly link to the other DLLs when they are needed.

Explicit linking eliminates the need to link the application with an import library.

FAQ

 

1. What is a DLL ?

DLL is an acronym for Dynamic Link Library. It contains functions that are compiled, linked, and stored separately from the processes that use them.

 

2. What is the extension for DLLs in Windows ?

.dll is the extension for Dynamic Link Libraries in Windows. It could also be .exe.

 

3. How is Dynamic Linking different from Static Linking ?

In Dynamic linking the functions from pre compiled libraries are not embedded into the final program executable at compile time by the linker.

 

4. What is the biggest advantage of DLLs ?

DLLs save memory, disk space and reduces swapping.

 

5. What is the biggest problem with DLLs ?

After installing a new application, one or more existing programs quit working. This generally points to DLL Hell.

 

6. Is Dynamic Linking the job of the programmer ?

Most of the Algorithms for Dynamic Linking are implemented by the Operating System or the compiler but there are certain points to be observed by the programmer for optimum efficiency and robustness.

 

7. Which Language is to be used for coding DLLs ?

Any Language that supports Win32 or Win16 programming can be used to code DLLs for the various versions of Windows. e.g. Visual Basic, Visual C++.

 

8. Can data in my DLL be shared with an application or with other DLLs?

Though Win32 DLLs are mapped into the address space of the calling process and by default, each process using a DLL has its own instance of all the DLLs global and static variables, but if a DLL needs to share data with other instances of it loaded by other applications, it could use memory mapped files.

 

9. What is the Entry Point function?

The DllMain function is an optional entry point into a dynamic-link library (DLL). If the function is used, it is called by the system when processes and threads are initialized and terminated.

 

10. Can a multithreaded application access a DLL in different threads?

An application can access DLLs from multiple threads created in the application.

 

11. What are the Steps to load a DLL?

Locate the DLL executable file on disk.

Traverse the list of DLLs loaded into the application's address space to determine if the DLL is already loaded.

Allocate the memory for the DLL to reside in and map the DLL binary into that memory.

Perform various manipulations in order for the DLL to work (that is, resolve fix ups in the DLL, and so forth).

12. What exactly is a fix up?

When a DLL is created, a base address is specified by the linker suggesting where Windows should load it in a process's 32-bit address space. Consider a shared DLL (call it abc.dll) that specifies 0x20000000 for the requested base address. Application a.exe is currently running with abc.dll loaded at 0x20000000. Application b.exe also uses the same abc.dll but has already loaded def.dll at 0x20000000. The OS must then change the base address (referred to as rebasing) of abc.dll to a unique address in process b. Because process b has rebased abc.dll, the OS cannot share abc.dll with the a.exe process.

 

13. What Search Path is used by Windows to Locate a DLL?

The directory where the executable module for the current process is located.

The current directory.

The Windows system directory.

The Windows directory.

The directories listed in the PATH environment variable.

Share this post


Link to post
Share on other sites

Thanks for the great explanation. I've always wondered what DLLs are exactly and why it is catastrophic whenever a windows system has errors related to certain DLLs. Now I have more of an understanding of that and can sort of see why. Again, thanks, and keep up the great work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.