C++ dynamic link library DLL production

Introduction to DLL

Dynamic link library DLL is a collection of reusable code and data that can be used by multiple applications at the same time. Unlike static link libraries, dynamic link libraries are loaded into memory at runtime for use by applications.
DLL has the following advantages:

Reusability: Since multiple applications can share a DLL, they can share the same code and data, increasing code reusability.
Saves memory: Since DLLs are loaded into memory at runtime, they can provide the required functionality without taking up too much memory.
Easy to update: When a DLL needs to be updated, simply replace the existing DLL file without recompiling the applications that use the DLL.
Dynamic linking: DLLs are linked into the application at runtime, so they can be loaded dynamically after the application starts, thus improving the application startup speed.
Stability: Since multiple applications share the same DLL, if a problem occurs with the code or data in the DLL, all applications using the DLL can be fixed after a single update.

3 ways to declare exported functions in DLL

Insert image description here
Insert image description here

C++ to create DLL files

Make dll file
<DllTest.h>

#pragma once
#ifdef MYLIBRARY_EXPORTS
#define MYLIBRARY_API __declspec(dllexport)
#else
#define MYLIBRARY_API __declspec(dllimport)
#endif
 
extern "C" MYLIBRARY_API int Add(int a, int b);

Note: extern “C” only solves the compatibility problem between c and c++ compilers. If you need compatibility with other compilers, the reliable way is to add a .def file. <DllTest.cpp
>

#include "pch.h"
#include "DllTest.h"

int Add(int a, int b)
{
    
    
	return a + b;
}

<Source.def>

LIBRARY DllTest
EXPORTS
Add @1

Call dll and test

//main.cpp
#include "..\\DllTest\DllTest.h"
#include <iostream>
#include <Windows.h>
#pragma comment(lib, "..\\x64\\Release\\DllTest\\DllTest.lib")
using namespace std;

typedef int(*AddFunc)(int, int);

int main()
{
    
    
    //调用__declspec(dllexport)定义的接口
	int a = Add(1, 200);
	printf("a=%d\n", a);
	//调用def定义的接口
	HINSTANCE hinstLib = LoadLibrary(TEXT("..\\x64\\Release\\DllTest\\DllTest.dll"));
	if (hinstLib != NULL)
	{
    
    
		AddFunc add = (AddFunc)GetProcAddress(hinstLib, "Add");
		if (add != NULL)
		{
    
    
			// 调用 DLL 中的函数
			int result = add(1, 2);
			std::cout << "b=" << result << std::endl;
		}
	}
	else
	{
    
    
		cout << "hinst is null" << endl;
	}

	system("pause");
	return 0;
}

Insert image description here
As you can see from the above, the interface defined through def is more troublesome when calling, and the pointer of the called function needs to be defined. Therefore, the first method is usually adopted to design the interface. Of course, in order to facilitate subsequent maintenance and solve compiler compatibility issues, the def file can also be written together.

Is the DllMain function necessary?

Introduction
to DllMain Just like exe has a main or WinMain entry function, DLL also has an entry function, which is DllMain. Use "DllMain" as the keyword to see how the MSDN help document introduces this function.
The DllMain function is an optional method of entry into a dynamic-link library (DLL). (Brief translation: For a Dll module, the DllMain function is optional .) This sentence is very important. Many beginners may think that a dynamic link library must have a DllMain function. In fact, this is not the case. Many DLLs that only contain resource information do not have a DllMain function.
When to call DllMain
When does the system call the DllMain function? The DllMain function will be called when calling LoadLibrary and FreeLibrary during static linking or dynamic linking. The third parameter fdwReason of DllMain specifies the reason for the system call Dll, which may be DLL_PROCESS_ATTACH, DLL_PROCESS_DETACH, DLL_THREAD_ATTACH and DLL_THREAD_DETACH. The following analyzes when the system calls DllMain from these four situations. Please refer to this blog for details .

[1] Method of writing DLL in C++
[2] Summary of production methods of C++ Windows dynamic library [.DLL file]
[3] C++ dynamic link library-06-Use of .def file in DLL
[4] DLL file_DLLMain function Detailed explanation

Guess you like

Origin blog.csdn.net/qq_44924694/article/details/133124626