Home » C++

A Simple Plugin Architecture For C++

18 June 2003 131,506 views 6 Comments

A Simple Plugin Architecture For C++ continued…

The PluginManager looks like this:

using namespace std;

class PluginManager
   PluginManager() {}
   ~PluginManager() { clearPluginList(); }

   void getPluginList(char * dirPath, bool addToList = false);
   PluginInterface * makeNewPluginInstance(int index)
      return pluginRegister.at(index)->makeNewInstance();
   int getNumPlugins() 
      return int(pluginRegister.size()); 
   char * getPluginName(int index) 
      return pluginRegister.at(index)->getName();
   char * getPluginType(int index)
      return pluginRegister.at(index)->getType();

   void clearPluginList();
      for (unsigned int i = 0; i < pluginRegister.size(); i++)
         delete pluginRegister.at(i);		
   vector <PluginDetails*> pluginRegister;

Here we’re using the Vector template from the Standard Template Library to store instances of the PluginDetails class. You will need to #include <vector> and declare using namespace std; for this to compile successfully. I’ve placed the functions inline so that you can see how simple this class is.

The only function that isn’t inlined in the header file is the "getPluginList( )" function, which is shown on the next page.

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


  • 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 🙂


  • 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:

  • AnhMV said:

    Thanks for this post