What are dynamic link libraries (DLLs) and frequently asked questions

Source: Internet
Author: User
Tags definition bool empty header thread valid versions knowledge base
News | links | questions

  Profile

This article describes what is a dynamic-link library (DLL) and the various issues that may occur when you use a DLL.

Then, this article describes some of the advanced issues you should consider when developing your own DLLs. In the process of explaining what a DLL is, this article describes the dynamic link methods, DLL dependencies, DLL entry points, export DLL functions, and DLL troubleshooting tools.

At the end of this article, the DLL is compared to the Microsoft. NET Framework assembly from a higher level.

  Brief introduction

For the Microsoft Windows operating system that is listed in the "Applies to" section, a large number of features of the operating system are provided by dynamic-link libraries (DLLs). Also, when you run a program on one of these Windows operating systems, many of the program's features may be provided by DLLs. For example, some programs may contain many different modules, and each module of the program is included and distributed from the DLL.

Using DLLs helps facilitate code modularity, code reuse, efficient use of memory, and reduced disk space consumption. As a result, the operating system and programs can load and run faster and take up less disk space on the computer.

When a program uses a DLL, a problem called dependencies can cause the program to run. When a program uses a DLL, a dependency is created. If another program overwrites and corrupts the dependency, the original program may not run successfully.

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

  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, the COMDLG32 DLL performs common functions related to dialog boxes. Therefore, each program can use the functionality contained in the DLL to implement the Open dialog box. This helps facilitate code reuse and efficient use of memory.

By using DLLs, programs can be modular and consist of relatively independent components. For example, an accounting program can be sold by module. Each module can be loaded into the main program at run time (if the appropriate module is installed). Because the modules are independent of each other, the program loads faster and the modules are loaded only when the corresponding function is requested.

In addition, updates can be more easily applied to individual modules without affecting other parts of the program. For example, you might have a payroll calculation program, and the tax rate changes every year. When these changes are quarantined in the DLL, you can apply the update without rebuilding or installing the entire program.

The following table describes some of the files that are implemented as DLLs in the Windows operating system: ActiveX control (. ocx) files

An example of an ActiveX control is a calendar control that allows you to select a date from a calendar.

• Control Panel (. cpl) file

An example of a. cpl file is an item that is located in Control Panel. Each item is a private DLL.

• Device driver (. drv) files

An example of a device driver is to control the printer driver that prints to the printer.

  Advantages of DLLs

The following table illustrates some of the benefits that are provided when a program uses a DLL: • Use fewer resources

When multiple programs use the same function library, DLLs can reduce the amount of duplication of code that is loaded in disk and physical memory. This can affect not only the programs that are running in the foreground, but also other programs that run on the Windows operating system.

• Extension of modular architecture

DLLs help facilitate the development of modular programs. This can help you develop large programs that require multiple language versions or programs that require a modular architecture. An example of a modular program is an accounting program that has multiple modules that can be loaded dynamically at run time.

• Simplify deployment and installation

When a function in a DLL needs to be updated or repaired, the deployment and installation DLL does not require you to re-establish the link between the program and the DLL. In addition, if multiple programs use the same DLL, multiple programs will benefit from that update or fix. This problem may occur more frequently when you use a third-party DLL that is regularly updated or repaired.

  DLL dependencies

A dependency is created when a program or DLL uses a DLL function from another DLL. As a result, the program is no longer self-contained, and if the dependency is corrupted, the program may experience problems. For example, if one of the following actions occurs, the program may not run: • Dependent DLLs are upgraded to the new version.

• Fixed the dependent DLL.

• Dependent DLLs are overwritten by their earlier versions.

• A dependent DLL was removed from the computer.

These operations are often referred to as DLL conflicts. If backward compatibility is not enforced, the program may not run successfully.

The following table describes the changes introduced in Microsoft Windows 2000 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 removing system DLLs. Therefore, when a program installation operation attempts to delete or update a DLL that is defined as a system DLL, Windows File Protection looks for a valid digital signature.

• Private DLLs

Private DLLs allow programs to avoid being subject to changes to shared DLLs. A private DLL uses version-specific information or an empty. local file to enforce the version of the DLL used by the program. To use a private DLL, locate the DLL in the program root folder. Then, for the new program, add version-specific information to the DLL. For older programs, use an empty. local file. Each method tells the operating system to use a private DLL located in the root folder of the program.

  DLL Troubleshooting Tool

You can use multiple tools to help you troubleshoot DLL problems. The following are some of these tools.

Dependency Walker

The Dependency Walker tool can be recursively scanned for all dependent DLLs used by the program. When you open the program in Dependency Walker, Dependency Walker performs the following checks: Dependency Walker checks to see if the DLL is missing.

dependency Walker Check for invalid program files or DLLs.

Dependency Walker checks whether the import function matches the exported function.

Dependency Walker checks for cyclic dependency errors.

Dependency Walker checks for modules that are not valid due to a different operating system.

By using Dependency Walker, you can record all the DLLs that your program uses. This may help to avoid and correct DLL problems that may occur in the future. When you install Microsoft Visual Studio 6.0, Dependency Walker will be located in the following directory:

Drive\program Files\Microsoft Visual Studio\Common\Tools

DLL Universal Problem Solver

The DLL Universal Problem Solver (dups) tool is used to audit, compare, record, and display DLL information. The following table describes the utilities that make up the Dups tool: Dlister.exe

The utility enumerates all the DLLs in the computer and logs this information to a text file or database file.

Dcomp.exe

The utility compares the DLLs listed in two text files and produces a third text file that contains differences.

Dtxt2db.exe

The utility will be loaded into the Dllhell database by using a text file created by the Dlister.exe utility and the Dcomp.exe utility.

Dlgdtxt2db.exe

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

For more information about the Dups tool, click the following article number to view the article in the Microsoft Knowledge Base:

247957 (http://support.microsoft.com/kb/247957/) Example: Resolving DLL compatibility issues with DUPS.exe

DLL Help Database

The DLL Help database helps you find specific versions of DLLs that are installed by Microsoft software products. For more information about the DLL Help database, please visit the following Microsoft Web site:

http://support.microsoft.com/dllhelp/(http://support.microsoft.com/dllhelp/)

  DLL Development

This section describes the issues and requirements that you should consider when developing your own DLLs.

Type of DLL

When you load a DLL in an application, you can use two linking methods to invoke the exported DLL function. The two linking methods are dynamic linking and run-time dynamic linking at load time.

Dynamic linking at load time

In load-time dynamic linking, an application explicitly invokes an exported DLL function as if it were called 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 so, the linker provides the system with the information it needs to load the DLL and resolves the location of the exported DLL functions at load time.

Run-time dynamic Link

In run-time dynamic linking, the application calls the LoadLibrary function or the LoadLibraryEx function to load the DLL at run time. After the DLL is successfully loaded, you can use the GetProcAddress function to obtain the address of the exported DLL function to invoke. When you use run-time dynamic linking, you do not need to use the import library file.

The following list describes the application conditions for when to use dynamic link loading and when to use run-time dynamic linking: • Startup performance

If the initial startup performance of your application is important, you should use run-time dynamic linking.

• Ease of Use

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

• Application logic

In the Run-time dynamic link, the application can branch to load different modules as needed. This is important when developing a multilingual version.

DLL entry point

When you create a DLL, you can optionally specify the entry point function. The entry point function is invoked when a process or thread attaches itself to a DLL or detaches itself from a DLL. You can use the entry point function to initialize data structures or destroy data structures based on the needs of the DLL. In addition, if your application is multithreaded, you can use thread local storage (TLS) in the entry point function to allocate memory specific 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 value of FALSE, the application does not start if you are using dynamic linking at load time. If you are using run-time dynamic linking, only individual DLLs are not loaded.

The entry point function should only perform simple initialization tasks and should not call any other DLL-loading or terminating functions. For example, in an entry point function, you should not call the LoadLibrary function or the LoadLibraryEx function directly or indirectly. In addition, you should not call the FreeLibrary function when the process terminates.

Note: In multithreaded applications, make sure that access to the DLL's global data is synchronized (thread-safe) to avoid possible data corruption. To do this, use TLS to provide unique data for each thread.

Exporting DLL functions

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

To use the function keyword, you must declare each function to be exported by using the following keywords:

__declspec (dllexport)

To use exported DLL functions in your application, you must declare the functions you want to import by using the following keywords:

__declspec (dllimport)

In general, you might want to use a header file that contains define statements and IFDEF statements to separate the export statements from the import statements.

You can also use the module definition file to declare exported DLL functions. When you use a module definition file, you do not have to add a function keyword to the DLL function that is being wizard. In the module definition file, you can declare the LIBRARY statement and the exports statement for the DLL. The following code is an example of a definition file.

Sampledll.def

//

LIBRARY "Sampledll"
Exports
HelloWorld
Sample DLLs and Applications
In Microsoft Visual C + + 6.0, you can create a DLL by selecting the Win32 Dynamic link library project type or the MFC Application Wizard (DLL).
The following code is an example of a DLL created in Visual C + + by using the Win32 Dynamic link library project type.
SampleDLL.cpp
//
#include "stdafx.h"
#define Exporting_dll
#include "SampleDLL.h"
BOOL apientry DllMain (HANDLE hmodule,
DWORD Ul_reason_for_call,
LPVOID lpreserved
)
{
return TRUE;
}
void HelloWorld ()
{
MessageBox (NULL, Text ("Hello World"), Text ("in a DLL"), MB_OK);
}
File:sampledll.h
//
#ifndef Indll_h
#define Indll_h
#ifdef Exporting_dll
extern __declspec (dllexport) void HelloWorld ();
#else
extern __declspec (dllimport) void HelloWorld ();
#endif
#endif

The following code is an example of an "WIN32 application" project that calls the exported DLL function in the Sampledll DLL.

SampleApp.cpp

//

#include "stdafx.h"
#include "SampleDLL.h"
int Apientry WinMain (hinstance hinstance,
HInstance hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HelloWorld ();
return 0;
}

Note: In load-time dynamic linking, you must link the SampleDLL.lib import library that was created when the Sampledll project was built.

In the Run-time dynamic link, you should invoke the SampleDLL.dll export DLL function using code similar to the following code.

...
typedef VOID (*DLLPROC) (LPTSTR);
...
HINSTANCE hinstDLL;
Dllproc HelloWorld;
BOOL Ffreedll;
hinstDLL = LoadLibrary ("SampleDLL.dll");
if (hinstDLL!= NULL)
{
HelloWorld = (dllproc) GetProcAddress (hinstDLL, "HelloWorld");
if (HelloWorld!= NULL)
(HelloWorld);
Ffreedll = FreeLibrary (hinstDLL);
}
...

When you compile and link sampledll applications, the Windows operating system searches the following locations for Sampledll Dll:1 in the following order. Application Folder

2. Current Folder

3.Windows System Folder

Note: The GetSystemDirectory function returns the path to the Windows system folder.

4.Windows folder

Note: The GetWindowsDirectory function returns the path to the Windows folder.

  . NET Framework Assemblies

After the introduction of the Microsoft. NET and. NET Framework, most of the issues associated with DLLs have been eliminated by using assemblies. An assembly is a logical unit of functionality that runs under the control of the. NET Common Language Runtime (CLR). The assembly is actually present as a. dll file or an. exe file. Internally, however, the assembly is very different from the Microsoft Win32 DLL.

The assembly file contains the assembly manifest, type metadata, Microsoft intermediate language (MSIL) code, and other resources. The assembly manifest contains assembly metadata to provide all the information necessary to make an assembly a self-describing assembly. The assembly manifest contains the following information: • assembly name

• Version Information

• Culture information

• Strong Name information

• assembly file List

• Type reference information

• Referencing and dependent assembly information

The MSIL code contained in an assembly cannot be executed directly, and needs to be performed through the CLR. By default, when you create an assembly, the assembly is an application-specific. To create a shared assembly, you need to assign the assembly a strong name and then publish the Assembly in the global assembly cache.

The following table describes some of the features of the Assembly and compares it to the capabilities of the Win32 DLL: • Self-description

When you create an assembly, all the information required by the CLR to run the assembly is contained in the assembly manifest. The assembly manifest contains a list of dependent assemblies. Therefore, the CLR can maintain a set of consistent assemblies that are used in the application. In the Win32 DLL, when you use a shared DLL, you cannot maintain consistency among the set of DLLs used in your application.

• Version control

In the assembly manifest, version information is recorded and implemented by the CLR. In addition, version-specific usage can be implemented through version policy. In the Win32 DLL, version control cannot be implemented by the operating system. Instead, you must ensure that the DLL is backward compatible.

• Parallel deployment

Assemblies support parallel deployments. One application can use one version of the assembly, while another can use a different version of the assembly. Starting with Windows 2000, parallel deployments are supported by placing DLLs in the application folder. In addition, Windows File Protection prevents system DLLs from being overwritten or replaced by unauthorized agents.

• Independence and isolation

An application developed by using an assembly can be self-contained and isolated from other applications that are running on the computer. This feature helps create a 0 interference installation.

• implementation

Assemblies are run under security permissions that are provided by the assembly manifest and are controlled by the CLR.

• Language Independence

You can develop assemblies by using any of the supported. NET languages. For example, you can develop an assembly in Microsoft Visual C # and then use that assembly in a Microsoft visual Basic. NET Project.

  Reference

For more information about DLLs and. NET Framework assemblies, visit the following Microsoft Web site:

DLL Help Database

Http://support.microsoft.com/dllhelp (HTTP://SUPPORT.MICROSOFT.COM/DLLHELP)

DLL conflict (DLL conflicts)

Http://msdn.microsoft.com/library/en-us/dnsetup/html/dlldanger1.asp (http://msdn.microsoft.com/library/en-us/ dnsetup/html/dlldanger1.asp)

Implementing parallel component sharing in your application (implementing Side-by-side component sharing in applications)

Http://msdn.microsoft.com/library/en-us/dnsetup/html/sidebyside.asp (http://msdn.microsoft.com/library/en-us/ dnsetup/html/sidebyside.asp)

How to generate and maintain stand-alone applications and parallel assemblies for Windows XP Isolated applications and Side-by-side assemblies for Windows XP)

Http://msdn.microsoft.com/library/en-us/dnwxp/html/sidexsidewinxp.asp (http://msdn.microsoft.com/library/en-us/ dnwxp/html/sidexsidewinxp.asp)

Simplifies deployment and resolution of DLL conflicts using the. NET Framework (simplifying deployment and solving DLLs conflicts with the. NET Framework)

Http://msdn.microsoft.com/library/en-us/dndotnet/html/dplywithnet.asp (http://msdn.microsoft.com/library/en-us/ dndotnet/html/dplywithnet.asp)

. NET Framework Developer's Guide: Assemblies

Http://msdn.microsoft.com/library/en-us/cpguide/html/cpconassemblies.asp (http://msdn.microsoft.com/library/ en-us/cpguide/html/cpconassemblies.asp)

Create Win32 DL (creating a Win32 DLL)

Http://msdn.microsoft.com/library/en-us/vccore98/html/_core_overview.3a_.creating_a_win32_dll.asp (http:// msdn.microsoft.com/library/en-us/vccore98/html/_core_overview.3a_.creating_a_win32_dll.asp)

Run-time dynamic Link

Http://msdn2.microsoft.com/en-us/library/ms685090.aspx (http://msdn2.microsoft.com/en-us/library/ms685090.aspx)

Threads local Storage (thread native storage)

Http://msdn2.microsoft.com/en-us/library/ms686749.aspx



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.