Science, Tech, Math › Computer Science Static vs Dynamic Dynamic Link Library Loading Share Flipboard Email Print Omar Havana / Getty Images Computer Science Delphi Programming Advanced Delphi Tutorials PHP Programming Perl Python Java Programming Javascript Programming C & C++ Programming Ruby Programming Visual Basic View More By Zarko Gajic Zarko Gajic Twitter Computer Science Expert MSCS, Computer Science, University of Osijek Zarko Gajic is experienced in SQL and has working knowledge of DB systems such as MS SQL Server, Firebird, Interbase, and Oracle. He is also proficient in XML, DHTML, and JavaScript. Learn about our Editorial Process Updated on March 13, 2019 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: 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: 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: 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. Cite this Article Format mla apa chicago Your Citation Gajic, Zarko. "Static vs Dynamic Dynamic Link Library Loading." ThoughtCo, Feb. 16, 2021, thoughtco.com/static-vs-dynamic-1058452. Gajic, Zarko. (2021, February 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 March 22, 2023). copy citation