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,点击加法

再点击减法

相关推荐
吾爱星辰3 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
ChinaDragonDreamer3 小时前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
IT良3 小时前
c#增删改查 (数据操作的基础)
开发语言·c#
Kalika0-04 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
_.Switch4 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
代码雕刻家4 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
一个闪现必杀技4 小时前
Python入门--函数
开发语言·python·青少年编程·pycharm
Fan_web4 小时前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
龙图:会赢的4 小时前
[C语言]--编译和链接
c语言·开发语言
威桑5 小时前
记一次控件提升后,运行却不显示的Bug
qt