Current location - Quotes Website - Signature design - What is a DLL? ?
What is a DLL? ?
Summary

This article explains what a dynamic link library (DLL) is and various problems that may occur when using DLL.

then, this article explains some advanced issues that should be considered when developing your own DLL. In the process of explaining what DLL is, this paper will explain the dynamic link method, DLL dependency, DLL entry point, export DLL function and DLL troubleshooting tools.

at the end of this article, we will compare DLL with Microsoft .NET Framework assembly at a high level.

Introduction

For the Microsoft Windows operating system listed in the section "Applicable to", many functions of the operating system are provided by dynamic link libraries (DLLs). In addition, when you run a program on one of these Windows operating systems, many functions of the program may be provided by DLL. For example, some programs may contain many different modules, and each module of the program is contained in a DLL and distributed from it.

Using DLL is helpful to promote code modularization, code reuse, effective use of memory and reduction of occupied disk space. Therefore, the operating system and programs can be loaded and run faster, and occupy less disk space in the computer.

when a program uses a DLL, a problem called dependency may prevent the program from running. When a program uses a DLL, it creates a dependency. If other programs rewrite and damage the dependency, the original program may not run successfully.

after the introduction of Microsoft .NET Framework, most dependency problems have been eliminated by using assemblies.

more information

what is a DLL?

a dll is a library that contains code and data that can be used by multiple programs at the same time. For example, in the Windows operating system, Comdlg32 DLL performs common functions related to dialog boxes. Therefore, every program can use the functions contained in the DLL to realize the Open dialog box. This helps to promote code reuse and effective use of memory.

by using DLL, the program can be modularized and composed of relatively independent components. For example, an accounting program can be sold by module. Each module can be loaded into the main program at runtime (if the corresponding module is installed). Because the modules are independent of each other, the loading speed of the program is faster, and the modules are only loaded when the corresponding functions are requested.

in addition, it is easier to apply the update to each module without affecting other parts of the program. For example, you may have a payroll calculation program and the tax rate changes every year. When these changes are isolated in the DLL, you can apply the updates without rebuilding or installing the whole program.

the following table illustrates some files implemented as dlls in the Windows operating system:? ActiveX control (.ocx) file

an example of an ActiveX control is a calendar control, which allows you to select a date from a calendar. Control panel (.cpl) file

An example of a. CPL file is an item located in the control panel. Each item is a dedicated DLL. Device driver (.drv) file

an example of a device driver is a printer driver that controls printing to a printer.

advantages of DLL

the following table illustrates some advantages provided when a program uses dll:? Use less resources

When multiple programs use the same function library, DLL can reduce the duplication of code loaded in disk and physical memory. This can greatly affect not only the programs running in the foreground, but also other programs running on the Windows operating system. Popularizing the modular architecture

DLL is helpful to promote the development of modular programs. This can help you develop large programs that require multiple language versions or programs that require modular architecture. An example of a modular program is an accounting program with multiple modules that can be dynamically loaded at runtime. Simplify deployment and installation

When the functions in the DLL need to be updated or repaired, deploying and installing the DLL does not require re-establishing the link between the program and the DLL. In addition, if multiple programs use the same DLL, then multiple programs will benefit from the update or repair. This problem may occur more frequently when you use a third-party DLL that is updated or repaired regularly.

DLL dependencies

when a program or DLL uses DLL functions in other DLLs, a dependency is created. Therefore, the program is no longer independent, and if the dependency is damaged, the program may encounter problems. For example, the program may not run if one of the following actions occurs:? Rely on DLL to upgrade to a new version. Fixed dependency DLL. Dependent DLL is overwritten by its earlier version. Deleted the dependent DLL from the computer.

these operations are often called DLL conflicts. If backward compatibility is not enforced, the program may not run successfully.

The following table describes the changes introduced in Microsoft Windows 2 and later Windows operating systems to help minimize dependency problems:? Windows file protection

in Windows file protection, the operating system prohibits unauthorized agents from updating or deleting the system DLL. Therefore, when a program installation operation attempts to delete or update a DLL defined as a system DLL, Windows File Protection will look for a valid digital signature. Special DLL

A special DLL can protect the program from changes to the * * * exclusive DLL. Special DLL uses version-specific information or an empty. local file to force the version of the DLL used by the program. To use a dedicated DLL, look for the DLL in the program root folder. Then, for the new program, add version-specific information to the DLL. For the old program, please use an empty. local file. Each method tells the operating system to use a special DLL located in the program root folder.

DLL troubleshooting tools

you can use several tools to help you solve DLL problems. The following are some of the tools.

dependency walker

the dependency walker tool can recursively scan to find all the dependent dlls used by the program. When you open a program in Dependency Walker, Dependency Walker performs the following checks:? Dependency Walker checks whether the DLL is missing. Dependency Walker checks whether there are invalid program files or dlls. Dependency Walker checks whether the import function and the export function match. Dependency Walker checks whether there is a circular dependency error. Dependency Walker checks whether there is a module that is invalid because it is aimed at a different operating system.

by using Dependency Walker, you can record all the dlls used by the program. This may help to avoid and correct DLL problems that may occur in the future. When you install Microsoft Visual Studio 6., Dependency Walker will be located in the following directory: drive \ program files \ Microsoft Visual Studio \ common \ Tools

DLL universal problem solver

dll universal problem solver (dups) tool is used to review, compare, record and display dll information. The following table describes the utilities that make up the DUPS tool:? Delister.exe

This utility enumerates all DLLs in the computer and records this information in a text file or database file. Dcomp.exe

this utility compares dlls listed in two text files and generates a third text file containing differences. Dtxt2DB.exe

This utility loads a text file created by using Dlister.exe utility and Dcomp.exe utility into the dllHell database. DlgDtxt2DB.exe

This utility provides a graphical user interface (GUI) version of the Dtxt2DB.exe utility.

types of DLLs

when you load a DLL in an application, you can use two linking methods to call the exported dll function. These two linking methods are dynamic linking at load time and dynamic linking at run time.

dynamic link at load time

in dynamic link at load time, the application explicitly calls the exported DLL function like a local function. To use load-time dynamic linking, provide a header file (.h) and an import library file (.lib) when compiling and linking the application. When you do this, the linker will provide the system with the information needed to load the DLL and resolve the location of the exported DLL function when loading.

runtime dynamic linking

in runtime dynamic linking, an application calls the LoadLibrary function or the LoadLibraryEx function to load a DLL at runtime. After successfully loading the DLL, you can use the GetProcAddress function to get the address of the exported DLL function to be called. When using runtime dynamic linking, you don't need to use the import library file.

the following list describes the application conditions about when to use load-time dynamic linking and when to use run-time dynamic linking:? Startup performance

if the initial startup performance of an application is important, you should use runtime dynamic linking. Ease of use

In dynamic linking at load time, exported DLL functions are similar to local functions. This makes it easy for you to call these functions. Application logic

in runtime dynamic linking, applications can branch to load different modules as needed. This is important when developing multilingual versions.

DLL entry point

when creating a DLL, you can selectively specify the entry point function. Entry point functions are called when processes or threads attach themselves to or detach themselves from a DLL. You can use the entry point function to initialize or destroy the data structure according to the needs of the DLL. In addition, if the application is multithreaded, thread local storage (TLS) can be used in the entry point function to allocate memory dedicated to each thread. The following code is an example of a DLL entry point function.

BOOL APIENTRY DllMain(

HANDLE hModule, // Handle to DLL module

DWORD ul_reason_for_call, // Reason for calling function

LPVOID lpReserved ) // Reserved

{

switch ( ul_reason_for_call )

{

case DLL_PROCESS_ATTACHED:

// A process is loading the DLL.

break;

case DLL_THREAD_ATTACHED:

// A process is creating a new thread.

break;

case DLL_THREAD_DETACH:

// A thread exits normally.

break;

case DLL_PROCESS_DETACH:

// A process unloads the DLL.

break;

}

return TRUE;

}

When the entry point function returns a FALSE value, if you use dynamic link at load time, the application will not start. If you are using runtime dynamic linking, only individual DLLs will not be loaded.

the entry point function should only perform a simple initialization task, and should not call any other DLL loading function or terminating function. For example, in the entry point function, the LoadLibrary function or LoadLibraryEx function should not be called directly or indirectly. In addition, the FreeLibrary function should not be called when the process is terminated.

note: in a multithreaded application, please ensure that the access to the global data of the DLL is synchronized (thread safety) to avoid possible data corruption. To do this, use TLS to provide unique data for each thread.

exporting DLL functions

to export DLL functions, you can add function keywords to the exported DLL functions or create a module definition file (.def) to list the exported DLL functions.

to use function keywords, you must use the following keywords to declare each function to be exported: __declspec(DLLexport) To use exported dll functions in an application, you must use the following keywords to declare each function to be imported: _ _ declspec (dll limport) Usually, you'd better use a header file containing a define statement and an ifdef statement to separate the export statement from the import statement.