#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);