'how to register a callback function into shared library

my shared library (search.so) implements below interface as below

ILibrary.h

class ILibrary
{
   virtual void registerSearchResultCallback(std::function<void(const std::shared_ptr<Info::info>& myinfo)>) = 0;
   virtual void startSearch() = 0;
};

Library.h

class Library: public ILibrary {
public:
   void registerSearchResultCallback(std::function<void(const std::shared_ptr<Info::info>& myinfo)>);
   void startSearch();
private:
   std::function<void(const std::shared_ptr<Info::info>& myinfo)> callback;
   //other members and functions here
}

Library.cpp

Library::registerSearchResultCallback(std::function<void(const std::shared_ptr<Info::info>&  myInfo)> cb)
{
   callback = cb;   
}

This is how I load the library and calls my functions from main. how to register SearchResult function by calling registerSearchResultCallback here ? I am not sure how to use dlsymb calls for this and wrap into the wrapper class which written below. I prefer to write dlsymb calls inside the already written below Wrapper class

main.cpp

void SearchResult(const std::shared_ptr<Info::info>& myinfo)> info)
{
     //using info for further operation
}

void main()
{
    std::shared_ptr<Wrapper<ILibrary>> loader = std::make_shared<Wrapper<ILibrary>>("search.so");
    loader->OpenLib();
    std::shared_ptr<ILibrary> iLib= loader->Instance();

    //how to call my library's 'registerSearchResultCallback' 
    //to register 'SearchResult' function here ? Do we need to use 'dlSymb' calls
    //for this and if so how to write it in Wrapper class(below)

    //after that I call search function as below
    iLib->startSearch();
}

I have a already written wrapper class to wrap the dl calls as below

class Wrapper : public IWrapper<T>
    {
    private:
        void            *_handle;
        std::string     _pathToLib;
        std::string     _allocClassSymbol;
        std::string     _deleteClassSymbol;

    public:
        
        Wrapper (std::string const &pathToLib,
            std::string const &allocClassSymbol = "allocator",
            std::string const &deleteClassSymbol = "deleter") :
            _handle(nullptr), _pathToLib(pathToLib),
            _allocClassSymbol(allocClassSymbol), _deleteClassSymbol(deleteClassSymbol)
        {
        }

        ~Wrapper () = default;

        void OpenLib() override
        {
            if (!(_handle = dlopen(_pathToLib.c_str(), RTLD_NOW | RTLD_LAZY))) {
                std::cerr << dlerror() << std::endl;
            }
        }

        std::shared_ptr<T> Instance() override
        {
            using allocClass = T *(*)();
            using deleteClass = void (*)(T *);


            auto allocFunc = reinterpret_cast<allocClass>(
                    dlsym(_handle, _allocClassSymbol.c_str()));
            auto deleteFunc = reinterpret_cast<deleteClass>(
                    dlsym(_handle, _deleteClassSymbol.c_str()));

            if (!allocFunc || !deleteFunc) {
                DLCloseLib();
                std::cerr << dlerror() << std::endl;
            }

            return std::shared_ptr<T>(
                    allocFunc(),
                    [deleteFunc](T *p){ deleteFunc(p); });
        }

        void CloseLib() override
        {
            if (dlclose(_handle) != 0) {
                std::cerr << dlerror() << std::endl;
            }
        }
    };


Solution 1:[1]

how to call my library's 'registerSearchResultCallback' to register 'SearchResult' function here ?

If SearchResult is a standalone function, it should be simply:

iLib->registerSearchResultCallback(SearchResult);

If SearchResult() (and main()) is a non-static class method, it would need to look more this instead:

iLib->registerSearchResultCallback(
    [this](const std::shared_ptr<Info::info>& myinfo)> info){
        this->SearchResult(info);
    }
);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1