显示调用动态库
#include <QLibrary>
// 定义函数指针
typedef char* (*pcfun)(void);
QLibrary dll("usbrd32.dll"); // 声明所用到的dll文件
// 加载动态库
if (dll.load())
{
ui->textBrowser->append("load successful");
}
else
{
ui->textBrowser->append("load failed");
}
// 通过函数名称载入函数
pcfun file_info =(pcfun)dll.resolve("file_info");
if (file_info)
{
file_info(); // 执行函数
}
else
{
ui->textBrowser->append("resolve failed");
}
显式调用仅需要usbrd32.dll一个文件复制到生成目录中即可;
显式调用需要显式的导出每个函数,使用起来太麻烦,所以还是用隐式调用。
动态链接库隐式调用: 隐式调用需要h文件及lib文件
将usbrd32.lib,usbrd32.h文件复制到工程源文件目录中;
将usbrd32.dll文件复制到生成目录;
修改工程文件(.pro),添加一行
# -l指定的库不带lib扩展名
# -L指定lib文件所在的目录
LIBS+= -L E:\code\qt\HelloWorld -lusbrd32
直接调用即可:
dll_version();
注意事项:
软件发布时,不会自动复制用户的dll文件,需要手动复制至发布目录中;
lib文件与VC格式相同,VC编译时自动生成的就可以直接用(与C++Builder不同);
头文件里要用extern "C"{}圈起来(也可以在文件中圈)
从动态链接库里导出lib文件: 需要VS支持(VS6)
导出函数列表:
dumpbin /exports usbrd32.dll > usbrd32.def
内容如下:
Microsoft (R) COFF Binary File Dumper Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
Dump of file usbrd32.dll
File Type: DLL
Section contains the following exports for usbrd32.dll
0 characteristics
54D07173 time date stamp Tue Feb 03 14:57:55 2015
0.00 version
1 ordinal base
61 number of functions
61 number of names
ordinal hint RVA name
1 0 00002720 active_card
2 1 00001FE0 close_device
3 2 00002230 connect_device
此处省略干行 ......................
Summary
3000 .data
2000 .rdata
1000 .reloc
7000 .text
修改def文件如下:(建议用脚本修改)
LIBRARY export
EXPORTS
active_card @1
close_device @2
connect_device @3
通过def文件生成lib文件:仅用于动态链接库的隐式调用
lib /def:usbrd32.def /machine:i386 /out:usbrd32.lib
- 阅读全文 -