Chapter 21: Dynamic Link Libraries


Overview

Dynamic link libraries are compiled files very similar to the standard applications that you've been creating so far. Like standard applications, dynamic link libraries can contain routines, resources, or both, but they cannot be directly executed. Dynamic link libraries are meant to be used by other applications at run time.

Whether you know it or not, you've been using dynamic link libraries for some time now, ever since you called your first Windows API function. The entire Win32 API is a huge collection of dynamic link libraries, most of which are stored in the operating system's System (non-NT) or System32 (NT) directory. Among the most important dynamic link libraries are certainly kernel32.dll, gdi32.dll, user32.dll, shell32.dll, and comctl32.dll.

Just as you can compare dynamic link libraries to standard applications, you can compare them to Delphi units. The major difference between the two is that the routines contained in a unit are compiled into each executable in which they are used, whereas routines compiled in a dynamic link library only reside in that library and are called from outside applications. By having a single compiled copy of a routine, you save both disk space and memory at run time, especially if the user runs multiple copies of your application or if the user runs several applications that use the same dynamic link library.

DLLs also enable you to more efficiently reuse your common routines. If you change the implementation of a routine that resides in a unit, you'll have to recompile all applications that use that routine. If you add the same routine to a DLL, you'll be able to change the implementation of the routine without recompiling all applications; you only need to recompile the DLL. The calling applications will be able to use the updated routine as long as you don't change the routine's interface. If you change the routine's interface, you'll have to recompile all calling applications.

Like everything else, dynamic link libraries have advantages and disadvantages. Here are the advantages of dynamic link libraries:

  • You can share common routines among several applications.

  • You can save memory and disk space by maintaining a single compiled copy of the routine.

  • You can enable other developers to use your application logic without giving them your source code.

  • You can reuse your Delphi routines in other programming languages (or the other way around), as long as you mark the exported routines with the stdcall directive. (If you omit the stdcall directive, you'll be able to use the DLL in Delphi applications only.)

  • You can more easily update/upgrade applications that use one or more dynamic link libraries.

  • You can easily create multi-language applications.

  • You can create resource-only dynamic link libraries that contain commonly used images, icons, sounds, and animations (e.g., moricons.dll in the Windows\System directory).

And here are the disadvantages of using DLLs in Delphi:

  • You can't normally pass a Delphi string to a DLL (you can if you use the additional borlandmm.dll, which is something most developers don't want to do).

  • If you want to pass strings to a DLL, you can pass ShortStrings (which is mostly not good because of the 255-character limit) or PChars (which is better, but you have to handle the memory yourself).

Now it's time to create our first DLL. As you'll see in a moment, it's actually harder to use a DLL than it is to create it.



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net