概述
应用软件通过调用密码卡接口库,实现与密码卡硬件的连接和密码服务调用。具体实现方式分为显示调用和隐式调用两种。
一、显式调用
显式调用涉及到的第三方库文件为:.dll和.h文件,具体实现步骤如下:
(1)定义目录
QString strADD= "/usr/lib/libsdf_crypto.so";
(2)创建库对象
#include <QLibrary>
QLibrary libXXX(strADD);
(3)加载动态库
LibXXX.load();
(4)援引库函数
typedef int (*Fun)(int,int); //定义函数指针,以备调用
Fun fxone=(Fun)LibXXX.resolve(“ fxone”);
二、显式调用代码示例
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}
三、隐式调用
隐式调用涉及到的第三方库文件为:.dll、.h、.a/.lib文件,具体实现步骤如下:
(1).h与.lib/.a文件复制到程序当前目录下
(2)dll文件复制到程序的输出目录
(3)在pro文件中,添加 .lib 文件的位置
LIBS+= -L D:/hitempt/api/ -l myDLL
(4)include头文件
C版本的dll需要:
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 }