使用QLibrary加载动态库

背景

项目中需要对操纵手柄进行读取,在网上找了几篇博客,基本都是依赖winmm.lib,但其中一篇直接加载它的动态库,从里面把函数解析出来,虽然麻烦,但还是眼前一亮,竟然可以这么做!!!
在这里插入图片描述
由于不太熟悉typedef的这种操作,一开始没看懂,后来查了下,这是使用typedef定义的函数指针,并简单整理了这篇文章

分析

以解析的第一个函数为例

QLibrary mylib("Winmm.dll");
typedef int (*MyPrototype) (HWND, UINT, UINT, BOOL);
MyPrototype qJoySetCapture = (MyPrototype)mylib.resolve("joySetCapture");

第一条语句是定义一个库对象mylib,将目标dll加载进来;
第二条语句是定义了一个函数指针类型,该类型定义的指针可指向返回值为int类型函数参数为(HWND,UINT,UINT,BOOL)的函数;
第三条语句是定义了一个函数指针qJoySetCapture。resolve函数中的joySetCapture是dll中的函数名,通过resolve进行解析,返回的是该函数的地址,通过强制类型转化,再让qJoySetCapture指向该地址。

resolve()函数

附上Qt官方解释
QLibrary::resolve(const char *symbol)

Returns the address of the exported symbol symbol. The library is loaded if necessary. The function returns nullptr if the symbol could not be resolved or if the library could not be loaded.

Example:

typedef int (*AvgFunction)(int, int);

AvgFunction avg = (AvgFunction) library->resolve("avg");
if (avg)
    return avg(5, 8);
else
    return -1;

The symbol must be exported as a C function from the library. This means that the function must be wrapped in an extern “C” if the library is compiled with a C++ compiler. On Windows you must also explicitly export the function from the DLL using the __declspec(dllexport) compiler directive, for example:

extern "C" __declspec(dllexport) int avg(int a, int b)
{
    return (a + b) / 2;
}

总结

一般只有对某个dll非常熟悉,知道dll中包含哪些函数、函数参数个数及类型、返回值类型才使用这种方式加载并调用函数,否则,还是使用头文件+lib的方式调用函数。
另外,可以使用Dependency Walker查看dll中的函数
在这里插入图片描述
可以看到,winmm.dll是由C语言编写编译生成的,在dll中的符号就是函数名。

参考

Qt动态连接库/静态连接库创建与使用,QLibrary动态加载库
使用QLibrary加载动态库

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HHT0506

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值