Plugin System.. help?

alkakios
alkakios used Ask the Experts™
on
Concerning the design of my 3d engine, I am thinking of using a plugin system. The idea is this: The main DLL, uses plugins to extend the engine's functionality. It should be able to load them on the fly, but I am not sure how to make a plugin system. I have been working on win32 dlls for quite a time, my previous engine design was a single win32 DLL.

Can someone help me here? I hope you understand my problem.
Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
hmm .. the most important and fundamental thing is an interface that the system provides for the plugin and the interface it expects from the plugin. as an abstract example

//plugin.dll

sysconsole* pcon = 0;

/*
 example that the plugin uses services from the system (what kind of services .. depends upon your imagination.
*/

bool EXPORT plugin_init(sysparams *prms)
{
    // cache the main consoles ptr for textout purposes
    pcon = prms->console;

    pcon->out_text("plugin init completed");
    // or in simple C
    out_text(pcon, "init complete");
}

bool EXPORT plugin_deinit()
{
    pcon->out_text("plugin shutdown");
}

this sample plugin just exports two methods (which are required by the interface). The system secifies that each plugin must export these two methods by name so as to get a starting point. It also publishes what would be passed to plugin and other details.

Now, again you might want the plugin to actually do something, for e.g. a plugin for a file loader might have to actually load a file. Depending upon your thought you might actually require the plugin to also export another function

bool plugin_loadfile(const char* pfile, void* pdata, long* plen)
{
 ...
}

....
yet another approach might be to pass a struct with pointer to functions and other members. for e.g.

typedef bool (*FUNC_LOAD)(const char* pfile, void* pdata, long* plen);

struct plugin_mod
{
     char name [128];
     ...
     FUNC_LOAD loadfile;
     ...
};

bool EXPORT plugin_init(sysparams *inparams, plugin_mod* outparams)
{
     strcpy(outparams->name, "myplugin 1.0");
     outparams->loadfile = my_loadfile;
     ..... etc.
}

bool my_loadfile(const char* pfile, void* pdata, long* plen)
{
...
}

yet another more robust aproach might be to use C++ classes, like requiring a plugin to do it the c++ way.

you publish a class say.

class plugin
{
public:
   virtual const char* get_name()=0;
   virtual bool loadfile(const char* file, ....)=0;
};

your plugin now simple provides a derivation of this class;

class my_plugin: public plugin
{
public:
   virtual const char& get_name() { return "myplugin"; }
   virtual bool loadfile(....) { .. }
};

however now the actual interface with the pkugin is not that straigtforward, because of the fact that it has to be dynamicaly linked you cannot export c++ interface and hence no classes can be passed around. you might have to hack around a bit thru pointers to get that working so use this approach if only required.

i hope you are not looking to COM specific or COM involved approaches because that by far is the best approach for plugin programming. considering the benefits and COM provides.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

now from the client/system side. There are different approaches with detection and loading of plugins. The most flexible and simple is just copying of plgugins in a specified folder.

At start the system enumerates all dlls/plugins in that folder and loads all that are avaialable. for e.g. pseudo code

file = findfirstfile("plugins/*.pll");
while(file)
{
    lib = loadlibrary(file);
    pinit = getprocaddr("plugin_init");

    if(!pinit) { not a plugin or corrupt }

    plugin_mod* pmod = new plugin_mod;
    if( pinit( g_sysparams, pmod ) )
    {
         g_pluginlist.add(pmod);
    }
    file = findnextfile("plugins/*.pll");
}

hope this helped a bit ...

Author

Commented:
Wow, you rock man, thanks totally.
YOU ROCK!

Commented:
This question didn't show any activity for more than 21 days. I will ask Community Support to close it unless you finalize it yourself within 7 days.
You can always request to keep this question open. But remember, experts can only help if you provide feedback to their comments.
Unless there is objection or further activity,  I will suggest to accept

    "ambience"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
========
Werner
Force accepted

** Mindphaser - Community Support Moderator **

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial