What is a dynamic link library?

Source: Internet
Author: User
What is DLL:

DLL is just a group Source code Module, each module contains some available Program Or other DLL-called functions. When an application calls a function in a DLL, the operating system maps the DLL file image to the address space of the process, in this way, all the threads in the process can call the functions in the DLL. After the DLL is loaded, the DLL is only attached to some threads in the process. Code And data, the operating system in order to save memory space, the same dll has only one in the memory, that is, if both of your applications need to load user32.dll, then, the operating system will only load user32.dll to the memory once. Because the code segment's permissions in the memory are read-only, when multiple applications load the same DLL, do not worry that the application will modify the DLL code segment. When a thread calls a DLL function, the function will obtain the parameter passed to it in the thread stack, and use the thread stack to store the variable he needs, any object created by the DLL function is owned by the calling thread or calling process. The DLL does not own any object. That is to say, if a function in the DLL calls virtualalloc, the system will reserve an address from the address space of the calling process. Even if the DLL ing is revoked, the calling process's reservation address will still exist until the user cancels the reservation or the process ends. Example code: Mylib. h
1 # Ifdef mylibapi2 # Else3# DefineMylibapi extern "C" _ desclspec (dllimport)4 # Endif5 6MylibapiIntG_nresult;7 8MylibapiIntAdd (IntNleft,IntNright)
Mylib. cpp
 1 # Include <windows. h> 2  3   # Define Mylibapi extern "C" _ declspec (dllexport) 4 # Include "  Mylib. h  "  5   Int  G_nresult;  6   7   Int Add ( Int Nleft, Int  Nright) 8   {  9 G_nresult = nleft + Nright;  10        Return  G_nresult;  11 }
Enter the command Cl/LDD mylib. CPP can generate the DLL for debugging. At this time, four more files are generated, namely mylib. exp, mylib. lib, mylib. DLL, mylib. OBJ mylib. OBJ stores the information required by the linker to generate DLL. When the linker detects that an application exports a function or variable, the linker generates mylib. lib file, which only lists the exported functions and variable symbol names. Enter the command to view the export segment dumpbin-Exports mylib in Lib. lib

 

We can see that this lib export _ add and _ g_nresult. If we use dumpbin-imports mylib. there are no variables or functions in lib imports. This is because only exported functions and variables are recorded in Lib. This file is generated only when an export function or variable is declared.

Mylib. dll is the final generated module. If you use dumpbin to view the export of mylib. dll

 

There are too many imports, so we can use _ desclspec (dllexport) to declare it as an export function when exporting a function, when using a function from a DLL, you can use _ desclspecc (dllimport) to declare a function. Of course, you can also choose not to use the import pre-declaration, however, using improt can clearly tell the compiler that these functions are imported from the DLL. What is the efficiency of exporting? After declaring a function or variable as an export, the compiler will embed some additional information when generating the OBJ so that the linker can use it when generating the DLL, A lib file will be generated to record the exported functions and variables. When generating an executable file, we need to link this lib to obtain some DLL information. When the linker generates the DLL, an exported symbol table is embedded in the DLL file. This symbol table records the names of exported functions and variables, and stores the corresponding file offset address, in this way, when the executable file needs to call the functions in the DLL, you can use this symbol table to find the address of the corresponding function. Finally, we start to build the executable file. The Code is as follows: myexe. CPP # include <cstdio>
# Include "mylib. H"

Int main (void)
{
Int nleft = 10, nright = 20;
Printf ("% d \ n", add (nleft, nright ));
} Cl myexe. CPP mylib. lib we must link mylib during compilation. lib, so that the compiler knows where to find the variables and function information of mylib, And the executable file knows that the program needs mylib. DLL, so that the program will search for the DLL on the user's disk when loading, if not found, it will report an error, if yes, the DLL is mapped to the memory space of the process. When the DLL is mapped to the memory space of the process, the loader will check whether the export segment symbol of the corresponding dll exists. If no, an error is reported. If yes, the loader loads the symbol to the file offset (RVA, virtual address, but in the DLL, it is actually the location of the file where the symbol is located. Add the virtual address loaded by the DLL and save it to the import segment of the executable program, when the code references the import symbol, the executable file will view the import segment and get the address of the import symbol, so that you can access the imported variables or functions, such as the mylib generated by us. the DlLL file can be output using dumpbin:

We can see that the RVA of ADD is 1000. Assume that our DLL is mapped to the address space of 1000 by the application, when the application is executed, the add function will eventually be loaded to 1000 + 1000, that is, after the DLL is mapped to the address space, it is just a bunch of additional code and data for the application.

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.