qt5 调用 第三方dll 库

本文详细介绍了如何在Qt5环境中,在Win7 64位系统下加载并使用第三方DLL库sl_icread_hlj.dll,包括函数查找、链接和错误处理的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值