All Topics  
Dynamic-link library

 

   Email Print
   Bookmark   Link






 

Dynamic-link library



 
 
Dynamic-link library (also written without the hyphen), or DLL, is Microsoft
Microsoft

Microsoft Corporation is a multinational corporation computer technology corporation that develops, manufactures, licenses, and supports a wide range of computer software products for computing devices....
's implementation of the shared library concept in the Microsoft Windows
Microsoft Windows

Microsoft Windows is a series of software operating systems and graphical user interfaces produced by Microsoft. Microsoft first introduced an operating environment named Windows in November 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces ....
 and OS/2
OS/2

OS/2 is a computer operating system, initially created by Microsoft and IBM, then later developed by IBM exclusively. The name stands for "Operating System/2," because it was introduced as part of the same generation change release as IBM's "IBM Personal System/2 " line of second-generation personal computers....
 operating system
Operating system

An operating system is an interface between hardware and applications; it is responsible for the management and coordination of activities and the sharing of the limited resources of the computer....
s. These libraries usually have the file extension DLL, OCX (for libraries containing ActiveX
ActiveX

ActiveX is a component Object Model developed by Microsoft for Microsoft Windows. By using the Component Object Model runtime, developers can create Component-based software engineering that perform a particular function or a set of functions....
 controls), or DRV (for legacy system driver
Device driver

In computing, a device driver or software driver is a computer program allowing higher-level computer programs to interact with a hardware device....
s). The file formats for DLLs are the same as for Windows EXE
EXE

EXE is the common filename extension denoting an executable file in the DOS, OpenVMS, Microsoft Windows, ReactOS, and OS/2 operating systems.Besides the executable program itself, many EXE files contain other components called Resource , such as bitmaps and icons which the executable program may use for its graphical user interface....
 files — that is, Portable Executable
Portable Executable

The Portable Executable format is a file format for executables, object file, and Dynamic-link librarys, used in 32-bit and 64-bit versions of Microsoft Windows operating systems....
 (PE) for 32-bit
32-bit

The range of integer values that can be stored in 32 bits is 0 through 4,294,967,295 or -2,147,483,648 through 2,147,483,647 using two's complement encoding....
 and 64-bit
64-bit

64-bit CPUs have existed in supercomputers since the 1960s and in RISC-based computer workstation and Server s since the early 1990s. In 2003 they were introduced to the mainstream personal computer arena, in the form of the x86-64 and 64-bit PowerPC processor architectures....
 Windows, and New Executable
New Executable

The NE, abbreviation for New Executable, is an executable file format that was introduced with MS-DOS 4.0 , and that was also used later in OS/2 and 16-bit Microsoft Windows....
 (NE) for 16-bit
16-bit

16-bit architectureThe HP 2100#Descendants and variants , introduced in 1975, was the world's first 16-bit microprocessor.Prominent 16-bit processors include the PDP-11, Intel 8086, Intel 80286 and the WDC 65C816....
 Windows.






Discussion
Ask a question about 'Dynamic-link library'
Start a new discussion about 'Dynamic-link library'
Answer questions from other users
Full Discussion Forum



Encyclopedia


Dynamic-link library (also written without the hyphen), or DLL, is Microsoft
Microsoft

Microsoft Corporation is a multinational corporation computer technology corporation that develops, manufactures, licenses, and supports a wide range of computer software products for computing devices....
's implementation of the shared library concept in the Microsoft Windows
Microsoft Windows

Microsoft Windows is a series of software operating systems and graphical user interfaces produced by Microsoft. Microsoft first introduced an operating environment named Windows in November 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces ....
 and OS/2
OS/2

OS/2 is a computer operating system, initially created by Microsoft and IBM, then later developed by IBM exclusively. The name stands for "Operating System/2," because it was introduced as part of the same generation change release as IBM's "IBM Personal System/2 " line of second-generation personal computers....
 operating system
Operating system

An operating system is an interface between hardware and applications; it is responsible for the management and coordination of activities and the sharing of the limited resources of the computer....
s. These libraries usually have the file extension DLL, OCX (for libraries containing ActiveX
ActiveX

ActiveX is a component Object Model developed by Microsoft for Microsoft Windows. By using the Component Object Model runtime, developers can create Component-based software engineering that perform a particular function or a set of functions....
 controls), or DRV (for legacy system driver
Device driver

In computing, a device driver or software driver is a computer program allowing higher-level computer programs to interact with a hardware device....
s). The file formats for DLLs are the same as for Windows EXE
EXE

EXE is the common filename extension denoting an executable file in the DOS, OpenVMS, Microsoft Windows, ReactOS, and OS/2 operating systems.Besides the executable program itself, many EXE files contain other components called Resource , such as bitmaps and icons which the executable program may use for its graphical user interface....
 files — that is, Portable Executable
Portable Executable

The Portable Executable format is a file format for executables, object file, and Dynamic-link librarys, used in 32-bit and 64-bit versions of Microsoft Windows operating systems....
 (PE) for 32-bit
32-bit

The range of integer values that can be stored in 32 bits is 0 through 4,294,967,295 or -2,147,483,648 through 2,147,483,647 using two's complement encoding....
 and 64-bit
64-bit

64-bit CPUs have existed in supercomputers since the 1960s and in RISC-based computer workstation and Server s since the early 1990s. In 2003 they were introduced to the mainstream personal computer arena, in the form of the x86-64 and 64-bit PowerPC processor architectures....
 Windows, and New Executable
New Executable

The NE, abbreviation for New Executable, is an executable file format that was introduced with MS-DOS 4.0 , and that was also used later in OS/2 and 16-bit Microsoft Windows....
 (NE) for 16-bit
16-bit

16-bit architectureThe HP 2100#Descendants and variants , introduced in 1975, was the world's first 16-bit microprocessor.Prominent 16-bit processors include the PDP-11, Intel 8086, Intel 80286 and the WDC 65C816....
 Windows. As with EXEs, DLLs can contain code, data
Data (computing)

In computer science, data is anything in a form suitable for use with a computer. Data is often distinguished from computer programs. A program is a set of instruction that detail a task for the computer to perform....
, and resource
Resource (Windows)

In Microsoft Windows, resources are read-only data embedded in EXE or Dynamic-Link Library files.The Windows API provides for easy access to all applications' resources....
s, in any combination.

In the broader sense of the term, any data file
Computer file

A computer file is a block of arbitrary information, or resource for storing information, which is available to a computer program and is usually based on some kind of durable computer storage....
 with the same file format
File format

A file format is a particular way to encode information for storage in a computer file.Since a disk drive, or indeed any computer storage, can store only bits, the computer must have some way of converting information to 0s and 1s and vice-versa....
 can be called a resource DLL. Examples of such DLLs include icon
Icon (computing)

On computer displays, a computer icon is a small pictogram. Icons have been used to supplement the normal alphanumerics of the computer. Modern computers now can handle bitmapped graphics on the display terminal, so the icons are widely used to assist users....
 libraries
, sometimes having the extension ICL, and font
Typeface

In typography, a typeface is a set of one or more fonts, in one or more sizes, designed with stylistic unity, each comprising a coordinated set of glyphs....
 files, having the extensions FON and FOT.

Background for DLL


The first versions of Microsoft Windows
Microsoft Windows

Microsoft Windows is a series of software operating systems and graphical user interfaces produced by Microsoft. Microsoft first introduced an operating environment named Windows in November 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces ....
 ran every program in a single address space
Address space

In computing, an address space defines a range of discrete addresses, each of which may correspond to a physical or virtual memory register, a Node , peripheral device, disk sector or other logical or physical entity....
. Every program was meant to co-operate by yielding the CPU to other programs so that the GUI was capable of multitasking and could be as responsive as possible. All Operating-System level operations were provided by the underlying operating system: MS-DOS
MS-DOS

MS-DOS is an operating system commercialized by Microsoft. It was the most commonly used member of the DOS family of operating systems and was the main operating system for personal computers during the 1980s....
. All higher level services were provided by Windows Libraries Dynamic Link Libraries. The drawing API, GDI
GDI

GDI is an acronym that can stand for:* Gasoline direct injection, a modern variant of fuel injection* Gender-related Development Index, an indication of the standard of living in a country, developed by the United Nations...
 was implemented in a DLL called GDI.EXE, the user interface in USER.EXE. These extra layers on top of DOS had to be shared across all running windows programs, not just to enable Windows to work in a machine with less than a megabyte of RAM, but to enable the programs to co-operate amongst each other. The Graphics Device Interface
Graphics Device Interface

The Graphics Device Interface is a Microsoft Windows application programming interface and core operating system component that is responsible for representing graphical objects and transmitting them to output devices such as computer display and computer printer....
 code in GDI needed to translate drawing commands to operations on specific devices. On the display, it had to manipulate pixels in the frame buffer. When drawing to a printer, the API calls had to be transformed into requests to a printer. Although it could have been possible to provide hard-coded support for a limited set of devices (like the Color Graphics Adapter
Color Graphics Adapter

The Color Graphics Adapter , originally also called the Color/Graphics Adapter or IBM Color/Graphics Monitor Adapter, introduced in 1981, was International Business Machines's first color graphics card, and the first color computer display standard for the IBM PC....
 display, the HP LaserJet Printer Command Language
Printer Command Language

Printer Command Language, more commonly referred to as PCL, is a Page description language developed by HP as a computer printer protocol and has become a de facto industry standard....
), Microsoft chose a different approach. GDI would work by loading different pieces of code to work with different output devices—pieces of code called 'Device Driver
Device driver

In computing, a device driver or software driver is a computer program allowing higher-level computer programs to interact with a hardware device....
s'.

The same architectural concept that allowed GDI to load different device drivers is that which allowed the Windows shell to load different windows programs, and for these programs to invoke API calls from the shared USER and GDI libraries. That concept was Dynamic Linking.

In a conventional non-shared, static library, sections of code are simply added to the calling program when its executable is built at the linking phase; if two programs use the same routine, the code has to be included in both. With dynamic linking, shared code is placed into a single, separate file. The programs that call this file are connected to it at run time, with the operating system (or, in the case of early versions of Windows, the OS-extension), performing the binding.

For those early versions of Windows (1.0 to 3.11), the DLLs were the foundation for the entire GUI.
  • Display drivers were merely DLLs with a .DRV extension that provided custom implementations of the same drawing API through a unified Device Driver Interface (DDI).
  • The Drawing (GDI) and GUI (USER) APIs were merely the function calls exported by the GDI and USER, system DLLs with .EXE extension.
  • Windows Screen savers were merely DLLs with a .SCR extension


This notion of building up the operating system from a collection of dynamically loaded libraries is a core concept of Windows that persists even today. DLLs provide the standard benefits of shared libraries, such as modularity
Modularity (programming)

Modular programming is a software design technique that increases the extent to which software is composed from separate parts, called modules. Conceptually, modules represent a separation of concerns, and improve maintainability by enforcing logical boundaries between components....
. Modularity allows changes to be made to code and data in a single self-contained DLL shared by several applications without any change to the applications themselves.

Another benefit of the modularity is the use of generic interfaces for plug-ins. A single interface may be developed which allows old as well as new modules to be integrated seamlessly at run-time into pre-existing applications, without any modification to the application itself. This concept of dynamic extensibility is taken to the extreme with the Component Object Model
Component Object Model

Component Object Model is an interface standard for software componentry introduced by Microsoft in 1993. It is used to enable interprocess communication and dynamic object creation in a large range of programming languages....
, the underpinnings of ActiveX
ActiveX

ActiveX is a component Object Model developed by Microsoft for Microsoft Windows. By using the Component Object Model runtime, developers can create Component-based software engineering that perform a particular function or a set of functions....
.

In Windows 1.x, 2.x and 3.x, all windows applications shared the same address space, as well as the same memory. A DLL was only loaded once into this address space; from then on all programs using the library accessed it. The library's data was shared across all the programs. This could be used as an indirect form of Inter-process communication
Inter-process communication

Inter-Process Communication is a set of techniques for the exchange of data among multiple thread in one or more Process . Processes may be running on one or more computers connected by a computer network....
, or it could accidentally corrupt the different programs. With Windows 95
Windows 95

Windows 95 is a consumer-oriented graphical user interface-based operating system. It was released on August 24, 1995 by Microsoft, and was a significant progression from the company's previous Microsoft Windows products....
 and successors every process runs in its own address space. While the DLL code may be shared, the data is private except where shared data is explicitly requested by the library. That said, large swathes of Windows 95
Windows 95

Windows 95 is a consumer-oriented graphical user interface-based operating system. It was released on August 24, 1995 by Microsoft, and was a significant progression from the company's previous Microsoft Windows products....
, Windows 98
Windows 98

Windows 98 is a graphical operating system released on 25 June 1998 by Microsoft and the successor to Windows 95. Like its predecessor, it is a hybrid 16-bit application/32-bit application monolithic product based on MS-DOS....
 and Windows Me
Windows Me

Windows Millennium Edition, or Windows Me , is a hybrid 16-bit/32-bit graphical operating system released on 14 September 2000 by Microsoft....
 were built from 16-bit libraries, a feature which limited the performance of the Pentium Pro
Pentium Pro

The Pentium Pro is a sixth-generation x86-based microprocessor developed and manufactured by Intel introduced in November 1995. It introduced the Intel P6 and was originally intended to replace the original Pentium in a full range of applications....
 microprocessor when launched, and ultimately limited the stability and scalability of the DOS-based versions of Windows.

While DLLs are the core of the Windows architecture, they have a number of drawbacks, collectively called "DLL hell
DLL hell

In computing, DLL hell is a colloquial term for the complications that arise when working with dynamic link library used with Microsoft Windows operating systems, particularly legacy 16-bit editions....
". Currently, Microsoft promotes Microsoft .NET as one solution to the problems of DLL hell, although they now promote Virtualization based solutions such as Microsoft Virtual PC
Microsoft Virtual PC

Microsoft Virtual PC is a virtualization suite for Microsoft Windows, and an emulation suite for Mac OS X on PowerPC-based systems. The software was originally written by Connectix, and was subsequently acquired by Microsoft....
 and Microsoft Application Virtualization
Microsoft Application Virtualization

Microsoft Application Virtualization is an Application Virtualization and application streaming solution from Microsoft. It was acquired by Microsoft during the List of companies acquired by Microsoft Corporation of Boston, Massachusetts-based Softricity on July 17, 2006....
, because they offer superior isolation between applications. An alternative mitigating solution to DLL hell has been the implementation of Side-by-Side Assembly
Side-by-side assembly

Side-by-side technology is a standard for executable in Microsoft Windows XP and later versions that attempts to reduce DLL hell. Side-by-side technology is also known as WinSxS or SxS....
.

Features of DLL


Memory management

In Win32, the DLL files are organized into sections. Each section has its own set of attributes, such as being writable or read-only, executable (for code) or non-executable (for data), and so on.

The code in a DLL is usually shared among all the processes that use the DLL; that is, they occupy a single place in physical memory, and do not take up space in the page file. If the physical memory occupied by a code section is to be reclaimed, its contents are discarded, and later reloaded directly from the DLL file as necessary.

In contrast to code sections, the data sections of a DLL are usually private; that is, each process using the DLL has its own copy of all the DLL's data. Optionally, data sections can be made shared, allowing inter-process communication
Inter-process communication

Inter-Process Communication is a set of techniques for the exchange of data among multiple thread in one or more Process . Processes may be running on one or more computers connected by a computer network....
 via this shared memory area. However, because user restrictions do not apply to the use of shared DLL memory, this creates a security hole; namely, one process can corrupt the shared data, which will likely cause all other sharing processes to behave undesirably. For example, a process running under a guest account can in this way corrupt another process running under a privileged account. This is an important reason to avoid the use of shared sections in DLLs.

If a DLL is compressed by certain executable packers
Executable compression

Executable compression is any means of Data compression an executable file and combining the compressed data with the decompression code it needs into a single executable....
 (e.g. UPX
UPX

UPX, the Ultimate Packer for eXecutables, is a free and open source Executable compression supporting a number of file formats from different operating systems....
), all of its code sections are marked as read-and-write, and will be unshared. Read-and-write code sections, much like private data sections, are private to each process. Thus DLLs with shared data sections should not be compressed if they are intended to be used simultaneously by multiple programs, since each program instance would have to carry its own copy of the DLL, resulting in increased memory consumption.

Import libraries

Linking to dynamic libraries is usually handled by linking to an import library when building or linking to create an executable file. The created executable then contains an import address table (IAT) by which all DLL function calls are referenced (each referenced DLL function contains its own entry in the IAT). At run-time, the IAT is filled with appropriate addresses that point directly to a function in the separately-loaded DLL.

Like static libraries, import libraries for DLLs are noted by the .lib
Lib

The term lib or Lib may refer to any of the following:* liberation * Lib Island in the Marshall Islands* One of List of Book of Mormon people#L Jaredite kings in the Book of Mormon...
 file extension. For example, kernel32.dll
Kernel32.dll

Kernel32.dll is a dynamic link library present in all 32-bit and 64-bit versions of Microsoft Windows. It exposes to applications most of the Win32 base APIs, such as memory management, input/output operations, creation, and synchronization functions....
, the primary dynamic library for Windows' base functions such as file creation and memory management, is linked via kernel32.lib.

Symbol resolution and binding

Each function exported by a DLL is identified by a numeric ordinal and optionally a name. Likewise, functions can be imported from a DLL either by ordinal or by name. The ordinal represents the position of the functions address pointer in the DLL Export Address table. It is common for internal functions to be exported by ordinal only. For most Windows API functions only the names are preserved across different Windows releases; the ordinals are subject to change. Thus, one cannot reliably import Windows API functions by their ordinals.

Importing functions by ordinal provides only slightly better performance than importing them by name: export tables of DLLs are ordered by name, so a binary search can be used to find a function. The index of the found name is then used to lookup the ordinal in the Export Ordinal table. In 16-bit Windows, the name table was not sorted, so the name lookup overhead was much more noticeable.

It is also possible to bind an executable to a specific version of a DLL, that is, to resolve the addresses of imported functions at compile-time. For bound imports, the linker saves the timestamp and checksum of the DLL to which the import is bound. At run-time Windows checks to see if the same version of library is being used, and if so, Windows bypasses processing the imports. Otherwise, if the library is different from the one which was bound to, Windows processes the imports in a normal way.

Bound executables load somewhat faster if they are run in the same environment that they were compiled for, and exactly the same time if they are run in a different environment, so there's no drawback for binding the imports. For example, all the standard Windows applications are bound to the system DLLs of their respective Windows release. A good opportunity to bind an application's imports to its target environment is during the application's installation. This keeps the libraries 'bound' until the next OS update. It does, however, change the checksum of the executable, so is not something that can be done with signed programs, or programs that are managed by a configuration management tool that uses checksums (such as MD5
MD5

In cryptography, MD5 is a widely used cryptographic hash function with a 128-bit hash value. As an Internet standard , MD5 has been employed in a wide variety of security applications, and is also commonly used to check the integrity of computer file....
 checksums) to manage file versions. As more recent Windows versions have moved away from having fixed addresses for every loaded library (for security reasons), the opportunity and value of binding an executable is decreasing.

Explicit run-time linking


DLL files may be explicitly loaded at run-time, a process referred to simply as run-time dynamic linking by Microsoft, by using the LoadLibrary (or LoadLibraryEx) API function. The GetProcAddress API function is used to lookup exported symbols by name, and FreeLibrary — to unload the DLL. These functions are analogous to dlopen, dlsym, and dlclose in the POSIX
POSIX

POSIX or "Portable Operating System Interface" is the collective name of a family of related standardizations specified by the Institute of Electrical and Electronics Engineers to define the application programming interface , along with shell and utilities interfaces for software compatible with variants of the Unix operating system, altho...
 standard API.

Note that with implicit run-time linking, referred to as load-time dynamic linking by Microsoft, if the linked DLL file cannot be found, Windows will display an error message and fail to load the application. The application developer cannot handle the absence of DLL files linked implicitly by the compile-time linker. On the other hand, with explicit run-time linking, developers have the opportunity to provide a graceful fall-back facility. For example, in Windows 3.1x
Windows 3.1x

Windows 3.1x is a line of operating systems produced by Microsoft for use on personal computers. The line began with Windows 3.1, which was released in March 1992 as a successor to Windows 3.0....
 a program with explicit linking could check for the (then optional) Object Linking and Embedding
Object Linking and Embedding

Object Linking and Embedding is a technology that allows embedding and linking to documents and other objects developed by Microsoft. For developers, it brought OLE custom controls , a way to develop and use custom user interface elements....
 libraries and expose additional functionality if they were present, whereas a program with implicit linking would fail to load.

// LSPaper draw using OLE2 function if available on client

HINSTANCE hOle2Dll ; hOle2Dll = LoadLibrary ( "OLE2.DLL" ) ;

if ( hOle2Dll != NULL )



The procedure for explicit run-time linking is the same in any language that supports pointers to functions
Function pointer

A function pointer is a type of pointer in C , C++, D programming language, and other C-like programming languages. When Dereference operator, a function pointer invokes a subroutine, passing it zero or more arguments just like a normal function....
, since it depends on the Windows API
Windows API

The Windows API, informally WinAPI, is Microsoft's core set of application programming interfaces available in the Microsoft Windows operating systems....
 rather than language constructs.

Compiler and language considerations


Delphi

In the heading of a source file, the keyword library is used instead of program. In the end of the file, the functions to be exported are listed in exports clause.

Delphi does not require LIB files to import functions from DLLs. To link to a DLL, external keyword is used in function declaration.

Microsoft Visual Basic
Visual Basic

'Visual Basic' is the third-generation programming language event-driven programming and integrated integrated development environment from Microsoft for its Component Object Model programming model....
 

In Visual Basic (VB), only run-time linking is supported; but in addition to using LoadLibrary and GetProcAddress API functions, declarations of imported functions are allowed.

When importing DLL functions through declarations, VB will generate a run-time error if the DLL file cannot be found. The developer can catch the error and handle it appropriately.

When creating DLLs in VB, the IDE will only allow you to create ActiveX DLLs, however to allow the user to explicitly tell the linker to include a .DEF file which defines the ordinal position and name of each exported function. This allows the user to create a standard Windows DLL using Visual Basic (Version 6 or lower) which can be referenced through a "Declare" statement.

C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
 and C++

Microsoft Visual C++ (MSVC) provides a number of extensions to standard C++ which allow functions to be specified as imported or exported directly in the C++ code; these have been adopted by other Windows C and C++ compilers, including Windows versions of GCC
GNU Compiler Collection

The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain....
. These extensions use the attribute __declspec before a function declaration. When external names follow the C naming conventions, they must also be declared as extern "C" in C++ code, to prevent them from using C++ naming conventions.

Besides specifying imported or exported functions using __declspec attributes, they may be listed in IMPORT or EXPORTS section of the DEF file used by the project. The DEF file is processed by the linker, rather than the compiler, and thus it is not specific to C++.

DLL compilation will produce both DLL and LIB files. The LIB file is used to link against a DLL at compile-time; it is not necessary for run-time linking. Unless your DLL is a COM
Component Object Model

Component Object Model is an interface standard for software componentry introduced by Microsoft in 1993. It is used to enable interprocess communication and dynamic object creation in a large range of programming languages....
 server, the DLL file must be placed in one of the directories listed in the PATH environment variable, in the default system directory, or in the same directory as the program using it. COM
Component Object Model

Component Object Model is an interface standard for software componentry introduced by Microsoft in 1993. It is used to enable interprocess communication and dynamic object creation in a large range of programming languages....
 server DLLs are registered using regsvr32.exe, which places the DLL's location and its globally unique ID (GUID) in the registry. Programs can then use the DLL by looking up its GUID in the registry to find its location.

Programming examples


Creating DLL exports

The following examples show language-specific bindings for exporting symbols from DLLs.

Delphi

library Example;

// function that adds two numbers function AddNumbers(a, b : Double): Double; begin Result := a + b; end;

// export this function exports AddNumbers;

// DLL initialization code: no special handling needed begin end.

C and C++
  1. include


// DLL entry function (called on load, unload, ...) BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)

// Exported function - adds two numbers extern "C" __declspec(dllexport) double AddNumbers(double a, double b)



Using DLL imports

The following examples show how to use language-specific bindings to import symbols for linking against a DLL at compile-time.

Delphi



program Example;

// import function that adds two numbers function AddNumbers(a, b : Double): Double; external 'Example.dll';

// main program var R:Double;

begin R := AddNumbers(1, 2); Writeln('The result was: ', R); end.

C and C++

Make sure you include Example.lib file (assuming that Example.dll is generated) in the project (Add Existing Item option for Project!) before static linking. The file Example.lib is automatically generated by the compiler when compiling the DLL. Not executing the above statement would cause linking error as the linker would not know where to find the definition of AddNumbers. You also need to copy the DLL Example.dll to the location where the .exe file would be generated by the following code.
  1. include
  2. include


// Import function that adds two numbers extern "C" __declspec(dllimport) double AddNumbers(double a, double b);

int main(int argc, char *argv[])



Using explicit run-time linking

The following examples show how to use the run-time loading and linking facilities using language-specific WIN32 API bindings.

Microsoft Visual Basic
Option Explicit Declare Function AddNumbers Lib "Example.dll" _ (ByVal a As Double, ByVal b As Double) As Double

Sub Main Dim Result As Double Result = AddNumbers(1, 2) Debug.Print "The result was: " & Result End Sub

Delphi
program Example;

uses Windows;

var AddNumbers : function (a, b: Double): Double; LibHandle : HMODULE;

begin LibHandle := LoadLibrary('example.dll');

if LibHandle = 0 then Exit;

AddNumbers := GetProcAddress(LibHandle, 'AddNumbers');

if Assigned( AddNumbers ) then Writeln( '1 + 2 = ', AddNumbers( 1, 2 ) ); else Writeln('Error: unable to find DLL function');

FreeLibrary(LibHandle); end.

C and C++
  1. include
  2. include


// DLL function signature typedef double (*importFunction)(double, double);

int main(int argc, char **argv)



Component Object Model

The Component Object Model
Component Object Model

Component Object Model is an interface standard for software componentry introduced by Microsoft in 1993. It is used to enable interprocess communication and dynamic object creation in a large range of programming languages....
 (COM) extends the DLL concept to object-oriented programming
Object-oriented programming

Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
. Objects can be called from another process or hosted on another machine. COM objects have unique GUIDs and can be used to implement powerful back-ends to simple GUI front ends such as Visual Basic and ASP. They can also be programmed from scripting languages. COM objects are more complex to create and use than DLLs.

Criticism


Microsoft DLLs were created to solve problems existing under batch systems in the 1960s. As operating systems, including Windows, have moved away from fixed memory models to paged virtual memory management systems, the problem DLLs were invented to solve have disappeared.

DLLs create non-obvious links between a program and code fragment "somewhere" in the system: even simple programs may not be totally contained in its .exe file, but be spread out over, and be dependent on, multiple DLLs. The exact location and depth of those dependencies may be difficult or impossible to determine. Since DLLs can call other DLLs, the chain of complexity can extend to any depth.

Second, DLLs are subject to problems caused by missing DLLs and conflicts between DLL versions, the so-called "DLL hell
DLL hell

In computing, DLL hell is a colloquial term for the complications that arise when working with dynamic link library used with Microsoft Windows operating systems, particularly legacy 16-bit editions....
". This is such a significant problem for Windows applications that even Microsoft is known to use the term.

Critics of the dynamic linking model argue that it is the wrong model for paged memory managed systems, and that static linking is better and more secure. DLLs, it is argued, degrade the isolation between processes that modern operating systems provide, consequently violating the security of the entire system.

See also

  • Dependency Walker
    Dependency walker

    Dependency Walker or depends.exe is a freeware Computer program for Microsoft Windows used to list the imported and exported functions of a Portable Executable file....
    , a utility which displays exported and imported functions of DLL and EXE files.
  • Dynamic library
  • Library (computing)
  • Linker
    Linker

    In computer science, a linker or link editor is a computer program that takes one ormore object file generated by a compiler and combines them into a single executable program....
  • Loader (computing)
    Loader (computing)

    In computing, a loader is the part of an operating system that is responsible for loading programs from executables into memory, preparing them for execution and then executing them....
  • Object file
    Object file

    In computer science, object code, or an object file, is the representation of code that a compiler or assembler generates by processing a source code file....
  • Shared library
  • Static library
    Static library

    In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable file....


External links

  • on MSDN
  • on MSDN
  • on MSDN
  • on Microsoft support site
  • on MSDN
  • on www.functionx.com. Tutorial for making and using DLLs
  • on www.codemaestro.com.
  • in Windows Vista