dll用VC6编写,导出函数int myQtAdd(int a,int b);默认C调用约定
addDll.h
int myQtAdd(int a,int b);
addDll.cpp
#include "addDll.h"
int myQtAdd(int a,int b)
{
return a+b;
}
addDll.def
LIBRARY addDll
EXPORTS
myQtAdd
一、动态调用
在VC中,我们通过windows API loadLibraryA(W),加载dll,并把dll地址映射到进程空间。然后获取函数地址.用后FreeLibray释放dll。
在Qt中,通过QLibrary操作dll。
main.cpp
#include
#include
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLibrary myDll("addDll.dll");
if(!myDll.load())
{
qDebug()<<"load dll error";
}
else
{
qDebug()<<"load dll suncess";
typedef int(*FUN)(int,int);
FUN myAdd=(FUN)myDll.resolve("myQtAdd");
if(myAdd)
{
qDebug()<
需要注意的是QLibrary的析构函数:
Destroys the QLibrary object.
Unless unload() was called explicitly, the library stays in memory until the application terminates.
除非unload()函数被显示调用,否则库会保留在内存中直到程序终止。所以还是手动卸载下好。
另外就是dll一定要放在debug目录下,这一点不同于VC,我之前一直放在源文件目录,结果一直显示未找到dll。
二、静态调用
PS:这种方法暂时还没弄好,真想吐槽一下,网上的博文简直千篇一律。有的根本是错误的。