qt5 调用 第三方dll 库

qt5 调用 第三方dll 库
win7 64位系统下面
完整测试代码:
https://blog.csdn.net/wowocpp/article/details/112987716
在这里插入图片描述
pro文件

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

win32: LIBS += -L$$PWD/CardReaderLib -lsl_icread_hlj

HEADERS += \
    sl_icread_hlj.h

在这里插入图片描述
运行log
在这里插入图片描述

main.cpp

#include <QCoreApplication>
#include <QLibrary>
#include <QDebug>

#define WINAPI      __stdcall

typedef int (*Fun)(int,int);

typedef int(WINAPI *lpJT_OpenReader)(int);
typedef int(WINAPI *lpJT_CloseReader)();
typedef int(WINAPI *lpJT_OpenCard)();
typedef int(WINAPI *lpJT_CloseCard)();
typedef unsigned short(WINAPI *lpJT_ProCommand)(unsigned char *, int , unsigned char *, int *);
typedef int(WINAPI *lpJT_SelectPSAMSlot)(int);
typedef int(WINAPI *lpJT_SamReset)(unsigned char *, int *);

typedef unsigned short(WINAPI *lpJT_SamCommand)(unsigned char *, int , unsigned char *, int *);


typedef unsigned long DWORD;

typedef void  (WINAPI *lpJT_GetCardSer)(DWORD * );

typedef int(WINAPI *lpOpenReader)(int);
typedef int(WINAPI *lpclose_reader)();
typedef int(WINAPI *lpOpenCard)();
typedef int(WINAPI *lpclosecard)();
typedef int(WINAPI *lpGetCardType)();
typedef int(WINAPI *lpGetCardSer)();
typedef int(WINAPI *lpSelectIcSlot)(unsigned char, unsigned char, char *);
typedef int(WINAPI *lpChange_SAMBanudrate)(unsigned char, unsigned char, unsigned char, char*);
typedef int(WINAPI *lpsam_command_hnew)(unsigned char, unsigned char, unsigned char, char*, char*);
typedef int(WINAPI *lpsam_reset_h)(char*);
typedef int(WINAPI *lppro_command_h)(char*, char*);
typedef int(WINAPI *lpreadblock_Ex)(int, char*);
typedef int(WINAPI *lpreadblock_h_Ex)(int, char*);
typedef int(WINAPI *lpwriteblock_Ex)(int, char*);
typedef int(WINAPI *lploadkey_Ex)(int, int, char*);

lpJT_OpenReader JT_OpenReader;
lpJT_CloseReader JT_CloseReader;
lpJT_OpenCard JT_OpenCard;
lpJT_CloseCard JT_CloseCard;
lpJT_ProCommand JT_ProCommand;
lpJT_SelectPSAMSlot JT_SelectPSAMSlot;
lpJT_SamReset JT_SamReset;
lpJT_SamCommand JT_SamCommand;
lpJT_GetCardSer JT_GetCardSer;

lpOpenReader OpenReader;
lpclose_reader close_reader;
lpOpenCard OpenCard;
lpclosecard closecard;
lpGetCardType GetCardType;
lpGetCardSer GetCardSer;
lpSelectIcSlot SelectIcSlot;
lpChange_SAMBanudrate Change_SAMBanudrate;
lpsam_command_hnew sam_command_hnew;
lpsam_reset_h sam_reset_h;
lppro_command_h pro_command_h;
lpreadblock_Ex readblock_Ex;
lpreadblock_h_Ex readblock_h_Ex;
lpwriteblock_Ex writeblock_Ex;
lploadkey_Ex loadkey_Ex;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QLibrary mylib("sl_icread_hlj.dll");

    JT_OpenReader = (lpJT_OpenReader)mylib.resolve("JT_OpenReader");
    if (JT_OpenReader == NULL)
    {
        qDebug()<< "Load JT_OpenReader Fail";

    }else{

        qDebug()<< "Load JT_OpenReader Success";
    }

    JT_OpenReader = (lpJT_OpenReader)mylib.resolve("JT_OpenReader");
    if (JT_OpenReader == NULL)
    {
        qDebug()<< "Load JT_OpenReader Fail";

    }else{

        qDebug()<< "Load JT_OpenReader Success";
    }

    JT_CloseReader = (lpJT_CloseReader)mylib.resolve("JT_CloseReader");
    if (JT_CloseReader == NULL)
    {
        qDebug()<< "Load JT_CloseReader Fail";

    }else{

        qDebug()<< "Load JT_CloseReader Success";
    }

    JT_OpenCard = (lpJT_OpenCard)mylib.resolve("JT_OpenCard");
    if (JT_OpenCard == NULL)
    {
        qDebug()<< "Load JT_OpenCard Fail";

    }else{

        qDebug()<< "Load JT_OpenCard Success";
    }

    JT_CloseCard = (lpJT_CloseCard)mylib.resolve("JT_CloseCard");
    if (JT_CloseCard == NULL)
    {
        qDebug()<< "Load JT_CloseCard Fail";

    }else{

        qDebug()<< "Load JT_CloseCard Success";
    }

    JT_ProCommand = (lpJT_ProCommand)mylib.resolve("JT_ProCommand");
    if (JT_ProCommand == NULL)
    {
        qDebug()<< "Load JT_ProCommand Fail";

    }else{

        qDebug()<< "Load JT_ProCommand Success";
    }

    JT_SelectPSAMSlot = (lpJT_SelectPSAMSlot)mylib.resolve("JT_SelectPSAMSlot");
    if (JT_SelectPSAMSlot == NULL)
    {
        qDebug()<< "Load JT_SelectPSAMSlot Fail";

    }else{

        qDebug()<< "Load JT_SelectPSAMSlot Success";
    }

    JT_SamReset = (lpJT_SamReset)mylib.resolve("JT_SamReset");
    if (JT_SamReset == NULL)
    {
        qDebug()<< "Load JT_SamReset Fail";

    }else{

        qDebug()<< "Load JT_SamReset Success";
    }

    JT_SamCommand = (lpJT_SamCommand)mylib.resolve("JT_SamCommand");
    if (JT_SamCommand == NULL)
    {
        qDebug()<< "Load JT_SamCommand Fail";

    }else{

        qDebug()<< "Load JT_SamCommand Success";
    }

    OpenReader = (lpOpenReader)mylib.resolve("OpenReader");
    if (OpenReader == NULL)
    {
        qDebug()<< "Load OpenReader Fail";

    }else{

        qDebug()<< "Load OpenReader Success";
    }

    close_reader = (lpclose_reader)mylib.resolve("close_reader");
    if (close_reader == NULL)
    {
        qDebug()<< "Load close_reader Fail";

    }else{

        qDebug()<< "Load close_reader Success";
    }

    OpenCard = (lpOpenCard)mylib.resolve("OpenCard");
    if (OpenCard == NULL)
    {
        qDebug()<< "Load OpenCard Fail";

    }else{

        qDebug()<< "Load OpenCard Success";
    }

    closecard = (lpclosecard)mylib.resolve("closecard");
    if (closecard == NULL)
    {
        qDebug()<< "Load closecard Fail";

    }else{

        qDebug()<< "Load closecard Success";
    }

    GetCardType = (lpGetCardType)mylib.resolve("GetCardType");
    if (GetCardType == NULL)
    {
        qDebug()<< "Load GetCardType Fail";

    }else{

        qDebug()<< "Load GetCardType Success";
    }

    GetCardSer = (lpGetCardSer)mylib.resolve("GetCardSer");
    if (GetCardSer == NULL)
    {
        qDebug()<< "Load GetCardSer Fail";

    }else{

        qDebug()<< "Load GetCardSer Success";
    }

    SelectIcSlot = (lpSelectIcSlot)mylib.resolve("SelectIcSlot");
    if (SelectIcSlot == NULL)
    {
        qDebug()<< "Load SelectIcSlot Fail";

    }else{

        qDebug()<< "Load SelectIcSlot Success";
    }

    Change_SAMBanudrate = (lpChange_SAMBanudrate)mylib.resolve("Change_SAMBanudrate");
    if (Change_SAMBanudrate == NULL)
    {
        qDebug()<< "Load Change_SAMBanudrate Fail";

    }else{

        qDebug()<< "Load Change_SAMBanudrate Success";
    }

    sam_command_hnew = (lpsam_command_hnew)mylib.resolve("sam_command_hnew");
    if (sam_command_hnew == NULL)
    {
        qDebug()<< "Load sam_command_hnew Fail";

    }else{

        qDebug()<< "Load sam_command_hnew Success";
    }

    sam_reset_h = (lpsam_reset_h)mylib.resolve("sam_reset_h");
    if (sam_reset_h == NULL)
    {
        qDebug()<< "Load sam_reset_h Fail";

    }else{

        qDebug()<< "Load sam_reset_h Success";
    }

    pro_command_h = (lppro_command_h)mylib.resolve("pro_command_h");
    if (pro_command_h == NULL)
    {
        qDebug()<< "Load pro_command_h Fail";

    }else{

        qDebug()<< "Load pro_command_h Success";
    }

    readblock_Ex = (lpreadblock_Ex)mylib.resolve("readblock_Ex");
    if (readblock_Ex == NULL)
    {
        qDebug()<< "Load readblock_Ex Fail";

    }else{

        qDebug()<< "Load readblock_Ex Success";
    }

    readblock_h_Ex = (lpreadblock_h_Ex)mylib.resolve("readblock_h_Ex");
    if (readblock_h_Ex == NULL)
    {
        qDebug()<< "Load readblock_h_Ex Fail";

    }else{

        qDebug()<< "Load readblock_h_Ex Success";
    }

    writeblock_Ex = (lpwriteblock_Ex)mylib.resolve("writeblock_Ex");
    if (writeblock_Ex == NULL)
    {
        qDebug()<< "Load writeblock_Ex Fail";

    }else{

        qDebug()<< "Load writeblock_Ex Success";
    }


    loadkey_Ex = (lploadkey_Ex)mylib.resolve("loadkey_Ex");
    if (loadkey_Ex == NULL)
    {
        qDebug()<< "Load loadkey_Ex Fail";

    }else{

        qDebug()<< "Load loadkey_Ex Success";
    }


    qDebug() << "--------------- start ----------------" << endl;

    int ret ;
    ret = JT_OpenReader(12); // COM12

    qDebug()<<QString("打开读卡器:ret = ") << ret << endl ;

    ret = JT_OpenCard();
    qDebug()<<QString("打开卡:ret = ") << ret << endl ;

    unsigned char repBuffer[600] = { 0 };
    unsigned char cmdBuffer[300] = { 0x00,0x84,0x00,0x00,0x04};
    int cmdLen = 5, repLen = 0;
    unsigned char tempBuffer[300] = { 0 };


    ret = JT_ProCommand(cmdBuffer, cmdLen, repBuffer, &repLen);

    repLen = strlen((char *)repBuffer);

    qDebug()<<QString("执行命令:ret = ") << ret << ",repLen = "<< repLen << endl ;
    for(int i = 0 ;i < repLen; i++){

        printf("%d---%x\n",i, repBuffer[i]);
    }

   // ret = JT_CloseCard();
    ret = JT_CloseReader();
    mylib.unload();
    return a.exec();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt中,第三方可以通过静态编译和动态编译两种方式进行使用。 1. 静态编译:静态编译是指将第三方的代码编译成静态(.a或.lib文件),并将静态链接到应用程序中,生成一个单独的可执行文件。这种方式需要在.pro文件中指定要链接的静态,例如: ``` LIBS += -lmylib ``` 其中,`mylib`是要链接的静态名字。 静态编译的优点是运行时不需要动态加载第三方,提高了启动速度和效率。但缺点是可执行文件较大,不易扩展和更新。 2. 动态编译:动态编译是指将第三方的代码编译成动态(.dll或.so文件),并在运行时通过libloaderapi函数手动加载动态,并通过QLibrary类获取动态中的函数地址,然后直接调用该函数。这种方式需要在代码中显式加载动态,例如: ``` QLibrary mylib("mylib.dll"); mylib.load(); ``` 然后通过QLibrary的resolve方法获取函数地址,例如: ``` void (*myFunc)() = (void (*)()) mylib.resolve("myFunc"); ``` 其中,`myFunc`是要调用的函数名。 动态编译的优点是灵活性高,可以根据实际需要动态加载和卸载第三方,便于扩展和更新。但缺点是运行时需要动态加载第三方,影响了启动速度和效率。 需要注意的是,在使用第三方时,需要遵循一些规范和最佳实践,以确保正确性和安全性。特别是在获取函数地址时,需要确保函数名的正确性和类型匹配,否则可能导致运行时错误和安全漏洞。同时,在编译和链接时,需要遵循第三方的使用规范,以确保文件的正确链接和使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值