声明:
事先我已经自己动手写了一个简单的dll文件(myDLL.dll),C版接口的。并且用我前两篇有关DLL文章里面的方法,从dll中导出了导入库(.lib)文件,dll中有两个函数,原型如下:
void
HelloWorld();
//函数内部调用Win32 API,功能是弹出一个helloworld提示框
int
add(
int
a,
int
b);
//实现两个数相加,并返回结果
下面分别通过显示调用和隐式调用两种方法,来模拟Qt如何调用外部dll文件中的功能函数,follow me....
预备知识:
1、如果在没有导入库文件(.lib),而只有头文件(.h)与动态链接库(.dll)时,我们才需要显示调用,如果这三个文件都全的话,我们就可以使用简单方便的隐式调用。
2、通常Windows下程序显示调用dll的步骤分为三步(三个函数):LoadLibrary()、GetProcAdress()、FreeLibrary()
其中,LoadLibrary() 函数用来载入指定的dll文件,加载到调用程序的内存中(DLL没有自己的内存!)
GetProcAddress() 函数
检索指定的动态链接库(DLL)中的输出库函数地址,以备调用
FreeLibrary() 释放dll所占空间
1、
显示调用
Qt提供了一个 QLibrary 类供显示调用。下面给出一个完整的例子:
1
#include
<
QApplication
>
2
#include
<
QLibrary
>
3
#include
<
QDebug
>
4
#include
<
QMessageBox
>
5
#include
"
dll.h
"
//引入头文件
6
typedef
int
(
*
Fun)(
int
,
int
);
//定义函数指针,以备调用
7
int
main(
int
argc,
char
**
argv)
8
{
9
QApplication app(argc,argv);
10
QLibrary mylib(
"
myDLL.dll
"
);
//声明所用到的dll文件
11
int
result;
12
if
(mylib.load())
//判断是否正确加载
13
{
14
QMessageBox::information(NULL,
"
OK
"
,
"
DLL load is OK!
"
);
15
Fun open
=
(Fun)mylib.resolve(
"
add
"
);
//援引 add() 函数
16
if
(open)
//是否成功连接上 add() 函数
17
{
18
QMessageBox::information(NULL,
"
OK
"
,
"
Link to Function is OK!
"
);
19
result
=
open(
5
,
6
);
//这里函数指针调用dll中的 add() 函数
20
qDebug()
<<
result;
21
}
22
else
23
QMessageBox::information(NULL,
"
NO
"
,
"
Linke to Function is not OK!!!!
"
);
24
}
25
else
26
QMessageBox::information(NULL,
"
NO
"
,
"
DLL is not loaded!
"
);
27 return 0;
//加载失败则退出
28
}
myDLL.dll为自定义的dll文件,将其复制到程序的输出目录下就可以调用。显然,显示调用代码书写量巨大,实在不方便。
2、
隐式调用
这个时候我们需要三个文件,头文件(.h)、导入库文件(.lib)、动态链接库(.dll),具体步骤如下:
1、
首先我们把 .h 与 .lib/.a 文件复制到程序当前目录下,然后再把dll文件复制到程序的输出目录,
2、
下面我们在pro文件中,添加 .lib 文件的位置:
LIBS
+=
-L
D:/hitempt/api/
-l
myDLL
-L 参数指定 .lib/.a 文件的位置
-l 参数指定导入库文件名(不要加扩展名)
另外,导入库文件的路径中,反斜杠用的是向右倾斜的
3、
在程序中include头文件(我试验用的dll是用C写的,因此要用
extern "C" { #include "dll.h" }
)
下面是隐式调用的实例代码:
1
#include
<
QApplication
>
2
#include
<
QDebug
>
3
extern
"
C
"
//由于是C版的dll文件,在C++中引入其头文件要加extern "C" {},注意
4
{
5
#include
"
dll.h
"
6
}
7
int
main(
int
argv ,
char
**
argv)
8
{
9
QApplication app(argv,argv);
10
HelloWordl();
//
调用Win32 API 弹出helloworld对话框
11
qDebug()
<<
add(
5
,
6
);
//
dll 中我自己写的一个加法函数
12
return
0;
//完成使命后,直接退出,不让它进入事件循环
13
}
还是隐式调用方便啊,直接就可以调用dll中的函数...