Series Article Directory
Chapter 1: Visual Studio 2019 Dynamic Link Library DLL Establishment
Chapter Two: VS Dynamic Link Library DLL Debugging
Table of contents
1.3 Changing the solution startup project
foreword
The previous chapter completed the establishment of the DLL, but the DLL will only be executed when it is called. This chapter records the process of debugging the DLL with a C++ project in the same solution.
1. Create a new C++ project?
In the same solution explorer, right click on the solution - Add - New Project - Add Empty Project
Two, debug DLL
1. Implicit call/static call
1.1 File Settings
Copy the .h file in the DLL directory and the .lib file generated by DLL compilation to the same directory as the .cpp of the C++ project
Windows soft link mapping files can be used so that they do not have to be copied every time they are generated
New-Item -ItemType SymbolicLink -Path 原文件路径 -Target 目标文件路径
1.2 Write C++ debugging code
what needs to be changed
- Change the two imports in the header
#include "dedip.h"
#pragma comment(lib, "dedip.lib")
- change external function
extern "C" __declspec(dllimport) bool eyeCloseCheck(BYTE*, int, int, int);
- Change the function when called
cout << eyeCloseCheck(buffer, width, height, VailImgNum) << endl;
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <windows.h>
#include "dedip.h"
#pragma comment(lib, "dedip.lib")
#include <opencv2/dnn/dnn.hpp>
using namespace cv;
using namespace std;
extern "C" __declspec(dllimport) bool eyeCloseCheck(BYTE*, int, int, int);
int main()
{
Mat srcImg = imread("D:/ImgEnglish/redeye_img_test/1.bmp");
int width = srcImg.cols;
int height = srcImg.rows;
//imshow("1", srcImg);
//waitKey(0);
//DLL调试
int length = (int)(srcImg.total() * srcImg.elemSize());
BYTE* buffer = new BYTE[length];
memcpy(buffer, srcImg.data, length * sizeof(BYTE));
int VailImgNum = 1;
cout << eyeCloseCheck(buffer, width, height, VailImgNum) << endl;
//imshow("2", OutImg);
//waitKey(0);
}
1.3 Changing the solution startup project
Right click on the project - set as startup project
2. Explicit call
It is troublesome to display the call, post a sample code, and refer to it yourself
The general idea is that there is no need to copy .h and .lib. When a solution is debug or release, the generated files are in one folder.
#include <stdio.h>
#include <windows.h>
void main(void)
{
typedef int(*MyFunDll)(void);
HMODULE hdll = LoadLibrary("Win32Project1.dll"); //加载dll文件
if (hdll != NULL)
{
MyFunDll MyFunCall = (MyFunDll)GetProcAddress(hdll, "main");//检索要调用函数的地址
if (MyFunCall != NULL)
{
MyFunCall(); //调用接口函数
}
}
FreeLibrary(hdll); //释放dll文件
}
Load the dll file through the LoadLibrary() function; then use the GetProcAddress() function to obtain the address of the interface function to be called (in the above example, use MyFunCall to store the address of the interface function); then call the interface function (MyFunCall); finally Release the dll file through the FreeLibrary() function. Therefore, if it is used to load other dll files, the changes in the above example are as follows:
The name of the loaded dll file ("Win32Project1.dll" in the above example);
the name of the interface function to be retrieved ("main" in the above example);
the format of the interface function to be called (such as MyFunCall() in the above example, the function's The parameter information should be consistent with the interface function "main" to be called).
Summarize
Summary: It is more convenient to write code implicitly, but you need to copy the file again when there is a change.
Reference: https://blog.csdn.net/weixin_44536482/article/details/91519413
C++ write dynamic link library dll and call dll-Baidu experience (baidu.com)