Google
 
   
Login
Username:

Password:


Lost Password?

Register now!
Search
Main Menu
top books
Polls
What do you think about php-deluxe.net?
Excellent!
Cool
Hmm..not bad
What the hell is this?
encyclopedia
recommendation
compare webbrowser
Freenet DSL
Who's Online
3 user(s) are online (3 user(s) are browsing encyclopedia)

Members: 0
Guests: 3

more...
browser tip
Unix Befehle
manual of unix befehle
recommendation!
Sponsored
partner

Dynamic-Link Library

Dynamic-Link Library (DLL) is the kind of dynamically loaded library (computer science) used in Microsoft Windows. These libraries usually have the file extension DLL, OCX (for libraries containing ActiveX controls), or ICL (for libraries containing only icon (computing)s).

The file formats for DLLs are same as for Windows EXE files. As with EXEs, DLLs can contain code (computer programming), data (computing), and resource (Windows)s, in any combination. For example, ICLs don t contain code at all.

= Background =

The initial purpose for DLLs was saving both disk space and memory required for applications. Any code which many applications share could be separated into a DLL which only exists as single disk file, and single instance in memory. Extensive use of DLLs allowed early versions of Windows to work under tight memory conditions.

Another benefit of using DLLs is modularity; whenever a change is to be made in a piece of code shared by several applications, only the DLL file is to be fixed all the applications remain unaffected. This allows software vendors to release relatively compact service packs for large applications, such as Microsoft Office, Microsoft Visual Studio, or even Windows itself.

Yet another benefit of the modularity achieved by using DLLs is the concept of plug-ins; new modules can be written for pre-existing applications and seamlessly integrated into them without modifications to the applications themselves. This concept of dynamic extensibility is taken to the extreme with ActiveX.

With this many benefits, using DLLs also has a drawback: the DLL hell, when several applications conflict on which exactly version of a shared DLL library is to be used. Such conflicts can usually be resolved by placing the different versions of the problem DLL into the applications folders, rather than a system-wide folder; however, this also nullifies the savings provided by using shared DLLs. Currently, Microsoft .NET is targeted as a solution to the problem of DLL hell by allowing side-by-side coexistence of different versions of a same shared library. With modern computers which have loads of disk space and memory, it can be a reasonable approach.

= Programming examples =

== Working with DLLs in Visual C Plus Plus ==

=== Creating a DLL ===

DLL interfaces are configured by exporting functions using the __declspec(dllexport) attribute, as shown in the following example: #include // Export this function extern C __declspec(dllexport) double AddNumbers (double a, double b); // DLL initialization function BOOL APIENTRY DllMain (HANDLE hModule, DWORD dwReason, LPVOID lpReserved) { return TRUE; } // Function that adds two numbers double AddNumbers (double a, double b) { return a + b; } This example will produce both DLL and LIB files when compiled.

=== Using a DLL ===

DLL functions can be called easily by first importing them using the __declspec(dllimport) attribute, as shown in the following example: #include #include // Import function that adds two numbers extern C __declspec(dllimport)double AddNumbers (double a, double b); void main () { double result = AddNumbers(1, 2); printf( The result was: %f , result); } Remember to link using the LIB file, and run with the DLL file in path.

== Working with DLLs in Delphi programming language ==

=== Creating a DLL ===

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. library Example; // Function that adds two numbers function AddNumbers(a, b: Double): Double; cdecl; begin AddNumbers := a + b end; // Export this function exports AddNumbers; // DLL initialization code: no special handling needed begin end.

=== Using a DLL ===

Delphi doesn t require LIB files to import functions from DLLs. To link to a DLL, external keyword is used in function declaration: program Example; {$APPTYPE CONSOLE} // Import function that adds two numbers function AddNumbers(a, b: Double): Double; cdecl; external Example.dll ; var result: Double; begin result = AddNumbers(1, 2); Writeln( The result was: , result) end.

== Run-time linking ==

DLL files can be loaded in two different ways, either during application startup ( see examples above ), or at run-time using the LoadLibrary, GetProcAddress and FreeLibrary API functions. The procedure for run-time linking is the same in any language, since it depends on Windows API rather than language constructs. The following example uses VC++ syntax to illustrate run-time linking: #include #include // DLL function signature typedef double (*AddNumbers)(double, double); void main () { AddNumbers function; double result; // Load DLL file HINSTANCE hinstLib = LoadLibrary( MyDll.dll ); if (hinstLib) { // Get function pointer function = (AddNumbers) GetProcAddress(hinstLib, AddNumbers ); // Call function. if (function) result = (function) (1,2); // Unload DLL file BOOL fFreeResult = FreeLibrary(hinstLib); } // Display result if (!hinstLib || !function) printf( ERROR: unable to call DLL function ); else printf( The result was: %f , result); } LIB file is not required in this case. The DLL file must however still remain in the path.

Note that with load-time linking, 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 necessary DLL files if he uses load-lime linking. On the other hand, with run-time linking he has the opportunity to provide a graceful fall-back facility.

=== Working with DLLs in Visual Basic ===

In VB, only run-time linking is supported; but in addition to using LoadLibrary/GetProcAddress APIs, declarations of imported functions are allowed. 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

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.

= See also =

  • Dependency walker, a nice utility which displays exported and imported functions of DLL and EXE files.
  • = External links =

  • [http://msdn.microsoft.com/library/default.aspurl=/library/en-us/vclang/html/_pluslang_the_dllexport_and_dllimport_attributes.asp dllexport, dllimport] on MSDN
  • [http://www.functionx.com/visualc/libraries/win32dll.htm Win32 DLL] on www.functionx.com. Tutorial for making and using DLL s