GithubHelp home page GithubHelp logo

chenry-github / dynamiclibloaddemo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kevinlq/dynamiclibloaddemo

0.0 0.0 0.0 22 KB

QLibrary 加载动态库详细用法,封装调用方法,使用简单快速、方法使用 Qt 反射更快捷

License: GNU General Public License v3.0

QMake 24.03% C++ 62.56% C 13.40%

dynamiclibloaddemo's Introduction

DynamicLibLoadDemo

QLibrary 加载动态库详细用法,封装调用方法,使用简单快速、方法使用 Qt 反射更快捷

核心代码

typedef int (* pRunFunctionFun)(const char *, const DS_Request &, DS_Request&);

#define GET_SDK_FUNC(type, func, symbol)  GET_FUNC(sdk, type, func, symbol);

#define GET_FUNC(sdk, type, func, symbol) \
    type func = getFunction<type>(&sdk, symbol); \
    if (func == nullptr) \
{ \
    qDebug()<<"fun is null: "<< symbol; \
    return 0; \
}

template <typename T>
T getFunction(QLibrary *lib, const char *func)
{
    T f = (T)lib->resolve(func);
    if (f == nullptr)
    {
        return nullptr;
    }
    return f;
}

使用

    QString strSDKName = QString("%1%2%3").arg(LIB_DIR_PATH).arg(LIB_NAME).arg(FILE_DLL_EXT);

    QLibrary library(strSDKName);

    bool bLoad = library.load();
	
	    // 调用方法1
    GET_FUNC(library, pRunFunctionFun, runFun, "runFunction");

    int nRet = runFun("addNum", request, response);

    qDebug() << "add Resut:" << nRet << response.m_strData;
	

反射调用

int runFunction(const char *funName, const DS_Request &request, DS_Request &response)
{
    Internal::SDKCoreImpl m_pCoreImpl;

    int nRet = -1;
    QMetaObject::invokeMethod(&m_pCoreImpl, funName,
                              Qt::DirectConnection,
                              Q_RETURN_ARG(int, nRet),
                              Q_ARG(DS_Request, request),
                              Q_ARG(DS_Request&,response));

    return nRet;
}

总结

关于详细介绍以及使用过程,可以关注微信公众号: devstone, 获取查看

dynamiclibloaddemo's People

Contributors

kevinlq avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.