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);
}
|