打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Qt DLL总结【二】-创建及调用QT的 DLL - 柳北风儿~~~~~~~欲宇仙炅 - ITeye技术网站

 


目录


Qt DLL总结【一】-链接库预备知识


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


Qt DLL总结【三】-VS2008+Qt 使用QPluginLoader访问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 文件源代码一直不变


 


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

#include <QtCore/qglobal.h>

#ifdef TESTDLL_LIB
# define TESTDLL_EXPORT Q_DECL_EXPORT
#else
# define TESTDLL_EXPORT Q_DECL_IMPORT
#endif

#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源码


 


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

#include 'testdll_global.h'

class TESTDLL_EXPORT TestDll
{
public:
TestDll();
~TestDll();
private:


};
extern 'C' TESTDLL_EXPORT void helloWorld();
extern 'C' TESTDLL_EXPORT int add(int a,int b);
#endif // TESTDLL_H

 


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


 


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

TestDll::TestDll()
{

}

TestDll::~TestDll()
{

}

void helloWorld()
{
std::cout << 'hello,world!';
}
int add(int a,int b)
{
return a + b;
}

   注: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


 


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

typedef int (*Fun)(int,int); //定义函数指针,int add(int a,int b);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QLibrary mylib('TestDll.dll'); //声明所用到的dll文件
int result;
//判断是否正确加载
if (mylib.load())
{
std::cout << 'DLL load is OK!'<<std::endl;
//调用外部函数 add()
Fun add = (Fun)mylib.resolve('add');
//是否成功连接上 add() 函数
if (add)
{
std::cout << 'Link to add Function is OK!'<<std::endl;
//这里函数指针调用dll中的 add() 函数
result = add(5,6);
std::cout << result;
}
else
std::cout << 'Link to add Function failed!!'<<std::endl;


}
//加载失败
else
std::cout << 'DLL is not loaded!'<<std::endl;


return a.exec();

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


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


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


②用GetProcAddress直接调用。


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


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


TestDll.h代码


 


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

#include 'testdll_global.h'

class TESTDLL_EXPORT TestDll
{
public:
TestDll();
virtual~TestDll();
virtual void helloWorld(); //类成员函数
private:


};
extern 'C' TESTDLL_EXPORT TestDll* getTestDll(); //获取类TestDll的对象
#endif // TESTDLL_H

 


 TestDll.cpp源码


 


Cpp代码  
  1. #include <iostream>  
  2. #include 'TestDll.h'  
  3.   
  4. TestDll::TestDll()  
  5. {  
  6.   
  7. }  
  8.   
  9. TestDll::~TestDll()  
  10. {  
  11.   
  12. }  
  13.   
  14. void TestDll::helloWorld()  
  15. {  
  16.     std::cout << 'hello,world!';  
  17. }  
  18.   
  19. TestDll* getTestDll()  
  20. {  
  21.     return new TestDll();  
  22. }  
#include <iostream>
#include 'TestDll.h'

TestDll::TestDll()
{

}

TestDll::~TestDll()
{

}

void TestDll::helloWorld()
{
std::cout << 'hello,world!';
}

TestDll* getTestDll()
{
return new TestDll();
}

 


 TestMain项目中的main.cpp源码


 


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

QLibrary mylib('TestDll.dll'); //声明所用到的dll文件
int result;
//判断是否正确加载
if (mylib.load())
{
GetTestDll getTestDll = (GetTestDll)mylib.resolve('getTestDll');
if(getTestDll)
{
TestDll *testDll = getTestDll();
testDll->helloWorld();
delete testDll;
}
}
//加载失败
else
std::cout << 'DLL is not loaded!'<<std::endl;
return a.exec();
}

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


 


 


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


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


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


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


 


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


 


TestDll.h


 


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

#include 'testdll_global.h'

class TESTDLL_EXPORT TestDll
{
public:
TestDll();
~TestDll();
void helloWorld(); //类成员函数
private:


};
extern 'C' TESTDLL_EXPORT int add(int a,int b); //自定义的外部函数
#endif // TESTDLL_H

TestDll.cpp源码


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

TestDll::TestDll()
{

}

TestDll::~TestDll()
{

}

void TestDll::helloWorld()
{
std::cout << 'hello,world!';
}
int add(int a,int b)
{
return a + b;
}

 


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源码


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. }  
#include <QtCore/QCoreApplication>
#include <iostream>
#include <QLibrary>
#include '../TestDll/TestDll.h'
//引入TestDll.lib文件,和上面的2,3步工作同理
//#pragma comment(lib, '../Debug/TestDll.lib')
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int result = add(5,6);
std::cout << result;
TestDll dll;
dll.helloWorld();
return a.exec();
}


 结果即可编译成功


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
VC++创建、调用dll的方法步骤
C#调用C/C++动态链接库(.dll)详解——第三篇 C++调用.dll
C#调用DLL错误:PInvokeStackImbalance。
boost::thread线程创建方式总结
C++内存管理之shared
转载:VS2010,C++调用动态链接库
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服