Home » C++

A Simple Plugin Architecture For C++

18 June 2003 87,343 views 6 Comments

There aren’t many Windows applications that can’t benefit from a plugin architecture. Not only does it allow you to expand your application’s functionality, but it also allows other developers to contribute as well. This is often instrumental in building a loyal customer base for an application.

This tutorial is based in Visual Studio.NET, but it can be followed using any C++ environment. If you aren’t using NET, then you’ll have to ignore any references to "solutions". This is simply NET talk for being able to group a number of projects together, and in previous versions of Visual Studio was known as a workspace.

This tutorial starts by creating the C++ interface which specifies how the plugin interacts with the main application (which will often be refered to as the server). It then covers the creation of a plugin application using a standard Win32 DLL, before finally looking at getting the server to scan a directory and load any suitable plugins that are available.

You can download the complete source code at the end of this tutorial, so don’t worry if you don’t get to see the implementation for every class member function.

The plugin base class

Create a basic Win32 C++ application called "PluginServer" and add a new header file called &quotPluginInterface.h". This will be the file that you distribute to third party developers so that they can create plugins for your application. Copy and paste the plugin base class shown below into your header file.

class PluginInterface 
      virtual ~PluginInterface(){}

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

Here I’ve created a pure virtual class. A pure virtual class is a class that cannot be created by using new, instead it must be derived from and have definitions for the pure virtual functions (functions defined as &quot =0 &quot) before an instance of a derived class can be made. This forces anyone creating a plugin to follow our specifications for the plugin interface.

This class has three member functions for the main application (the server) to interface with. These three functions are just to illustrate how to define the plugin base class, so you can define your own functions here if you like, as long as they are pure virtual functions.

The next page introduces a method of communication between the application and the plugin.

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