Home » C++

A Simple Plugin Architecture For C++

18 June 2003 127,916 views 6 Comments

A Simple Plugin Architecture For C++ continued…

These two extra functions in our plugin provide our application with strings containing the plugin name and type:

extern "C"
{
   __declspec(dllexport) char * getPluginName()
   {
      return "Plugin01";
   }

   __declspec(dllexport) char * getPluginType()
   {
      return "Plugin01_Class";
   }
}

Instead of insisting that plugin developers write out all these declarations in full, we can abbreviate them using macros. Copy and paste the following lines into your "PluginInterface.h" file.

#define DECLARE_PLUGIN(x) extern "C"{__declspec(dllexport) \
PluginInterface * makePlugin(){ return new x;}}
#define SET_PLUGIN_NAME(x) extern "C"{__declspec(dllexport) \
char * getPluginName(){ return x;}}
#define SET_PLUGIN_TYPE(x) extern "C"{__declspec(dllexport) \
char * getPluginType(){ return x;}}

This will allow us to simplify the declarations in our plugin to:

DECLARE_PLUGIN(Plugin01)
SET_PLUGIN_NAME("Plugin01")
SET_PLUGIN_TYPE("Plugin01_Class")

The final addition to make is to add a couple of function pointer typedefs to make the syntax a bit easier later on. Your final "PluginInterface.h" in your main application should look something like this:

#define DECLARE_PLUGIN(x) extern "C"{__declspec(dllexport) \
PluginInterface * makePlugin(){ return new x;}}
#define SET_PLUGIN_NAME(x) extern "C"{__declspec(dllexport) \
char * getPluginName(){ return x;}}
#define SET_PLUGIN_TYPE(x) extern "C"{__declspec(dllexport) \
char * getPluginType(){ return x;}}

class PluginInterface 
{
   public:
      PluginInterface(){}
      virtual ~PluginInterface(){}

      virtual int Activate()=0;
      virtual int Execute()=0;
      virtual int Destroy()=0;
};

typedef PluginInterface * (*PLUGIN_FACTORYFUNC)();
typedef char * (*PLUGIN_TEXTFUNC)();

On the next page, I’ll show you a finished plugin class.

1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 4.00 out of 5)
Loading...

6 Comments »

  • Coder said:

    Hey, thanks for posting this example. Also thanks for posting the C++ source code examples, very helpful! 🙂

  • AndyN (author) said:

    Hey, no problem! This is an old one. I’m happy you and others are still getting some use from this 🙂

    Andy

  • Browner87 said:

    You don’t have any example of how to return pointers from a function. Is this possible using ANSI C++? Can you return a (char *) from the shared library to the main program?

  • AndyN (author) said:

    It’s been a long time since I’ve looked into this, but I’m pretty sure the answer’s no. I have a feeling that the dll and the main program don’t share memory space, and if you try to free memory that was allocated by the other, it’ll throw an error.

    If that’s the case, then the only way you could make this work would be to pass some sort of factory function to the plugin to allocate memory in the right place. That way the responsibility lies with the main process to free the memory later, plus it can safely unload the dll at any stage without memory leaks.

  • Browner87 said:

    Well, the pointer returning does work (it was other code that I had that was casing the problem). As for memory leaks, I don’t know. I found a better to achieve what I wanted so I only did it for experience.

    May I suggest that Linux programmers however read the following article if you are trying to implement this on Linux:
    http://www.linuxjournal.com/article/3687

  • AnhMV said:

    Thanks for this post