20、Qt使用QLibrary类调用动态链接库dll中的成员函数

一、功能说明

创建一个项目,里面包含两个子项目,

子项目1生成动态链接库dll,里面包含一个加法和一个减法;

子项目2是带界面的可执行文件exe,使用QLibrary类来调用子项目1的dll。

二、创建子项目1

新建项目,选择"其他项目"、"子目录项目"

更改总项目名称和位置

选择编译器

默认,点击"完成&添加子项目"

选择子项目1模板,"Library"、"C++库"

更改类型和名称

默认

默认

默认

完成后,项目文件结构如下

右击"LibraryDll",添加新文件

选择C++、C++ Class

更改名称

默认

在LibraryDll.pro中添加代码,更改子项目1的dll生成位置

复制代码
#-------------------------------------------------
#
# Project created by QtCreator 2023-09-27T14:10:33
#
#-------------------------------------------------

QT -= gui

TARGET = LibraryDll
TEMPLATE = lib

DEFINES += LIBRARYDLL_LIBRARY

CONFIG(debug , debug | release) {
win32:!wince{
DESTDIR = $$PWD/../dll
}
unix {
DESTDIR = $$PWD/../dll
}
} else {
win32:!wince{
DESTDIR = $$PWD/../dll
}
unix {
DESTDIR = $$PWD/../dll
}
}

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
librarydll.cpp \
dllmain.cpp

HEADERS += \
librarydll.h \
librarydll_global.h \
dllmain.h

unix {
target.path = /usr/lib
INSTALLS += target
}

更改librarydll.h代码如下

复制代码
#ifndef LIBRARYDLL_H
#define LIBRARYDLL_H

#include "librarydll_global.h"

class LIBRARYDLLSHARED_EXPORT LibraryDll
{

public:
    LibraryDll();

    int addFun(int , int);
    int subFun(int , int);
};

#endif // LIBRARYDLL_H

更改librarydll.cpp代码如下

复制代码
#include "librarydll.h"

LibraryDll::LibraryDll()
{
}

int LibraryDll::addFun(int value1, int value2)
{
    return (value1 + value2);
}

int LibraryDll::subFun(int value1, int value2)
{
    return (value1 - value2);
}

更改dllmain.h代码如下

复制代码
#ifndef DLLMAIN_H
#define DLLMAIN_H

#include "librarydll_global.h"

extern "C" LIBRARYDLLSHARED_EXPORT int addFun(int , int);
extern "C" LIBRARYDLLSHARED_EXPORT int subFun(int , int);

#endif // DLLMAIN_H

更改dllmain.cpp代码如下

复制代码
#include "dllmain.h"
#include "librarydll.h"

static LibraryDll LibraryInstance;

int addFun(int value1, int value2)
{
    return LibraryInstance.addFun(value1, value2);
}

int subFun(int value1, int value2)
{
    return LibraryInstance.subFun(value1, value2);
}

右击"LibraryDll",重新构建

构建完成后,在dll目录下生成dll

三、创建子项目2

右击"MyLibrary",新子项目

选择"Application"、Qt Widgets Application

更改子项目2名称

默认

默认

默认

在mainwindow.ui中添加如下控件

更改mainwindow.h代码

复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

//定义函数指针
typedef int (*addFun)(int , int);
typedef int (*subFun)(int , int);

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

更改mainwindow.cpp代码

复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLibrary>

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

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

void MainWindow::on_pushButton_clicked()
{
    QString path = QApplication::applicationDirPath() + "/LibraryDll.dll";

    QLibrary dllLib;
    dllLib.setFileName(path);
    if(dllLib.load())
    {
        addFun myFun = reinterpret_cast<addFun>(dllLib.resolve("addFun"));
        int ret = myFun(ui->lineEdit->text().toInt(), ui->lineEdit_2->text().toInt());
        ui->label->setText(QString::number(ret));

        dllLib.unload();
    }
    else
    {
        ui->label->setText("load dll error: " + dllLib.errorString());
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    QString path = QApplication::applicationDirPath() + "/LibraryDll.dll";

    QLibrary dllLib;
    dllLib.setFileName(path);
    if(dllLib.load())
    {
        subFun myFun = reinterpret_cast<subFun>(dllLib.resolve("subFun"));
        int ret = myFun(ui->lineEdit->text().toInt(), ui->lineEdit_2->text().toInt());
        ui->label->setText(QString::number(ret));

        dllLib.unload();
    }
    else
    {
        ui->label->setText(dllLib.errorString());
    }
}

四、运行测试

输入5和3,点击加法

再点击减法

相关推荐
云小逸6 分钟前
C++ 第一阶段:对象、内存与生命周期
开发语言·c++
玖玥拾21 分钟前
Lua 基础语法(二)
开发语言·unity·lua
王的宝库25 分钟前
GO常用标准库包
开发语言·后端·golang
geovindu41 分钟前
python: Face Recognition
开发语言·后端·python·人脸识别
平头哥技术团队1 小时前
Day 13 | 调 line-height 和 margin:三处间距让名片页脱离模板感
开发语言·前端·javascript·学习·html5
黑马程序员毕设1 小时前
基于Java的仪器管理系统设计与实现
java·开发语言·spring boot·后端·微信小程序
平头哥AI1 小时前
Day 13 | 一个函数回两个值:Go 的 error 是从哪冒出来的
开发语言·后端·golang
asdzx672 小时前
Python 实现 PDF 文本查找与高亮标注
开发语言·python·pdf
zander2582 小时前
LeetCode 1143. 最长公共子序列
开发语言·python·算法
Sumerking2 小时前
llc_control.c 专项评审(v3 更新版)
c语言·开发语言·算法·obc