Tag Archives: program innovation

Dynamic Link Library (DLL) and C (Run time)

Before we start about Dynamic Link Library (DLL), there are few question which pop-up in my mind are:
  1. What does Library means in Computer?
  2. If there is any Library available then how to create or maintain it?
  3. Does the Library file is portable on Cross Platform?

What does Library means in Computer?

In English the Library means a collection of books, a place where a large number of books are placed and in Computer it means the collections of functions. With these functions any body can read and write other functions. The use of Library files are mostly done by IDE Development Environment, as which these Libraries provide the static structure to all same perfectness and can be used simultaneously by many programs such as Library used by many people’s.

History about Library

The earliest programming concepts of libraries were intended to separate data definitions from the program implementation. “COMPOOL” (Communication Pool) concept to popular attention in 1959, although it adopted the idea from the large-system SAGE software.
Another major contributor to the modern library concept came in the form of the sub-program innovation of FORTRAN. FORTRAN sub-programs can be compiled independently of each other, but the compiler lacks a linker. So prior to the introduction of modules in Fortran-90, type checking between sub-programs was impossible.
Finally, historians of the concept should remember the influential Simula 67. Simula was the first object-oriented programming language, and its classes are nearly identical to the modern concept as used in Java, C++, and C#. The class concept of Simula was also a progenitor of the package in Ada and the module of Modula-2. Even when developed originally in 1965, Simula classes could be included in library files and added at compile time.

If there is any Library available then how to create or maintain it

Libraries contain code and data that provide services to independent programs. This encourages the sharing and changing of code and data in a modular fashion, and eases the distribution of the code and data. Some executables are both standalone programs and libraries, but most libraries are not executable. Executables and libraries make references known as links to each other through the process known as linking, which is typically done by a linker. Most compiled languages have a standard library although programmers can also create their own custom libraries. Most modern software systems provide libraries that implement the majority of system services. Such libraries have commonality services which a modern application requires. As such, most code used by modern applications is provided in these system libraries.
Some programming languages may use a feature called smart linking wherein the linker is aware of or integrated with the compiler, such that the linker knows how external references are used, and code in a library that is never actually used, even though internally referenced, can be discarded from the compiled application. For example, a program that only uses integers for arithmetic, or does no arithmetic operations at all, can exclude floating-point library routines. This smart-linking feature can lead to smaller application file sizes and reduced memory usage.

Does the Library file is portable on Cross Platform?

So, we can say that Libraries are important in the program linking or binding process, which resolves references known as links or symbols to library modules. The linking process is usually automatically done by a linker or binder program that searches a set of libraries and other modules in a given order. Usually it is not considered an error if a link target can be found multiple times in a given set of libraries. Linking may be done when an executable file is created, or whenever the program is used at run time.
When linking is performed during the creation of an executable or another object file, it is known as static linking or early binding. In this case, the linking is usually done by a linker, but may also be done by the compiler. A static library, also known as an archive, is one intended to be statically linked. Originally, only static libraries existed. Static linking must be performed when any modules are recompiled.
For Cross Platform, it is dependable on Compiler, if the Compiler supports the cross-platform Compiling then the Library Created in One Platform is used on Another Platform. Generally, this feature is available in every compiler but not used to make the compiler the O/S specific but with changing some of the options, one can compile the program for cross Platform. The best Example of this is C Language Programs the programs such as MySQL, PHP, Apache are used as cross-platform and Libraries created by them are also portable to cross-platform but certainly a tweak is required to generate that type of code and it has many Rules. To know about rules kindly refer to Manual of your Compiler.

Differences Between Applications and DLLs

Even though DLLs and applications are both executable program modules, they differ in several ways. To the end-user, the most obvious difference is that DLLs are not programs that can be directly executed. From the system’s point of view, there are two fundamental differences between applications and DLLs:
  1. An application can have multiple instances of itself running in the system simultaneously, whereas a DLL can have only one instance.
  2. An application can own things such as a stack, global memory, file handles, and a message queue, but a DLL cannot.

Advantages of Using DLLs

  1. Saves disk space. Many applications can share a single copy of the DLL on disk. In contrast, each application built with a static link library has the library code linked into its executable image as a separate copy.
  2. Saves memory and reduces swapping. Many processes can use a single DLL simultaneously, sharing a single copy of the DLL in memory. In contrast, Windows must load a copy of the library code into memory for each application that is built with a static link library.
  3. Upgrades to the DLL are easier. When the functions in a DLL change, the applications that use them do not need to be recompiled or re-linked as long as the functions’ arguments and return values do not change. In contrast, statically linked object code requires that the application be re-linked when the functions change.
  4. Provides after-market support. For example, a display driver DLL can be modified to support a display that was not available when the application was shipped.
  5. Supports multi language programs. Programs written in different programming languages can call the same DLL function as long as the programs follow the function’s calling convention. The programs and the DLL function must be compatible in the following ways: the order in which the function expects its arguments to be pushed onto the stack, whether the function or the application is responsible for cleaning up the stack, and whether any arguments are passed in registers.
  6. Provides a mechanism to extend the MFC library classes. You can derive classes from the existing MFC classes and place them in an MFC extension DLL for use by MFC applications.
  7. Eases the creation of international versions. By placing resources in a DLL, it is much easier to create international versions of an application. You can place the strings for each language version of your application in a separate resource DLL, and have the different language versions load the appropriate resources.

Dis-advantage

A potential disadvantage to using DLLs is that the application is not self-contained; it depends on the existence of a separate DLL module.

Linking an Executable to a DLL

An executable file links to (or loads) a DLL in one of two ways:
  1. Implicit linking.
  2. Explicit linking.
Implicit linking is sometimes referred to as static load or load-time dynamic linking. Explicit linking is sometimes referred to as dynamic load or run-time dynamic linking. With implicit linking, the executable using the DLL links to an import library (.LIB file) provided by the maker of the DLL. The operating system loads the DLL when the executable using it is loaded. The client executable calls the DLL’s exported functions just as if the functions were contained within the executable.
With explicit linking, the executable using the DLL must make function calls to explicitly load and unload the DLL, and to access the DLL’s exported functions. The client executable must call the exported functions through a function pointer. An executable can use the same DLL with either linking method. Furthermore, these mechanisms are not mutually exclusive, as one executable can implicitly link to a DLL and another can attach to it explicitly.

Using Linking Implicitly

To implicitly link to a DLL, executables must obtain the following from the provider of the DLL:
A header file (.H file) containing the declarations of the exported functions and/or C++ classes.
An import library (.LIB files) to link with. The linker creates the import library when the DLL is built.

The actual DLL (.DLL file).

Executable’s using the DLL must include the header file containing the exported functions (or C++ classes) in each source file that contains calls to the exported functions. From a coding perspective, the function calls to the exported functions are just like any other function call. To build the calling executable file, you must link with the import library. If you are using an external Makefile, specify the file name of the import library where you list other object (.OBJ) files or libraries that you are linking with. The operating system must be able to locate the .DLL file when it loads the calling executable.

Using Linking Explicitly

With explicit linking, applications must make a function call to explicitly load the DLL at run time. To explicitly link to a DLL, Call LoadLibrary() (or a similar function) to load the DLL and obtain a module handle and Call GetProcAddress() to obtain a function pointer to each exported function that the application wants to call. Because applications are calling the DLL’s functions through a pointer, the compiler does not generate external references, so there is no need to link with an import library. Call FreeLibrary() when done with the DLL.
Tagged , , ,