Home » C++

A Simple Plugin Architecture For C++

18 June 2003 131,500 views 6 Comments

A Simple Plugin Architecture For C++ continued…
Setting up communications

For the application to be able to use the class derived from PluginInterface, it needs some way to make an instance of it. So we create what is known as a factory function to construct the class for the application. The factory function is shown below, and we will use it in the main source file of the plugin, later on in this tutorial.

extern "C"
   __declspec(dllexport) PluginInterface * makePlugin()
      return new Plugin01;

The plugin’s factory function is creating the "Plugin01" class and returning it to the caller (the server). Before we progress any further, there are several parts to this code that should be explained.

  • extern "C"

    When the name of the factory function is stored inside the DLL, it is left unchanged providing we use this declaration. Without it, the function name undergoes C++ name mangling and our application won’t be able to find the function easily.To see name mangling in action, use the following command on your DLL.
    (Dumpbin.exe is included in the Visual Studio distribution)

    dumpbin dllname /exports
  • __declspec(dllexport)

    These storage-class attributes tell the compiler to expose the function for use external to the DLL. Without this, the function name would not be included in the DLL’s export table, and our application would be unable to find the function.

For added convenience and flexibility we will declare two more exported functions, and these are 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