Qt如何调用dll文件

来源:互联网 发布:python编程工具 编辑:程序博客网 时间:2024/05/29 03:51
Qt提供了一个 QLibrary 类供显示调用。下面给出一个完整的例子:   testDLL.dll为自定义的dll文件,将其复制到程序的输出目录下就可以调用。
#include <QApplication> #include <QLibrary> #include <QDebug> #include <QMessageBox>  typedef int (*Fun)(int,int); //定义函数指针,以备调用 int main(int argc,char **argv) {     QApplication app(argc,argv);     QLibrary mylib("testDLL.dll");   //声明所用到的dll文件     int result;     if (mylib.load())              //判断是否正确加载     {         QMessageBox::information(NULL,"OK","DLL load is OK!");         Fun open=(Fun)mylib.resolve("add");    //援引 add() 函数         if (open)                  //是否成功连接上 add() 函数         {             QMessageBox::information(NULL,"OK","Link to Function is OK!");             result=open(5,6);      //这里函数指针调用dll中的 add() 函数             qDebug()<<result;         }         else             QMessageBox::information(NULL,"NO","Linke to Function is not OK!!!!");     }     else     {         QMessageBox::information(NULL,"NO","DLL is not loaded!");         return 0;  //加载失败则退出     }}

原创粉丝点击