Qt使用QLibrary调用C++动态库

本文详细介绍了在Qt环境中调用动态链接库(DLL)的三种方法:1) 使用Win32API直接调用,涉及LoadLibrary和GetProcAddress函数;2) 利用Qt的QLibrary类加载和调用DLL函数;3) 直接链接DLL并使用Qt的库支持。文中通过代码示例展示了如何在Qt项目中实现这些方法,包括如何定义和调用DLL中的函数,以及处理加载和调用过程中的错误。
摘要由CSDN通过智能技术生成

Qt系列文章目录

前言

Qt调用DLL方法一:使用Win32 API

一、dll编写

#ifndef MYTESTDLL_H
#define MYTESTDLL_H

#include "MyTestDll_global.h"
#include "qdialog.h"
class MYTESTDLLSHARED_EXPORT MyTestDll {
public:
    MyTestDll();
};
extern "C" Q_DECL_EXPORT int add(int a,int b);
extern "C" Q_DECL_EXPORT QDialog *showDialog();
#endif // MYTESTDLL_H

显式链接到 DLL,应用程序必须:
1 调用 LoadLibrary(或相似的函 数)以加载DLL和获取模块句柄。
2 调用 GetProcAddress,以获取指向应用程序要调用的每个导出函数的函数指针。由于应用程序是通过指针调用DLL的函数,编译器不生成外部引用,故无需导入库链接(不需要lib文件)。
3 使用完 DLL后调用FreeLibrary。

需要说明的是,typedef int (Fun)(int,int); //定义函数指针,以备调用;是声明想要调用的函数指针,这个函数的参数必须和DLL里面的一样。Fun是DLL里面“实际的函数名”,必须和头文件里面声明的一样,否则将会调用失败。使用需要加Windows.h头文件。Windows里的Qt是用Mingw GCC来编译,而Mingw GCC可以支持Win32的API。使用时并不需要包含DLL的头文件。编译时可能会报错error: cannot convert 'const char’ to 'LPCWSTR ^,只要在pro文件中添加DEFINES-= UNICODE即可。

二、调用dll

1.引入库

代码如下(示例):


```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qlibrary.h"
#include "qmessagebox.h"
#include "string.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

QString sresult = nullptr;

typedef int (*Fun)(int,int); //定义函数指针,以备调用
void MainWindow::on_pushButton_clicked()
{
   QLibrary mylib("E:\\QtExercise\\qtPluginDev\\MyTest\\debug\\MyTestDll.dll");   //声明所用到的dll文件
   int result;
//    bool flag = mylib.load();
   if (mylib.load())              //判断是否正确加载
   {
       Fun open=(Fun)mylib.resolve("add");    //援引 add() 函数
       if (open)                  //是否成功连接上 add() 函数
       {
            int s1=this->ui->lineEdit->text().toInt();
            int s2=this->ui->lineEdit_2->text().toInt();
            result=open(s1,s2);      //这里函数指针调用dll中的 add() 函数
            sresult=QString::number(result);
            this->ui->lineEdit_3->setText(sresult);
       }
   }
   else
   {
       QMessageBox::information(NULL,"NO","DLL is not loaded!");
   }

}
typedef QDialog *(*Funs)(); //定义函数指针,以备调用
void MainWindow::on_pushButton_2_clicked()
{
   //加载插件
   QLibrary mylib("MyTestDll.dll");   //声明所用到的dll文件
   if (mylib.load())              //判断是否正确加载
   {
       Funs open=(Funs)mylib.resolve("showDialog");    //援引 add() 函数
       if (open)                  //是否成功连接上 add() 函数
       {
           QDialog *form1= open();
           form1->show();
        }
    }
}


## 2.Qt调用DLL方法二:使用Qt的API 
对于调用DLL的方法,Qt原来本身就有相应的类来实现,用起来和Win32的步骤差不多
h文件

```cpp
extern "C" Q_DECL_EXPORT int add(int a,int b);
extern "C" Q_DECL_EXPORT QDialog *showDialog();
typedef int (*Fun)(int,int); //定义函数指针,以备调用

Qt调用DLL方法三:直接调用 DLL

VC的引用库文件为xxx.lib, GCC的为xxx.a, 通过比较两种库文件的格式,发现很相似。于是把xxx.dll,xxx.lib和xxx.h复制到Qt的project下,直接把xxx.lib改为xxx.a, 根据Qt的库名字的格式, 在xxx.a的前面加上lib, 即为libxxx.a。

再在Qt的.pro文 件中最后面加上

LIBS += -L. –lxxx //增加当前目录下的libxxx.a

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += \
        main.cpp

LIBS += -L$$PWD/./ -lCartDll
LIBS += -L$$PWD/./ -lsvml_disp
#svml_disp.lib

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/./ -lCartDll
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/./ -lCartDll
else:unix: LIBS += -L$$PWD/./ -lCartDll

INCLUDEPATH += $$PWD/.
DEPENDPATH += $$PWD/.

在Qt的源文件中加上

#include “xxx.h”

现在就可以直接调用xxx.h中的函数了。

// xxx.h

#ifndef XXX_H

#define XXX_H

WINAPI int xxx_func(void);

#endif

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qlibrary.h"
#include "qmessagebox.h"
#include "string.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

QString sresult = nullptr;

typedef int (*Fun)(int,int); //定义函数指针,以备调用
void MainWindow::on_pushButton_clicked()
{
   QLibrary mylib("E:\\QtExercise\\qtPluginDev\\MyTest\\debug\\MyTestDll.dll");   //声明所用到的dll文件
   int result;
//    bool flag = mylib.load();
   if (mylib.load())              //判断是否正确加载
   {
       Fun open=(Fun)mylib.resolve("add");    //援引 add() 函数
       if (open)                  //是否成功连接上 add() 函数
       {
            int s1=this->ui->lineEdit->text().toInt();
            int s2=this->ui->lineEdit_2->text().toInt();
            result=open(s1,s2);      //这里函数指针调用dll中的 add() 函数
            sresult=QString::number(result);
            this->ui->lineEdit_3->setText(sresult);
       }
   }
   else
   {
       QMessageBox::information(NULL,"NO","DLL is not loaded!");
   }

}
typedef QDialog *(*Funs)(); //定义函数指针,以备调用
void MainWindow::on_pushButton_2_clicked()
{
   //加载插件
   QLibrary mylib("MyTestDll.dll");   //声明所用到的dll文件
   if (mylib.load())              //判断是否正确加载
   {
       Funs open=(Funs)mylib.resolve("showDialog");    //援引 add() 函数
       if (open)                  //是否成功连接上 add() 函数
       {
           QDialog *form1= open();
           form1->show();
        }
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值