Qt 下 QLibrary 动态加载 dll

34 篇文章 1 订阅
QLibrary dll("FingerChecker.dll");
    if(dll.load())
    {
        typedef bool (*VerFinger)(char*, char*);
        VerFinger pVerFingerFunc =
                (VerFinger)dll.resolve("VerFinger");        //用resolve来解析fun1函数
        if ( pVerFingerFunc )       //解析成功则进行运算并提示相关信息
        {
            pVerFingerFunc(text,text);
            QMessageBox::about(this,"OK","OK");
        }
        else
        {
            QMessageBox::about(this,"","");
        }
    }
    else
    {
        QMessageBox::about(this,"","");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt5 中动态添加 dll动态链接库)可以使用 QLibrary 类。QLibrary 类提供了动态链接库的加载和卸载功能,能够将动态链接库加载到进程中,并在程序运行时使用其中的函数。 以下是动态添加 dll 的基本步骤: 1. 包含 QLibrary 头文件: ```cpp #include <QLibrary> ``` 2. 创建 QLibrary 对象,指定 dll 文件路径: ```cpp QLibrary myLib("myDll.dll"); ``` 3. 使用 load() 函数加载 dll: ```cpp if(myLib.load()){ // dll 加载成功 }else{ // dll 加载失败 } ``` 4. 使用 resolve() 函数获取 dll 中的函数指针: ```cpp // 获取函数指针 typedef void (*MyFunction)(); MyFunction myFunction = (MyFunction)myLib.resolve("myFunction"); if(myFunction){ // 调用函数 myFunction(); }else{ // 获取函数指针失败 } ``` 5. 在程序退出前,使用 unload() 函数卸载 dll: ```cpp myLib.unload(); ``` 完整示例代码: ```cpp #include <QLibrary> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 创建 QLibrary 对象,指定 dll 文件路径 QLibrary myLib("myDll.dll"); // 加载 dll if(myLib.load()){ qDebug() << "dll loaded successfully"; // 获取函数指针 typedef void (*MyFunction)(); MyFunction myFunction = (MyFunction)myLib.resolve("myFunction"); if(myFunction){ // 调用函数 myFunction(); }else{ qDebug() << "get function pointer failed"; } // 卸载 dll myLib.unload(); }else{ qDebug() << "dll loaded failed"; } return a.exec(); } ``` 注意: 1. dll 文件必须位于应用程序可执行文件同一目录下或系统路径中。 2. 在使用 resolve() 函数获取函数指针时,需要指定函数的名称,并且需要将函数指针强制转换为正确的类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值