Sunday, November 2, 2014

Creating Basic C++ Plugins (Part 3 of 3)

Part 1 introduced the basics of the plugin system. Part 2 created a class of shared libraries, then showed a sample program to load the plugins and execute them. Finally, here in part 3, we'll make a custom plugin and run it through our program from the last example. This final set of code is the simplest, check it out.


Part 3:

Create another source file and include the "pluginAPI.h" header from part 1. Next, create a class which derives from the plugin base class and define its methods. The final step is to create a factory and deleter function using the prototypes declared in the included header. These don't require the decorations such as "extern "C"" or the "PLUGIN_API" macro but they are paired with a compiler directive. Once all of these methods have been implemented in your source file, indicate to your compiler that wou're building a shared library, define the BUILD_PLUGIN directive, and tell the linker to use the "stdcall" calling convention if you're on Windows. See the sample source file below for an example:

Putting it all together:

So now, you should have two binary outputs. An executable from part 2 of the tutorial, and a DLL or SO file from the above sample. Place both of the binaries into a single directory and run the executable while using the shared library file as a second parameter. Once it runs, you will see the output that was defined in your derived plugin class.

Major security issue.

Voila, aside from letting programs run any insecure code all willly-nilly, you now have an awesome plugin system that can be used to extend functionality in a C++ ptoject. This could mean tossing in a scripting language interpreter, adding an image loader, or adding a trojan into your friend's program.

Enjoy!

No comments:

Post a Comment