Sign in to follow this  
jakesee

Possible 2-way dependency for plugin dll?

Recommended Posts

jakesee    100
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
Mike nl    390
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this