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 //加载失败则退出
}
}
采用的共享内存方式。第1个用来存储接收到的数据块,第2个用来放接收端(64位)的winid,主要是提供给发送32位的dll调用端读取,在接收到数据后通知64位的接收端有新的数据需要从共享内存中读取。
第3个共享内存区是同步信号区。