Qt如何调用dll文件

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;  //加载失败则退出
     }
}

猜你喜欢

转载自blog.csdn.net/qq_40110291/article/details/78134124