1. Dynamic Link Library Overview:
- dynamic-link libraries do not usually run directly or accept messages; it only works when other modules invoke functions in the dynamic-link library.
- All functions in the Windows API are included in the dynamic link library.
- Dynamic link library is divided into static library and dynamic library.
2, the creation of dynamic link library:
The function to be exported is in the form:
extern int Add (intint b) { return a +b; }
To solve the name adaptation problem, you can add a module definition file for the project. def
LIBRARY MFC-addexports add
The classes to be exported are in the form of:
class _declspec (dllexport) point{public: void OutPut (intint y);};
Generated after compilation: a DLL (. dll) file, an ingest library (. lib) file, and so on
Note: If you specify an export flag when declaring a class, all functions in the class are exported, otherwise only the class member functions that have the export flags specified when they are declared are exported;
3, dynamic link library loading:
(1) Implicit loading
Use extern or _declspec (dllimport) to declare an external function (preferably write this declaration in a header file *.h)
int Add (intint b);
Import the corresponding ingest library (. lib) file: Link the Lib library in the project settings or use the Code link Lib library in the source file:
#pragma comment (lib, "*.lib")
Add the directory where the dynamic link library file is located to the environment variable in the system: path
(2) Explicit loading
Maps the specified executable module to the address space of the calling process
#include <Windows.h>= LoadLibrary (_t (". \\.. \\MFC-Add\\Debug\\MFC-Add.dll"));
Gets the address of the exported function in the dynamic link library
typedef int (_stdcall *addproc) (int a,int b); // declares that message is a pointer type to a function, the function returns a value of type int; _stdcall is the standard calling convention Addproc Addproc; Addproc = (addproc) GetProcAddress (handlerdll,_t ("add"));
Calling the Export function
Addproc (5,6);
Release a reference to a DLL when you do not need to access the DLL
FreeLibrary (".. \\.. \\MFC-Add\\Debug\\MFC-add. dll");
Note:
- Dumpbin and Depends Tools
- calling convention: _stdcall: The standard calling convention is the WINAPI calling convention, which is the Pascal calling convention, the non-C calling convention
- Name adaptation: the C + + compiler will name the exported function when it builds the DLL, and the different compilers use different adaptation rules
C + + dynamic link library