当前位置:网站首页>12. Dynamic link library, DLL
12. Dynamic link library, DLL
2022-07-05 08:49:00 【Full stack programmer webmaster】
Dynamic link libraries usually cannot run directly , Can't receive messages . They're separate files , It includes executable programs or other DLL Call a function to do something . Only when other modules call functions in the dynamic link library , It works .
1、 We can put functions that perform certain functions in a DLL , Provided to other function calls .Windows API All the functions in are contained in DLL in , Three of the most important :
1)Kernal32.dll Include those for memory management , Functions of processes and threads , Such as CreateThread function .
2)User32.dll Includes performing user interface tasks , Functions such as window creation and message transmission , Such as 3)CreateWindow function .
3)GDI32.dll A function that displays text for drawing .
2、 Static library and dynamic library :
Static library :
Functions and data are compiled into a binary file ( Usually the extension is .LIB). In the case of using static libraries , When compiling linked executables , The linker copies these functions and data from the library and combines them with other modules of the application to create the final executable . There is no need to publish the used static library when publishing products .
Dynamic library :
Dynamic libraries generally provide two files : A lead in warehouse (.lib), One is DLL(.dll) file ..lib The file contains this DLL Symbolic names of exported functions and variables , and .dll The file contains the DLL Actual functions and data . When using dynamic library , When compiling linked executables , Just link to the DLL Import library file , The DLL The function code and data in are not copied to the executable , Until the executable runs , To load the required DLL, Will be DLL Map to the address space of the process , And then visit DLL Functions exported in . Release products at the same time DLL.
Icon P703 Two processes access the same DLL Situation of time
Two kinds of loading dll How to file , Implicit loading and explicit loading .
3、Dumpbin command
If an application wants to access a DLL The function in , Then the function must be an exported function . It can be used Dumpbin see . You may need to pass VCVARS32.bat To establish the VC Environment variables used .
dumpbin -exports name.dll
4、 export DLL Middle function
Add _declspec(dllexport) In front of the function .
1) Implicitly load
(1) adopt extern Declare external functions . And enter lib Symbol name file . hold dll Document and lib The file is placed in the specified directory in the VC++ The catalog contains .
(2) utilize _declspec(dllimport) Before the external function is declared .
For convenience dll Use , It usually provides a .h Documents for customers ; We usually handle the function declaration in the header file as follows :
#ifdef DLL1_API
#else
#define DLL1_API _declspec(dllimport)
#endif
DLL1_API int _stdcall add(int a,int b);
And in the function definition .cpp In file , The following form defines :
#define DLL1_API extern "C" _declspec(dllexport)
#include "Dll1.h"
#include <Windows.h>
#include <stdio.h>
int _stdcall add(int a,int b)
{
return a+b;
}
In this way, no additional code changes are required when providing to customers .
Again , You can define the above DLL1 Add in front of a class , Or class member function to export the class or class member function . As shown below :
class /*DLL1*/ Point
{
public:
void DLL1_API output(int x,int y);
void test();
};
2) Explicitly load
adopt LoadLibrary To achieve .LoadLibrary Map the specified executable module to the address space of the calling process ,and the return value is a handle to the module. Re pass GetProcAddress To get the address of the exported function in the dynamic library .
HINSTANCE hInst;
hInst=LoadLibrary("Dll3.dll");
typedef int (/*_stdcall*/ *ADDPROC)(int a,int b);
//ADDPROC Add=(ADDPROC)GetProcAddress(hInst,"[email protected]@[email protected]");
ADDPROC Add=(ADDPROC)GetProcAddress(hInst,"add");
//ADDPROC Add=(ADDPROC)GetProcAddress(hInst,MAKEINTRESOURCE(1));
The MAKEINTRESOURCE macro converts an integer value to a resource type
compatible with the resource-management functions. This macro is used in place of a
string containing the name of the resource
The DllMain function is called when the driver DLL first starts up.
After use , adopt FreeLibrary To reduce the load dll Reference count of , When it comes to 0 when , The DLL The module is unloaded from the address space of the calling process .
5、 Name adaptation
Different compilers compile functions , And when using different function calling conventions , The adaptation of functions is different .
http://www.cnblogs.com/mydomain/archive/2010/09/27/1837179.html
thus , There will be some problems when calling functions , If used C++ The functions generated by the compiler pass C There will be an error when the compiler calls . In this case , You can add extern “C”
#define DLL1_API extern “C” _declspec(dllimport)
utilize extern “C” Can solve C++,C Inter call problem , But there is one flaw , It can't be used to export member functions of a class ., It can only be used to export global functions .
If the calling convention of the function changes , If some functions use _stdcall, Some use pascal, So even if you use extern “C”, Name adaptation will still happen .
You can define a module definition file .def The way to solve this problem :
Create a new one newname.def file , Join the project :
LIBRARY Dll2
EXPORTS
add
subtract
Dll2 The name is the same as the generated dynamic link library .
EXPORTS: If the symbol name defined below is the same as the function name , Then export the function with a symbolic name , If different , Export functions according to the following rules :
entryname[=internalname] [@ordinal[NONAME]] [DATA] [PRIVATE]
There are three methods for exporting a definition, listed in recommended order of use:
1.The __declspec(dllexport) keyword in the source code
2.An EXPORTS statement in a .def file
3.An /EXPORT specification in a LINK command[1]
Reference resources :
[1] http://msdn.microsoft.com/en-us/library/hyx1zcd3.aspx
[2] VC++ Go into detail
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/110679.html Link to the original text :https://javaforall.cn
边栏推荐
猜你喜欢
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
[牛客网刷题 Day4] JZ35 复杂链表的复制
Old Wang's esp8266 and old Wu's ws2818 light strip
猜谜语啦(8)
Solutions of ordinary differential equations (2) examples
IT冷知识(更新ing~)
微信H5公众号获取openid爬坑记
319. Bulb switch
深度学习模型与湿实验的结合,有望用于代谢通量分析
Guess riddles (7)
随机推荐
Basic number theory -- Euler function
猜谜语啦(6)
Halcon shape_ trans
Yolov4 target detection backbone
Meta标签详解
Apaas platform of TOP10 abroad
特征工程
319. Bulb switch
Ros-11 common visualization tools
Typescript hands-on tutorial, easy to understand
Xrosstools tool installation for X-Series
Halcon snap, get the area and position of coins
Programming implementation of ROS learning 6 -service node
How many checks does kubedm series-01-preflight have
ROS learning 4 custom message
Beautiful soup parsing and extracting data
Business modeling | process of software model
Redis实现高性能的全文搜索引擎---RediSearch
Task failed task_ 1641530057069_ 0002_ m_ 000000
696. 计数二进制子串