Introduction to DLL
DLL(Dynamic Link Library) is an executable file that contains functions and data that can be called in other programs. It is an important concept in the Windows operating system for code sharing and modularity.
specificities
- code sharing: Multiple programs can use the same DLL file at the same time, and theNo need to compile its code into each program. This saves disk space and memory, and simplifies updating and maintaining the program.
-
run-time link: with static link libraries (
.lib
file) is different, the DLL is not linked into the program at compile time, but at program runtime. This means that if a DLL is updated, a program using that DLL can use the new version directly without recompiling. - Multi-language support: DLLs can be written in different languages, such as C, C++, Delphi, etc., as long as they follow certain calling conventions.
- expandability: Applications can dynamically add or subtract functionality by loading and unloading DLLs.
- Resource sharing: The DLL has only one instance in memory, which is shared by all applications that use it, thus saving resources.
Generate DLL
New dll project
New project - select "Win32 console application".
Next, in the pop-up box, selectdll
Wakong Project
When you're done, you get the directory structure of the empty project and create therespond in singing
Two documents
Write code
- exist
The interface that implements the sum and difference of two numbers in the
int sum(int a, int b){
return a + b;
}
int sub(int a, int b){
return a - b;
}
- exist
Declare the function in the
int sum(int a, int b);
int sub(int a, int b);
-
In order to be used in a dll, the function also needs to be prefixed:
extern "C" __declspec(dllexport)
-
included among these
_declspec(dllexport)
means to specify the target that needs to be exported to dll (for dll generation) -
extern "C"
Indicates that the C program is exported as a DLL.
-
extern "C" __declspec(dllexport) int sum(int a, int b);
extern "C" __declspec(dllexport) int sub(int a, int b);
Macro definitions can also be used to make the code more readable
#define SumAndSub_API __declspec(dllexport)
extern "C" SumAndSub_API int sum(int a, int b);
extern "C" SumAndSub_API int sub(int a, int b);
Generate DLL
exist.h
cap (a poem).cpp
After adding the code to the project, right-click on the project and select "Generate".
After successful generation, the projectDebug
The generated dll file can be found in the folder
.h
The directory needs to be documented as well
The packaging of the DLL is now complete.
Calling the DLL
implicit call
First you need to recreate an empty project to invoke the test:
Once created, three more files need to be introduced i.e. the previously generatedcap (a poem)
and projects
file
included among theseneeds to be placed in the current project's
Debug
directory, and the other two can be configured in the properties ((Dependencies also need to be added)
Right-click the item - select Properties
optionVC++ Catalog
where "Include Catalog" is added..h
The folder where the file is located
"Library catalog" added.lib
Catalog
When the addition is complete it is shown in the figure:
Then add the dependencies in the linker-input options
When you're done, apply the properties to save, and then copy the DLL file to the current project'sDebug
directory
Below is a newTo test if the DLL can be called successfully
#include "iostream"
#include ""
using namespace std;
int main(){
cout << sum(2, 5) << endl;
cout << sub(5, 2) << endl;
system("pause");
return 0;
}
Successful call to DLL
explicit call
The explicit call in turn contains thestatic calltogether withdynamic call。
Static Explicit Calls
- Static calls:
.lib
file contains the function code itself, which is added directly to the program at compile time, called thestatic link library (static link library). Static calls use a static link library. The linker gets all the referenced functions from the static link library LIB and puts the library into the executable file along with the code.
First, create a new empty project, and when you're done, add a.cpp
source file and write a simple main function
int main(){
}
Right-click on the item and select - Generate (Generate)Debug
(catalog)
particle marking the following noun as a direct objectinto the current project's
Debug
directory
establish.cpp
The source file is called statically with the following code
#include "iostream"
using namespace std;
#pragma comment(lib, "D:\\Code\\C++\\dll\\DLL1\\Debug\\")
// dllfunction encapsulated in the
extern "C" __declspec(dllimport) int sum(int, int);
extern "C" __declspec(dllimport) int sub(int, int);
int main(){
cout << sum(2, 5) << endl;
cout << sub(5, 2) << endl;
system("pause");
return 0;
}
included among these#pragma comment(lib, "D:\\Code\\C++\\dll\\DLL1\\Debug\\")
represent a linkThis library is equivalent to adding a directory to the "Library Directory" in the "VC++ Directory" in the project properties, and to adding dependencies to the linker-input (an explicit and an implicit difference).
Dynamic Explicit Calls
- Dynamic invocation:
.lib
file contains information about the DLL file where the function is located and the location of the function in the file (the entry), and the code is provided by the DLL loaded in process space at runtime, called theDynamic link library (dynamic link library). Dynamic calls use dynamic link libraries, where the executable module (.dll file or .exe file) itself does not contain the code for the DLL function it calls, but only contains the information needed to locate the code for the DLL function at runtime, and to be able to locate and link to the correct DLL file and function when the program is run. (This information typically includes the function name, parameter types, etc., and is sufficient for the operating system to parse and call the correct function at runtime.)
Again, after creating the project, Mr. ChengDebug
catalogs
After generating theinto the Debug directory
Dynamic calls are made in the following way
- first by
LoadLibrary()
function to load the specified dll file into the program's memory (DLLs do not have their own memory) -
GetProcAddress()
function retrieves the address of the output library function of the specified dll file by function pointertypedef int(*func)(int a, int b);
to load the function and use it. -
FreeLibrary()
Free the space occupied by the dll.
#include "iostream"
#include ""
using namespace std;
typedef int(*func)(int a, int b);
int main(){
// dynamic loading (computing)dll
HMODULE hModule = LoadLibrary("");
if (!hModule){
cout << "Error!" << endl;
}
// loading function
func sum = func(GetProcAddress(hModule, "sum"));
func sub = func(GetProcAddress(hModule, "sub"));
if (sum != NULL){
cout << sum(5, 2) << endl;
}
if (sub != NULL){
cout << sub(5, 2) << endl;
}
// liberate (a *er)
FreeLibrary(hModule);
system("pause");
return 0;
}
The LoadLibrary() function for loading dlls can also use absolute paths, so you don't need to put the dll file in the Debug directory.
// Dynamically load the dll
HMODULE hModule = LoadLibrary("D:\\\Code\\\C++\\dll\\\DLL1\\\Debug\\");;
Note: The dynamic invocation approach is usually the only way to.dll
file, and the lack of.h
cap (a poem).lib
used in the documentation.When all three files are complete, a simpler and more convenient implicit call should be used。