• Advertisement
Sign in to follow this  

Possible 2-way dependency for plugin dll?

This topic is 2686 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I am thinking about a plugin architecture at the moment and is unclear how the following situation can be realised:


// in main.dll
struct _MAIN_EXPORT MainData { ... };
void _MAIN_EXPORT DoWorkWithPlugin(String plugin_path, MainData _data)
{
// some form of loading plugin dll business which i do not know how yet
// LoadLibrary? GetProcAddress?
// suppose i manage to find out the class name of the plugin is "Plugin"
// then...
IPlugin* plugin_interface = new Plugin();

// circular dependency?
// IPlugin needs to know about MainData before it can compile
// Main.dll need to know about IPlugin::do(DataMain) before it can compile?
plugin_interface->do(data);

// how to resolve this?
}

// in my application
#pragma comment("lib", "main.dll")
int main()
{
MainData data;
DoWorkWithPlugin("path\to\plugin.dll", data);
}



How can I set this up in a way it will work?

Share this post


Link to post
Share on other sites
Advertisement
No problem here. Both MainData and the IPlugin class would be defined in some header that gets included by both the main.dll source files as well as the plugin source files. Then both know about the data structures used.

By the way, you'll probably want to use GetProcAddress to get a function from the plugin.dll which creates and returns an instance of the plugin class.

Example: (it's been a while since I used LoadLibrary, etc, so treat this as some quickly-written pseudo-code)

custom_plugin.cpp:
#include "plugin_defs.h"

class MyCustomPlugin : public IPlugin
{
void DeleteThis() { delete this; }

void do(MainData& d) {};
};

IPlugin* EXPORT CreatePlugin()
{
return new MyCustomPlugin();
}



main.cpp:
#include "plugin_def.h"

void EXPORT DoWorkWithPlugin(String path, const MainData& data)
{
// Load library and get function
HANDLE hLib = LoadLibrary(path);
IPlugin* (*plugin_creator)() = GetProcAddress(hLib, "CreatePlugin");

// Create plugin
IPlugin* plugin = plugin_creator();

plugin->do(data);
}



Keep in mind that it's good practice that objects created in a DLL should be deleted in that DLL. This can be tricky when you're passing out pointers across DLL boundaries. This is why I defined the DeleteThis member in the example.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement