Home » C++

A Simple Plugin Architecture For C++

18 June 2003 131,503 views 6 Comments

A Simple Plugin Architecture For C++ continued…

Here is an example of how your main application can use the PluginManager class:

#include <direct.h>

PluginManager pluginManager;

void getPluginList()
{
   char curpath[MAX_PATH];

   //Get the current working directory
   if( _getcwd( curpath, MAX_PATH ) == NULL )
   {
      MessageBox(hWnd,"Can't get current directory","Error",MB_OK);
      return;
   }
   pluginManager.getPluginList(curpath);
}

void executePlugin(int index)
{
   int numPlugins = pluginManager.getNumPlugins();
   PluginInterface * plugin = pluginManager.makeNewPluginInstance(index);
   
   plugin->Activate();
   plugin->Execute();
   plugin->Destroy();
   
   delete plugin;   
}

If your routine seems to be unable to find any plugins, then firstly check that the DLLs are being created in your executable’s directory, and make sure that the working directory for your application is also set to the same directory as your executable.

As a companion to this tutorial, I have created a Visual Studio.NET C++ project for you to download. It creates a dialog and displays a list of available plugins when a button is pressed. The appropriate plugin is executed when the user double clicks on its list entry. The solution comes with two plugin projects that are created in the executable’s directory.

Download: Tutorial03.zip (14.6 Kb)
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