Chapter 16: Creating Dynamic Link Libraries

When I started to work with Windows, the term Dynamic Link Library (DLL) first seemed strange to my ear, because I was accustomed to MS-DOS. In the opinion of those accustomed to the MS-DOS programming paradigm, a library must be bound at the stage of linking. DLLs seemed similar to the concept of overlay. In MS-DOS, overlays were used to utilize random access memory more economically. An overlay manager loaded individual parts of the overlay and unloaded other parts . It allowed impressive economy of the memory. However, overlay could be used only by a single application. DLLs, on the other hand, can be used by several applications simultaneously hence the term "library".

General Concepts

The use of DLLs is the method of implementing a modular structure at run time. DLLs also simplify the software development process. Instead of recompiling large EXE modules, it is enough to recompile a single DLL. Furthermore, it is possible to simultaneously access a DLL from several executable modules, which makes multitasking more flexible. The most important point is that the structure of a DLL module is practically the same as that of an EXE module. Those who have developed MS-DOS programs must be acquainted with the overlay concept. By its functionality, DLL is similar to overlay; [i] however, its name dynamic link library suits it better.

You have already seen how to define the imported functions when creating EXE modules. For this purpose, it is enough to declare these functions as EXTERN . When creating DLLs, you'll have to declare both imported and exported functions.

To proceed further, it is necessary to revisit the concept of linking. I introduced it earlier when describing the linker operation. During translation, the names declared in the program as external ( EXTERN ) are linked to appropriate names from the declared libraries using the IMPORTLIB directive. This way of linking is known as early, or static, linking. With a DLL, linking takes place at run time. This kind of linking is called late, or dynamic, linking. Dynamic linking can take place automatically at program startup or at any time convenient for the programmer using special application program interface functions. In this case, you are dealing with implicit and explicit linking. These approaches are illustrated in Fig. 16.1. Note also that the use of DLLs allows disk space economy, because the procedure in a library is included only once in contrast to procedures placed into the module from static libraries. [ii]

image from book
Figure 16.1: Concept of linking

In the Windows environment, two linking mechanisms are employed: symbolic names and ordinal numbers . In the first case, the function defined in a library is identified by its name; in the latter case, it is identified by its ordinal number, which must be specified in the course of translation. Linking by ordinal number was used in Windows 3.x. In my opinion, linking by names is more convenient.

DLLs can also contain resources. For example, font files are DLLs containing only resources. It is necessary to point out that when a DLL is loaded into the address space of your process, it becomes a part of your program. Accordingly, the process data are available from the DLL, and DLL data are available for the process.

In every DLL, it is necessary to define the entry point (entry procedure). By default, it is assumed that the entry point is the label specified after the END directive (for example, END START ). When a DLL is loaded or unloaded, the entry procedure is called automatically. Note that the method of loading the DLL (explicit or implicit) doesn't matter, because the DLL will be unloaded from the memory automatically when the process or thread is closed. Principally, the entry point can be used for startup initialization of variables . Quite often, this procedure remains blank. When this procedure is called, it accepts three parameters:

  • First parameterDLL module identifier

  • Second parameterThe reason for the call

  • Third parameterReserved

Consider the second parameter of the entry procedure in more detail. It can take the following values:

 DLL_PROCESS_DETACH   equ 0 DLL_PROCESS_ATTACH   equ 1 DLL_THREAD_ATTACH    equ 2 DLL_THREAD_DETACH    equ 3 
  • DLL_PROCESS_ATTACH Informs the program that the DLL has been loaded into the address space of the calling process.

  • DLL_THREAD_ATTACH Informs the program that the current process creates a new thread. Such messages are sent to all DLLs loaded by the process for that instance.

  • DLL_PROCESS_DETACH Informs the program that the DLL is being unloaded from the address space of the process.

  • DLL_THREAD_DETACH Informs the program that a certain thread created by the current process, into whose the address this DLL has been loaded, is going to be closed.

[i] Overlay means that different parts are alternately loaded into the overlay memory area.

[ii] Generally, it would be more correct to call the libraries used for Windows programming (such as IMPORT32.LIB and USER32.LIB) import libraries instead of static libraries. These libraries do not contain program code. They only contain information used by translators.



The Assembly Programming Master Book
The Assembly Programming Master Book
ISBN: 8170088178
EAN: 2147483647
Year: 2004
Pages: 140
Authors: Vlad Pirogov

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