#include <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();
}