当前位置:网站首页>DLL的封装及调用
DLL的封装及调用
2022-04-22 18:01:00 【东城青年】
DLL封装
1.新建Win32项目


2.新建function.h头文件
注意:
- 其中__declspec(dllexport)代表导出dll;
- 为了在生成dll的过程中函数名不发生改变方便后面dll的显示调用,需要在前面加上extern "C";
- dll封装类,为了后面dll的显示调用能调到类中的方法,需要在类中方法名前加上virtual;
如果后面采用dll的隐式调用,则正常声明即可,因为lib文件中会生成相应的函数名和入口地址。
function.h:
#define CREATDLL_EXPORTS
#ifdef CREATDLL_EXPORTS
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
#include <string>
class Test {
public:
virtual void add_fun(int a, int b, int &c);
virtual void subtract_fun(int a, int b, int &c);
Test();
~Test();
};
extern "C" DLL_API Test* getObjectAddress();
extern "C" DLL_API void threshold_image(std::string src_path, std::string dst_path, double thresh);
3.在Win32Project1.cpp中实现函数
Win32Project1.cpp:
#include "stdafx.h"
#include "iostream"
#include "function.h"
#include "opencv2\opencv.hpp"
using namespace std;
using namespace cv;
Test::Test()
{
}
Test::~Test()
{
}
void Test::add_fun(int a, int b, int &c)
{
c = a + b;
}
void Test::subtract_fun(int a, int b, int &c)
{
c = a - b;
}
Test* getObjectAddress()
{
Test* Pclass = new Test();
return Pclass;
}
void threshold_image(std::string src_path, std::string dst_path, double thresh)
{
Mat src = imread(src_path, 0);
Mat dst = src.clone();
threshold(src, dst, thresh, 255, THRESH_BINARY);
imwrite(dst_path, dst);
}
4.选好平台环境如Release,生成解决方案,则在x64/Release下会生成对应的dll和lib文件。

DLL调用
新建Win32控制台程序,空项目。
一、隐式调用
1.将.dll、.lib、.h头文件都拷入当前工程目录下;
2.将头文件包含进来;
3.配置lib库路径及名称两种方法:
- 项目->属性->链接器->常规->附加库目录中将lib库目录添加进来,然后在链接器->输入中输入lib库的名称;
- 直接在函数中添加一行代码:#pragma comment (lib, "Win32Project1.lib")
4.隐式调用
main.cpp:
#include "iostream"
#include "function.h"
using namespace std;
#pragma comment (lib, "Win32Project1.lib")
int main()
{
Test *PTest = getObjectAddress();
int c = 0;
PTest->add_fun(1, 2, c);
cout << c <<endl;
string src_path = "src.jpg";
string dst_path = "dst.jpg";
threshold_image(src_path, dst_path, 100);
}
二、显示调用
显示调用只需拷贝dll即可,通过Windows.h中的LoadLibrary加载dll,然后再通过GetProcAddress返回相应函数名的函数指针。
main.cpp:
#include "iostream"
#include "Windows.h"
#include <tchar.h>
using namespace std;
class Test {
public:
virtual void add_fun(int a, int b, int &c);
virtual void subtract_fun(int a, int b, int &c);
Test();
~Test();
};
typedef Test*(*PFun_class)();
typedef void(*PFun_threshold_image_DLL)(std::string src_path, std::string dst_path, double thresh);
int main()
{
HMODULE hdll = LoadLibrary(_T("Win32Project1.dll"));
if (hdll != NULL) {
//dll中调用类
PFun_class P_fun_class = (PFun_class)GetProcAddress(hdll, "getObjectAddress");
if (P_fun_class != NULL) {
Test* P_object = P_fun_class();
int c = 0;
P_object->add_fun(1, 2, c);
cout << c << endl;
}
else {
cout << "P_fun_class is null" << endl;
}
// dll中调用普通函数
PFun_threshold_image_DLL P_fun_threshold_image = (PFun_threshold_image_DLL)GetProcAddress(hdll, "threshold_image");
if (P_fun_threshold_image != NULL) {
string src_path = "src.jpg";
string dst_path = "dst.jpg";
P_fun_threshold_image(src_path, dst_path, 100);
}
else {
cout << "P_threshold_image is null" << endl;
}
}
else {
cout << "load dll failed" << endl;
}
FreeLibrary(hdll);
}
版权声明
本文为[东城青年]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_24946843/article/details/124317814
边栏推荐
- Activiti 挂起与激活任务
- 软考高项笔记 | 可行性研究的步骤
- Soft test high-level notes | five levels of informatization
- 软考高项笔记 | 项目进度管理
- Interface test mock practice (II) | complete batch manual mock in combination with JQ
- An idea plug-in that doesn't work, but can install X
- 阿里云容器&服务网格产品技术动态(202203)
- In 2022, it is said on the Internet that Apple's upcoming new models iPhone 14 pro and iPhone 14 Pro Max will be a new screen shape, not a banged screen. Do you expect the iPhone 14 with a new screen
- How do I completely delete files on my computer?
- OPLG:新一代云原生可观测最佳实践
猜你喜欢

软考高项笔记 | 信息系统安全

软考高项笔记 | 项目建议书的内容

Fiddler- 篡改服务器返回的数据

软考高项笔记 | 项目的特点

Unable to translate SQLException with Error code ‘0‘, will now try the fallback translator

阿里云容器&服务网格产品技术动态(202203)

【C语言进阶篇】一篇文章带你认清结构、枚举和联合

MySQL排序与分页

Huawei router realizes the connection between headquarters and branches through MPLS virtual private network
![[Lane] ultra fast lane detection (2) custom model test](/img/28/51c6f1aee1d2e70829fbb71657ce2b.jpg)
[Lane] ultra fast lane detection (2) custom model test
随机推荐
Dpdk obtains and parses packets from ring queue and uses hashtable statistics
[Lane] ultra fast lane detection (2) custom model test
小程序----API
软考高项笔记 | 信息系统项目典型生命周期模型
After calling Bapi many times, what is the problem with the last one-time commit work?
2021年资产管理与托管银行行业发展研究报告
Common libraries for video playback
Go operation MySQL
Examples of comprehensive application of DOM -- click plus one and minus one
数字化靶场的未来方向
What data indicators should app focus on?
最近学习感悟
Multithreading notes | six states of threads
为 JUnit 配置测试库
Soft test high item notes | demand classification
软考高项笔记 | 软件集成技术
多线程笔记 | 线程的六种状态
Soft test high item notes | typical life cycle model of information system project
软考高项笔记 | 软技能
Secyun assisted the "SaaS cloud management platform solution based on pseudo application integration framework" released by CETC 32