Dynamic library calling and other technologies

Source: Internet
Author: User
Tags microsoft c
Large applications Program These modules are composed of many modules that complete relatively independent functions and work together to complete the work of the entire software system. When constructing a software system Source code If all files are statically compiled into the EXE file of the entire application, some problems will occur: one drawback is that it increases the size of the application and occupies more disk space, when the program runs, it also consumes a large amount of memory space, resulting in a waste of system resources. Another drawback is that when writing a large exe program, you must adjust all the source code during each modification and reconstruction. Code It increases the complexity of the compilation process and is not conducive to periodic unit tests. In addition, some modules may have more common functions and will still be used when constructing other software systems.

On the Windows system platform, a completely different effective programming and running environment is provided. You can create an independent program module as a smaller dynamic link library (Dynamic linkable library, DLL files and can be compiled and tested separately. During runtime, the system will load the EXE program to the memory space only when it does need to call these DLL modules. This method not only reduces the size of the EXE file and the need for memory space, but also enables these DLL modules to be used by multiple applications at the same time.

Dynamic Link Library Overview

Dynamic Link Library is one of the most important implementation technologies of windows. Many new features and features of windows are implemented through DLL. In fact, Windows itself is composed of many DLL, its most basic three major component modules kernel, GDI and user are DLL.

In general, a DLL is a disk file. dll,. DRV,. Fon,. sys, and many system files with. EXE extension can be DLL. It consists of global data, service functions, and resources. It is loaded into the virtual space of the process at runtime and becomes part of the calling process. If there is no conflict with other DLL, the file is usually mapped to the same address of the virtual space of the process. The DLL module contains various export functions to provide external services. When Windows loads the DLL module, it matches the process function call with the export function of the DLL file. Dll can have its own data segment, but does not have its own stack. The stack memory required by the DLL module is allocated from the stack of the running process, use the same stack mode as the application that calls it; a DLL has only one instance in the memory; DLL implements code encapsulation; DLL compilation and specific Programming Language And the compiler.

Classification of Dynamic Link Libraries

Microsoft's Visual C ++ supports three types of DLL: Non-mfc dll (non-MFC dynamic library), regular DLL (conventional DLL), and extension DLL (Extended DLL ).

1. Non-mfc dll (non-MFC dynamic library)

This dynamic link library refers to a DLL written in C language directly without the MFC class library structure. The exported function is a standard C interface, it can be called by applications not compiled by MFC or MFC. If you do not need to use mfc for the created DLL, you should create a non-mfc dll, because the use of MFC will increase the size of your library, thus wasting your disk and memory space.

2. Regular DLL (conventional DLL)

Similar to the extension DLL below, this dynamic link library is written using the MFC class library. One obvious feature of this dynamic link library is that there is a class that inherits cwinapp in the source file (note: although such DLL is derived from cwinapp, there is no message loop ), the exported function is a C function, C ++ class, or C ++ member function (do not confuse the C ++ class with the Microsoft C ++ class of MFC ), applications that call conventional DLL do not need to be MFC applications, as long as they can call C-like functions, they can be used to develop applications in Visual C ++, Delphi, Visual Basic, Borland C, and other compiling environments. Conventional dll can be subdivided into static link to MFC and dynamic link to MFC:

(1) the dynamic connection library for static connection to MFC is only supported by VC professional and Enterprise Edition. The output functions in this type of DLL can be used by any Win32 program, including the application using MFC. The output function is in the following format:

Extern "C" Export yourexportedfunction ();

Without the extern "C" modifier, the output function can only be called from the C ++ code.

(2) the output functions dynamically linked to the Common DLL of MFC can be used by any Win32 program, including the applications using MFC. All functions output from the DLL should start with the following statement:

Afx_manage_state (afxgetstaticmodulestate ())

This statement is used to correctly switch the status of the MFC module.

3. extension DLL (Extended DLL)

This dynamic link library is created using the dynamic link version of MFC, and is called only by applications written in the MFC class library. For example, you have created a derived class from the ctoolbar class of MFC to create a new toolbar. to export this class, you must put it in an extended DLL of MFC. Extension DLL is different from conventional DLL. It does not have a class Object inherited from cwinapp. Therefore, developers must add initialization code and end code to the dllmain function in the DLL. Compared with conventional DLL, the extended dll has the following differences:

1) it does not have an object derived from cwinapp;

2) It must have a dllmain function;

3) When dllmain calls the afxinitextensionmodule function, it must check the return value of this function. If 0 is returned, dllmmain also returns 0;

4) if you want to output a cruntimeclass object or resource, you must provide an initialization function to create a cdynlinklibrary object. In addition, it is necessary to output the initialization function;

5) The MFC application using the extension DLL must have a class derived from cwinapp, And the extension DLL initialization function is generally called in initinstance.

Dynamic Connection Library Creation

1. Non-mfc dll Creation

Each dll must have an entry point, just like an application written in C, there must be a winmain function. In non-mfc dll, dllmain is a default entry function. You do not need to write your own DLL entry function, with this default entry function, the dynamic link library can be correctly initialized when called. If the application DLL needs to allocate additional memory or resources, or you need to initialize and clear each process or thread, you need. in the CPP file, the dllmain () function is written in the following format.

Bool apientry dllmain (handle hmodule, DWORD ul_reason_for_call, lpvoid lpreserved)
{
Switch (ul_reason_for_call)
{
Case dll_process_attach:
.......
Case dll_thread_attach:
.......
Case dll_thread_detach:
.......
Case dll_process_detach:
.......
}
Return true;
}

In the parameter, hmoudle is a handle that points to itself when the dynamic library is called (in fact, it is a selector pointing to the _ dgroup segment );
Ul_reason_for_call indicates the reason why a dynamic library is called. When a process or thread loads or detaches a Dynamic Linked Library, the operating system calls the entry function, it also describes the reason why the dynamic link library is called. All its possible values are:

(1) dll_process_attach: The process is called or called Load Library. The DLL is linked to the address space of the current process and initialized;

(2) dll_thread_attach: the current process creates a new thread, and the DLL is called in the new thread body;

(3) dll_process_detach: the process that calls the DLL is terminated and the DLL is uninstalled;

(4) dll_thread_detach: the thread that calls the DLL is terminated and the DLL is uninstalled;

Lpreserved is a reserved parameter.

If you add a function, variable, C ++ class, or other function to the DLL, you can call the keyword _ declspec (dllexport) of VC ).

2. Create regular DLL and extension DLL in the MFC Appwizard [DLL] Mode

There are three methods to generate a DLL file under the MFC Appwizard [DLL]: static link to the common DLL of the MFC, dynamic link to the common DLL of the MFC, and MFC extension DLL. when creating the DLL is, select the DLL creation method based on the actual situation.

The difference between conventional DLL with static link to MFC and conventional DLL with static link to MFC is that the former uses the static Link Library of MFC and the generated DLL file is of a large length. This method is generally not used; the latter uses the dynamic link library of MFC, And the generated DLL file length is small. All the functions output by dynamic link to the conventional DLL of MFC should start with the following statement:

Afx_manage_state (afxgetstaticmodulestate () // This statement is used to correctly switch the status of the MFC module.

The MFC extension DLL is used to create a derived class of MFC. It is called only by applications written in the MFC class library. Extension DLLs is different from regular DLLs. It does not have a class Object inherited from cwinapp. the compiler uses the DLL entry function dllmain () as the initialization of the DLL, you can implement initialization in this function. The Code is as follows:

Bool winapi apientry dllmain (hinstance hinstdll, DWORD reason, lpvoid flmpload)
{
Switch (reason)
{
............... // Initialization code;
}
Return true;
}

The hinstdll parameter stores the DLL handle. The reason parameter specifies the reason for calling the function. lpreserved is a parameter reserved by the system. The implicit link is a non-zero value, and the explicit link value is zero.

Dynamic Connection Library call

There are two types of dynamic link library calls: one is implicit call and the other is display call.

1. implicit call

This call method needs to be generated when a dynamic Connection Library is generated. the LIB file is added to the project of the application. When using the functions in the DLL, you only need to describe it and then you can directly call the DLL output function through the function name, the calling method is the same as other functions in the program. Implicit call does not need to call Load Library () and free library (). When a programmer creates a DLL file, the link program automatically generates a corresponding lib import file. This file contains the symbolic name and optional Identification Number of each DLL export function, but does not contain the actual code. The LIB file is compiled into the application project as an alternative DLL file.

When programmers compile an application by implicit calling, the calling functions in the application match the exported symbols in the Lib file, and these symbols or identification numbers are written to the generated EXE file. The LIB file also contains the corresponding DLL file name (but not the full path name), and the link program also stores it inside the EXE file. When a DLL file needs to be loaded while the application is running, Windows will find and load the DLL based on the information, and then use the symbolic name or identification number to achieve dynamic links to the DLL function. All DLL files called by the application will be loaded into the memory when the application EXE file is loaded.

2. explicit call

This call method is to explicitly call your own dynamic Connection Library in the application using the load library or the afxloadlibrary provided by MFC, and specify the dll path as the parameter. Loadlibary returns the hinstance parameter, which is used by the application to call the getprocaddress function. After importing the dynamic link library, use getprocaddress () to obtain the function to be introduced. This function converts the symbol name or ID number to the internal address of the DLL, then you can call the introduced function just like using the UDF of this application. Before exiting the application, use the afxfreelibrary provided by free library or MFC to release the dynamic Connection Library.

Explicit calling allows programmers to determine when or not to load DLL files, the operating system does not need to load all the DLL referenced by the application to the memory when loading the application, as long as it loads it when using a DLL, in this way, the time used by the application during initial loading and the memory consumption can be reduced. During the DLL loading process, Windows will follow the search order below to locate the DLL:

① Directory containing the EXE file;

② Current working directory of the process;

③ Windows System directory;

④ Windows Directory;

⑤ A series of directories in the PATH environment variable.

Summary

Using dynamic link library (DLL) in Windows has many advantages. The most important thing is that multiple applications, or even applications written in different languages, can share a DLL file, the real implementation of resource "sharing" greatly reduces the application code execution and makes more effective use of the memory; another advantage of using DLL files as a separate program module, encapsulation and independence are good. When the software needs to be upgraded, the developer only needs to modify the corresponding DLL file. Moreover, if the parameter is not modified after the function in the DLL changes, the program code does not need to be re-compiled. This is very useful in programming and greatly improves the efficiency of software development and maintenance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.