DLL,是 Dynamic Link Library的缩写,中文名
动态链接库
。DLL是一个包含可由多个程序,同时使用的代码和数据的库。 本文简介DLL 概念,记录 DLL 工程创建与使用方法。
.h
头文件和.lib
文件
LoadBibrary()
GetProcessAddress()
.dll
, .ocx
或者 .drv
(驱动程序)。
__declspec(dllexport)
extern "C"
函数体中.h
头文件和.lib
文件typedef DWORD(*MYDEMOW)();
MYDEMOW demo =
hmo = LoadLibrary(_T("DLL2.dll"));
MYDEMOW demo= (MYDEMOW)GetProcAddress(hmo, "DEMOW")
demo();
FreeLibrary(hmo);
文件
-> 新建
-> 项目
-> Visual C++
-> Windows 桌面
-> 动态链接库
。dll_demo
dll.h
,在其中声明外界调用的类和接口,我在这里设置了几种示例,用于静态、动态调用测试:1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 | #pragma once#ifdef CREATEDELL_API_DU#else #define CREATEDELL_API_DU _declspec(dllimport) //当编译时,头文件不参加编译,所以.cpp文件中先定义,后头文件被包含进来,因此外部使用时,为dllexport,而在内部编译时,则为dllimport#endif // 静态加载测试类class CREATEDELL_API_DU animal //需要被外界调用的类(父类){public: virtual int outDate() = 0; //纯虚函数 void getWide(int x); void getHigh(int y);protected: int wide; int high;};class CREATEDELL_API_DU cat :public animal //需要被调用的类(子类cat){public: int outDate();};class CREATEDELL_API_DU dog :public animal //需要被调用的类(子类dog){public: int outDate();};// 静态/动态 加载测试函数int square1(int x);extern "C"{ __declspec(dllexport) int square2(int x);}__declspec(dllexport) int square3(int x);//动态加载测试类class Math{public: virtual double add(double a, double b) = 0; virtual double substract(double a, double b) = 0; virtual double multiply(double a, double b) = 0; virtual double divide(double a, double b) = 0; virtual ~Math() {};};extern "C"{ __declspec(dllexport) Math* FactoryCreate(); __declspec(dllexport) void FactoryDestroy(Math* pmath);}class MathImplementation : public Math{public: double add(double a, double b); double substract(double a, double b); double multiply(double a, double b); double divide(double a, double b);}; |
---|
新建 dll.cpp
源文件,在其中完成对外类、接口的实现
在源文件开头需要引入 pch.h
加入
#include "pch.h"
, 否则会报错 1错误 C1010 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "pch.h"”? dll_demo
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 | #include "pch.h"#define CREATEDELL_API_DU _declspec(dllexport)#include <iostream>#include "dll.h"using namespace std;//父类中函数实现 //静态加载类实现void animal::getWide(int x) { wide = x;}void CREATEDELL_API_DU animal::getHigh(int y) { high = y;}//子类cat中数据输出实现int CREATEDELL_API_DU cat::outDate() { return (wide + high); wide += wide; high += high;}//子类dog数据输出实现int CREATEDELL_API_DU dog::outDate() { return (wide - high);}//函数的实现//测试函数实现int square1(int x){ return x * x;}int square2(int x){ return x * x;}int square3(int x){ return x * x;}//动态加载测试类实现Math* FactoryCreate(){ return new(std::nothrow) MathImplementation; //注意返回的是实现类的指针}void FactoryDestroy(Math* pmath){ delete pmath;}double MathImplementation::add(double a, double b){ return a + b;}double MathImplementation::substract(double a, double b){ return a - b;}double MathImplementation::multiply(double a, double b){ return a * b;}double MathImplementation::divide(double a, double b){ return a / b;} |
---|
dll
, lib
等文件dll
的创建dll
、 lib
、.h
后,我们就可以着手使用了dll_load
dll
,lib
, .h
文件的方式调用 dlldll.h
lib
文件所在路径,作为库目录lib
文件作为依赖项dll
文件拷贝到项目源文件夹用于静态加载
load.cpp
dll
代码,其中包含了静态、动态加载 dll 函数、类的简单示例,一些需要注意的点我写在注释里了
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 | #include<iostream>#include <Windows.h>#include <string>#include "dll.h"typedef int(*test_func1)(int);typedef int(*FUN_SUB)(int, int);typedef Math* (*PFNFACTORYCREATE)();typedef void(*PFNFACTORYDESTROY)(Math*);using std::wstring;using std::to_wstring;using namespace std;bool main(){ // 静态调用函数测试 cout << endl << "静态调用函数测试" << endl; // cout << square1(7) << endl; // 由于在 dll 创建时未声名 __declspec(dllexport),因此会报错“无法解析的外部符号” cout << square2(7) << endl; cout << square3(7) << endl; // 静态调用类测试 cout << endl << "静态调用类测试" << endl; dog dog; //实例化dog对象、赋值、并输出。 dog.getHigh(5); dog.getWide(6); cout << dog.outDate() << endl; cat cat; //实例化cat对象、赋值、并输出 cat.getHigh(16); cat.getWide(4); cout << cat.outDate() << endl; Math * sta_obj = FactoryCreate(); cout << sta_obj->add(1.0, 2.0) << endl; cout << sta_obj->substract(1.0, 2.0) << endl; cout << sta_obj->multiply(1.0, 2.0) << endl; cout << sta_obj->divide(1.0, 2.0) << endl; FactoryDestroy(sta_obj); // 动态加载DLL cout << endl << "动态加载DLL" << endl; HMODULE hModule = LoadLibrary("dll_demo.dll"); if (hModule == nullptr) { std::cout << "加载DLL失败!\n"; return 0; } // 动态加载 dll 函数 cout << endl << "动态加载 dll 函数" << endl; test_func1 dllFuntest1 = test_func1(GetProcAddress(hModule, "square1")); // 由于未进行 __declspec(dllexport) 声名,加载失败 test_func1 dllFuntest2 = test_func1(GetProcAddress(hModule, "square2")); test_func1 dllFuntest3 = test_func1(GetProcAddress(hModule, "square3")); // 由于未写入 extern "C",动态加载失败,但可以静态加载 if (dllFuntest1 == nullptr) { std::cout << "dllFuntest1 加载函数失败!\n"; } if (dllFuntest2 == nullptr) { std::cout << "dllFuntest2 加载函数失败!\n"; } if (dllFuntest3 == nullptr) { std::cout << "dllFuntest3 加载函数失败!\n"; } std::cout << dllFuntest2(7) << std::endl; // 动态加载 dll 类 cout << endl << "动态加载、使用 dll 类" << endl; PFNFACTORYCREATE FactoryCreate = PFNFACTORYCREATE(GetProcAddress(hModule, "FactoryCreate")); PFNFACTORYDESTROY FactoryDestroy = PFNFACTORYDESTROY(GetProcAddress(hModule, "FactoryDestroy")); if (FactoryCreate == nullptr) { std::cout << "加载类定义失败!\n"; return 0; } Math *pmath = FactoryCreate(); cout << pmath->add(1.0, 2.0) << endl; cout << pmath->substract(1.0, 2.0) << endl; cout << pmath->multiply(1.0, 2.0) << endl; cout << pmath->divide(1.0, 2.0) << endl; FreeLibrary(hModule); system("pause"); return 0;} |
---|
dll
得到的输出1234567891011121314151617181920212223242526 | 静态调用函数测试4949静态调用类测试1203-120.5动态加载DLL动态加载 dll 函数dllFuntest1 加载函数失败!dllFuntest3 加载函数失败!49动态加载、使用 dll 类3-120.5请按任意键继续. . . |
---|
1 | Error C2375 'onnx_inference::initModel': redefinition; different linkage |
---|