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.
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