Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

c++ - Loading dll at runtime

I need to load a DLL at runtime. I don't know in advance which DLL to load, but DLLs are the implementation of an interface (pure virtual method). The final goal is to have a pointer to a DLL to call its methods.

Now, I want to test just the DLL loading, calling a test method, but I fail. The test method is void test().

#if defined (_WIN32)
const path PluginExtension(".dll");
#define STDCALL __stdcall
#else
const path PluginExtension(".so");
#define STDCALL
#endif

extern "C"
{    
    typedef void*  (STDCALL* CreatorFunction)();
        
    constexpr auto FunctionName{ "CommandGeneratorEngine::Interface::test" };
}

auto library = LoadLibraryExW(pluginPath.native().c_str(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH);
if (library != nullptr) {
                
    auto creator = (CreatorFunction)GetProcAddress(library, FunctionName);
    if (creator != nullptr) {
        // do something 
    }
    else
    {
        throw std::exception("Error when loading comm plugin");
    }

The interface

namespace TGComm {
namespace CommandGeneratorEngine {
    
class Interface {
public:
        
    using Ptr = std::shared_ptr<Interface>;
    virtual ~Interface() = default;
    
    virtual  void test() = 0;
         
};

}
}

And the interface implementation:

class LHFImplementationInterface : public CommandGeneratorEngine::Interface
{
public:

    void __declspec(dllexport) __stdcall test() override{
        // do something...
    }
};

The line

auto creator = (CreatorFunction)GetProcAddress(library, FunctionName);

returns a null value.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Solved.

I created a new header file, h1.h

#include "Global.h"

extern "C" {

LHCOMMQT_LIB void* createTGCommPlugin();

}

its cpp is

#include "h1.h"
#include "LHFImplementationinterface.h"

void* createTGCommPlugin() {
    auto p = new LHFImplementationInterface();
    return p;
}

and global.h is

#ifndef LHCOMM_GLOBAL_H
#define LHCOMM_GLOBAL_H

#include <QtCore/qglobal.h>

#ifdef LHCOMMQT_EXPORTS
# define LHCOMMQT_LIB Q_DECL_EXPORT
#else // ifdef LHCOMMQT_EXPORTS
# define LHCOMMQT_LIB Q_DECL_IMPORT
#endif // ifdef LHCOMMQT_EXPORTS

#endif // LHCOMM_GLOBAL_H

in main:

typedef void* (STDCALL* CreatorFunction)();
constexpr auto FunctionName{ "createTGCommPlugin" };
try {
    auto library = LoadLibraryExW(pluginPath.native().c_str(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH);
    if (library != nullptr) {
    
        auto creator = (CreatorFunction)GetProcAddress(library, FunctionName);
...

and now works good


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...