Qt DLL总结【二】-创建及调用QT的 DLL

时间:2023-03-09 13:30:47
Qt DLL总结【二】-创建及调用QT的 DLL

开发环境:VS2008+Qt4.7.4

最近看了不少Qt的DLL例子,总结一下如何创建和调用QT 动态链接库。

先讲一下对QT动态链接库的调用方法,主要包括:

1、显式链接DLL,调用DLL的全局函数,采用Qt的QLibrary方法

2、显示链接DLL,调用DLL中类对象、成员函数。(通过对象即可实现类成员函数的调用)

①用虚函数表的方法,这也是COM使用的方法,利用Qt的QLibrary技术调用;

②用GetProcAddress直接调用。

用Qt的QPluginLoader类直接调用生成的DLL插件类对象

3、隐式链接DLL:也是采用Qt的Qlibrary方法

关于这种三种方法,下面详细叙说

详细分类叙述

前提:两个项目文件目录

1、TestDLL项目:testdll_global.h,TestDll.h,TestDll.cpp

2、TestMain exe应用项目:main.cpp

testdll_global.h 文件源代码一直不变

  1. #ifndef TESTDLL_GLOBAL_H
  2. #define TESTDLL_GLOBAL_H
  3. #include <QtCore/qglobal.h>
  4. #ifdef TESTDLL_LIB
  5. # define TESTDLL_EXPORT Q_DECL_EXPORT
  6. #else
  7. # define TESTDLL_EXPORT Q_DECL_IMPORT
  8. #endif
  9. #endif // TESTDLL_GLOBAL_H

DLL的显式链接在某些时候比隐式链接具有更大的灵活性。比如,如果在运行时发现DLL无法找到,程序可以显示一个错误信息并能继续运行。当你想为你的程序提供插件服务时,显式链接也很有用处

1、采用显示链接调用DLL中全局函数,只需要一个TestDLL.dll。

通常Windows下程序显示调用dll的步骤分为三步(三个函数):LoadLibrary()、GetProcAdress()、FreeLibrary()

其中,LoadLibrary() 函数用来载入指定的dll文件,加载到调用程序的内存中(DLL没有自己的内存!)

GetProcAddress() 函数检索指定的动态链接库(DLL)中的输出库函数地址,以备调用

FreeLibrary() 释放dll所占空间

而QT的QLibrary类显示链接调用DLL的步骤:load()、resolve(const char * symbol )、unload()和VC步骤类似

TestDll.dll项目中的TestDLL.h源码

  1. #ifndef TESTDLL_H
  2. #define TESTDLL_H
  3. #include "testdll_global.h"
  4. class TESTDLL_EXPORT TestDll
  5. {
  6. public:
  7. TestDll();
  8. ~TestDll();
  9. private:
  10. };
  11. extern "C" TESTDLL_EXPORT void helloWorld();
  12. extern "C" TESTDLL_EXPORT int add(int a,int b);
  13. #endif // TESTDLL_H

TestDll.dll项目中的TestDLL.cpp源码

  1. #include <iostream>
  2. #include "TestDll.h"
  3. TestDll::TestDll()
  4. {
  5. }
  6. TestDll::~TestDll()
  7. {
  8. }
  9. void helloWorld()
  10. {
  11. std::cout << "hello,world!";
  12. }
  13. int add(int a,int b)
  14. {
  15. return a + b;
  16. }

注:1)建立成功DLL项目后,可以在VS命令提示行中用命令"dumpbin -exports DllTest.dll"来查看(也可以用VC工具包中的depends使用程序来查看)  
   注:2)必须使用extern "C"链接标记,否则C++编译器会产生一个修饰过的函数名,这样导出函数的名字将不再是helloworld,而是一个形如" ?helloWorld@TestDll@@UAEXXZ”的名字。为什么名字不是helloworld呢?这是因为C++为了支持函数的重载,会在编译时将函数的参数类型信息以及返回值类型信息加入到函数名中,这样代码中名字一样的重载函数,在经过编译后就互相区分开了,调用时函数名也经过同样的处理,就能找到对应的函数了。详细可以看这篇文章动态链接库(Dynamic Link Library)学习笔记

TestMain项目 main.cpp

  1. #include <QtCore/QCoreApplication>
  2. #include <iostream>
  3. #include <QLibrary>
  4. typedef int (*Fun)(int,int); //定义函数指针,int add(int a,int b);
  5. int main(int argc, char *argv[])
  6. {
  7. QCoreApplication a(argc, argv);
  8. QLibrary mylib("TestDll.dll");   //声明所用到的dll文件
  9. int result;
  10. //判断是否正确加载
  11. if (mylib.load())
  12. {
  13. std::cout << "DLL load is OK!"<<std::endl;
  14. //调用外部函数 add()
  15. Fun add = (Fun)mylib.resolve("add");
  16. //是否成功连接上 add() 函数
  17. if (add)
  18. {
  19. std::cout << "Link to add Function is OK!"<<std::endl;
  20. //这里函数指针调用dll中的 add() 函数
  21. result = add(5,6);
  22. std::cout << result;
  23. }
  24. else
  25. std::cout << "Link to add Function failed!!"<<std::endl;
  26. }
  27. //加载失败
  28. else
  29. std::cout << "DLL is not loaded!"<<std::endl;
  30. return a.exec();
  31. }

2、采用显示链接,调用C++类中的类对象、成员函数 

如果你想导出并显式链接一组C++类中的成员函数又该怎么办呢?这里有两个问题。第一是C++成员函数名是经过修饰的(即使指定extern "C"标记也是这样);第二是C++不允许将指向成员函数的指针转换成其它类型。这两个问题限制了C++类的显式链接。下面介绍两种方法来解决这个问题:

①用虚函数表的方法,这也是COM使用的方法,利用Qt的QLibrary技术调用;

②用GetProcAddress直接调用。

用Qt的QPluginLoader类直接调用生成的DLL插件类对象

①虚函数表的方法,QLibrary 技术调用

TestDll.h代码

  1. #ifndef TESTDLL_H
  2. #define TESTDLL_H
  3. #include "testdll_global.h"
  4. class TESTDLL_EXPORT TestDll
  5. {
  6. public:
  7. TestDll();
  8. virtual~TestDll();
  9. virtual void helloWorld(); //类成员函数
  10. private:
  11. };
  12. extern "C" TESTDLL_EXPORT TestDll* getTestDll(); //获取类TestDll的对象
  13. #endif // TESTDLL_H

TestDll.cpp源码

  1. #include <iostream>
  2. #include "TestDll.h"
  3. TestDll::TestDll()
  4. {
  5. }
  6. TestDll::~TestDll()
  7. {
  8. }
  9. void TestDll::helloWorld()
  10. {
  11. std::cout << "hello,world!";
  12. }
  13. TestDll* getTestDll()
  14. {
  15. return new TestDll();
  16. }

TestMain项目中的main.cpp源码

  1. #include <QtCore/QCoreApplication>
  2. #include <iostream>
  3. #include <QLibrary>
  4. #include "../TestDll/TestDll.h"  //头文件还是需要加的,否则无法解析TestDll类
  5. typedef TestDll* (*GetTestDll)();//定义函数指针,获取类TestDLL对象;
  6. int main(int argc, char *argv[])
  7. {
  8. QCoreApplication a(argc, argv);
  9. QLibrary mylib("TestDll.dll");   //声明所用到的dll文件
  10. int result;
  11. //判断是否正确加载
  12. if (mylib.load())
  13. {
  14. GetTestDll getTestDll = (GetTestDll)mylib.resolve("getTestDll");
  15. if(getTestDll)
  16. {
  17. TestDll *testDll = getTestDll();
  18. testDll->helloWorld();
  19. delete testDll;
  20. }
  21. }
  22. //加载失败
  23. else
  24. std::cout << "DLL is not loaded!"<<std::endl;
  25. return a.exec();
  26. }

这个方法的使用得用户可以很容易地为你的程序制作插件。它的缺点是创建对象的内存必须在dll中分配

②用GetProcAddress直接调用类对象中的成员函数

这个方法,我没测试,对我没对大作用,还得用def导出DLL函数,有兴趣的就参考一下这篇文章。DLL中类的显式链接

用Qt的QPluginLoader类直接调用生成的DLL插件类对象

这个方法,我单独写一篇总结,请看QPluginLoader的简单小例子VS2008+Qt 使用QPluginLoader访问DLL

3、采用隐式链接方法,通过QLibrary类对DLL中类对象、全局函数的调用

TestDll.h

  1. #ifndef TESTDLL_H
  2. #define TESTDLL_H
  3. #include "testdll_global.h"
  4. class TESTDLL_EXPORT TestDll
  5. {
  6. public:
  7. TestDll();
  8. ~TestDll();
  9. void helloWorld(); //类成员函数
  10. private:
  11. };
  12. extern "C" TESTDLL_EXPORT int add(int a,int b);  //自定义的外部函数
  13. #endif // TESTDLL_H

TestDll.cpp源码

  1. #include <iostream>
  2. #include "TestDll.h"
  3. TestDll::TestDll()
  4. {
  5. }
  6. TestDll::~TestDll()
  7. {
  8. }
  9. void TestDll::helloWorld()
  10. {
  11. std::cout << "hello,world!";
  12. }
  13. int add(int a,int b)
  14. {
  15. return a + b;
  16. }

TestMain项目中的main.cpp ,需要稍微配置头文件和lib文件

1、在项目中主程序引入TestDll.h头文件,

2、配置项目属性:加入TestDLL.lib的文件目录,在Linker/General/Additional Library Diretories里面选择TestDll.lib的文件目录D:\VSWorkSpace\Test\Debug

3、配置项目属性:加入TestDll.lib文件,在Linker/Input/Additional Dependencies 中加入 TestDll.lib

main.cpp源码

  1. #include <QtCore/QCoreApplication>
  2. #include <iostream>
  3. #include <QLibrary>
  4. #include "../TestDll/TestDll.h"
  5. //引入TestDll.lib文件,和上面的2,3步工作同理
  6. //#pragma comment(lib, "../Debug/TestDll.lib")
  7. int main(int argc, char *argv[])
  8. {
  9. QCoreApplication a(argc, argv);
  10. int result = add(5,6);
  11. std::cout << result;
  12. TestDll dll;
  13. dll.helloWorld();
  14. return a.exec();
  15. }

结果即可编译成功