qt动态库加载


#include <QLibrary>
#include <QCoreApplication>


typedef	ULONG (STDCAL *TEST_MYCLASS)(int id,ULONG *p, ULONG *p);


class m_CLASS
{
public:
    m_CLASS(void);
    virtual ~m_CLASS(void);
public:
    BOOL	GetModule();
    void    FreeModuleX();

    static m_CLASS * instance()
    {
        static m_CLASS fucins;
        return &fucins;
    }

public:
    
    ULONG   TEST_MYCLASS(int id,ULONG *p, ULONG *p);


protected:

    TEST_MYCLASS m_test_myclass;

    QLibrary *pModule[3];

};

#endif // 

cpp

#include <QProcess>
#include <QDebug>


m_CLASS::m_CLASS()
{
    mm_test_myclass = nullptr;
}

CXXSKFFunction::~CXXSKFFunction()
{
    FreeModuleX();
    return;
}

BOOL CXXSKFFunction::GetModule()
{
    QString QStrPath[3];
    QStrPath[0] = "/lib/libansc11.so";
    QStrPath[1] = "/lib/libpkcs1.so";
    QStrPath[2] = "/lib/libInterface_Usb.so";
    for(int i = 0; i < 3; i++)
    {
        QString path = QCoreApplication::applicationDirPath() + QStrPath[i];
        pModule[i] = new QLibrary(path);
        if(pModule[i] == nullptr)
        {
            qDebug() << QString::fromLocal8Bit("动态库[%1]加载失败!nullptr").arg(path);
            return false;
        }
        pModule[i]->setLoadHints(QLibrary::ExportExternalSymbolsHint);
        if(!pModule[i]->load())
        {
            qDebug() << QString::fromLocal8Bit("动态库[%1]加载失败![%2]").arg(path).arg(pModule[i]->errorString());
            return false;
        }
    }
       mm_test_myclass = (TEST_MYCLASS)pModule[2]->resolve("库里的函数名");
	mm_test_myclass = (TEST_MYCLASS)pModule[2]->resolve("库里的函数名");
	mm_test_myclass = (TEST_MYCLASS)pModule[2]->resolve("库里的函数名");
	mm_test_myclass = (TEST_MYCLASS)pModule[2]->resolve("库里的函数名");
	mm_test_myclass = (TEST_MYCLASS)pModule[2]->resolve("库里的函数名");
	mm_test_myclass = (TEST_MYCLASS)pModule[2]->resolve("库里的函数名");
	mm_test_myclass = (TEST_MYCLASS)pModule[2]->resolve("库里的函数名");

    if ( nullptr != mm_test_myclass ) && )
    {
        qDebug() << QString::fromLocal8Bit("动态库加载成功!");
        return true;
    }
    else
    {
        qDebug() << QString::fromLocal8Bit("动态库加载失败!");
        FreeModuleX();
        return false;
    }
}

void m_CLASS::FreeModuleX()
{
    mm_test_myclass = nullptr;

    for(int i = 0; i < 3; i++)
    {
        if(pModule[i] != nullptr)
        {
            pModule[i]->unload();
            delete pModule[i];
            pModule[i] = nullptr;
        }
    }
}

ULONG   m_CLASS::自己封装的一层(int id,ULONG *p, ULONG *p)
{
    if(mm_test_myclass)
    {
        ULONG ulRet = mm_test_myclass(id,p,p);
        return ulRet;
    }
    return -1;
}


示例

m_CLASS::instance()->自己封装的一层(666, p,p);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值