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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…