Static vs Dynamic Dynamic Link Library Loading

When to Use Static and Dynamic DLL Loading

Static vs. Dynamic DLL Loading
Static vs. Dynamic DLL Loading.

A DLL (Dynamic Link Library) acts as a shared library of functions that can be called upon by numerous applications and other DLLs. Delphi lets you create and use DLLs so that you can call these functions at will. However, you must import these routines before you can call them.

Functions exported from a DLL can be imported in two ways - either by declaring an external procedure or function (static) or by direct calls to DLL specific API functions (dynamic).

Let's consider a simple DLL. Below is the code for "circle.dll" exporting one function, called "CircleArea," which calculates the area of a circle using the given radius:

 library circle;
 
 uses
 SysUtils, Classes, Math;
 
 {$R *.res}
 
 function CircleArea(const radius : double) : double; stdcall;
 begin
 result := radius * radius * PI;
 end;
 
 exports CircleArea;
 
 begin
 end.
 

Once you have the circle.dll, you can use the exported "CircleArea" function from your application.

Static Loading

The simplest way to import a procedure or function is to declare it using the external directive:

 function CircleArea(const radius : double) : double; external 'circle.dll';
 

If you include this declaration in the interface part of a unit, circle.dll is loaded once when the program starts. Throughout execution of the program, the function CircleArea is available to all units that use the unit where the above declaration is.

Dynamic Loading

You can access routines in a library through direct calls to Win32 APIs, including LoadLibrary, FreeLibrary, and GetProcAddress. These functions are declared in Windows.pas.

Here's how to call the CircleArea function using dynamic loading:

 type
 TCircleAreaFunc = function (const radius: double) : double; stdcall;
 var
 dllHandle : cardinal;
 circleAreaFunc : TCircleAreaFunc;
 begin
 dllHandle := LoadLibrary('circle.dll') ;
 if dllHandle <> 0 then
 begin
 @circleAreaFunc := GetProcAddress(dllHandle, 'CircleArea') ;
 if Assigned (circleAreaFunc) then
 circleAreaFunc(15) ; //call the function
 else
 ShowMessage('"CircleArea" function not found') ;
 FreeLibrary(dllHandle) ;
 end
 else
 begin
 ShowMessage('circle.dll not found / not loaded') ;
 end;
 end;
 

When importing using dynamic loading, the DLL is not loaded until the call to LoadLibrary. The library is unloaded by the call to FreeLibrary.

With static loading, the DLL is loaded and its initialization sections execute before the calling application's initialization sections are executed. This is reversed with dynamic loading.

Should You Use Static or Dynamic?

Here's a simple look at the advantages and disadvantages of both static and dynamic DLL loading:

Static Loading

Pros:

  • Easier for a beginner developer; no "ugly" API calls
  • DLLs are loaded just once, when the program starts

Cons:

  • The application will not start if any DLLs are missing or can not be found. An error message like this will appear: "This application has failed to start because 'missing.dll' was not found. Re-installing the application may fix this problem".

    By design, the DLL search order with static linking includes the directory from which the application loaded, the system directory, the Windows directory, and directories listed in the PATH environment variable

    Note also that the search order might be different for various Windows versions.
    Always expect to have all the DLLs in the directory where the calling application is.
  • More memory is used since all DLLs are loaded even if you won't use some of the functions

Dynamic Loading

Pros:

  • You can run your program even when some of the libraries it uses are not present
  • Smaller memory consumption since the DLLs are used only when needed
  • You can specify the full path to the DLL
  • Could be used for modular applications. The application only exposes (loads) modules (DLLs) "approved" for the user
  • The ability to load and unload library dynamically, is the foundation of a plug-in system that allow a developer to add extra functionality to programs
  • Backwards compatibility with older Windows versions in which system DLLs might not support the same functions or be supported in the same way. Detecting the Windows version first, then dynamically linking based on what your app is running on, allows you to support more versions of Windows and provide workarounds for older OSs (or at the very least, gracefully disabling features you can't support)

    Cons:

    • Requires more code, which isn't always easy for a beginner developer
    Format
    mla apa chicago
    Your Citation
    Gajic, Zarko. "Static vs Dynamic Dynamic Link Library Loading." ThoughtCo, Oct. 16, 2017, thoughtco.com/static-vs-dynamic-1058452. Gajic, Zarko. (2017, October 16). Static vs Dynamic Dynamic Link Library Loading. Retrieved from https://www.thoughtco.com/static-vs-dynamic-1058452 Gajic, Zarko. "Static vs Dynamic Dynamic Link Library Loading." ThoughtCo. https://www.thoughtco.com/static-vs-dynamic-1058452 (accessed November 24, 2017).