





//可以使用QStringLiteral(“中文”)宏,但是这样讲就不能使用tr()函数了
//第二种办法
#if _MSC_VER >= 1600
#pragma execution_charater_set("utf-8")
#endif
//这样任然可以使用tr()
在项目中新建一个include文件夹(名字随意),将lib/a静态库和.h文件放进去


调用分为显式调用和隐式调用,都需要将dll复制到执行目录下
如果在没有导入库文件(.lib),而只有头文件(.h)与动态链接库(.dll)时,我们才需要显示调用,如果这三个文件都全的话,我们就可以使用简单方便的隐式调用
//显式调用
#include <QApplication>
#include <QLibrary>
#include <QMessageBox>
#include "dll.h" //引入头文件,不这样也可以
typedef int (*Fun)(int,int); //定义函数指针,以备调用
int main(int argc,char **argv)
{
QApplication app(argc,argv);
QLibrary mylib("myDLL.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; //加载失败则退出28}隐式调用
首先我们把 .h 与 .lib/.a 文件复制到程序当前目录下,然后再把dll文件复制到程序的输出目录
下面我们在pro文件中,添加 .lib 文件的位置: LIBS+= -LD:/hitempt/api/ -lmyDLL
-L 参数指定 .lib/.a 文件的位置
-l 参数指定导入库文件名(不要加扩展名)
另外,导入库文件的路径中,反斜杠用的是向右倾斜的 #include <QApplication>
#include <QDebug>
extern "C" //由于是C版的dll文件,在C++中引入其头文件要加extern "C" {},注意
{
#include "dll.h"
}
int main(int argv ,char **argv)
{
QApplication app(argv,argv);
HelloWordl(); //调用Win32 API 弹出helloworld对话框
qDebug()<<add(5,6); // dll 中我自己写的一个加法函数
return 0; //完成使命后,直接退出,不让它进入事件循环
}qDebug()<<add(5,6); // dll 中我自己写的一个加法函数 return 0; //完成使命后,直接退出,不让它进入事件循环 }