language-agnosticpluginscompiled-language

What is recommended for plug in systems in applications?


What are the “normal” ways to do plug-ins in compiled languages (C#/C/C++/D)? I am specifically interested in language agnostic approaches but language specific is not unacceptable.

For the time being, “compile time” plug in approaches (just include the code or not and everything works) are valid but things that can migrate to a more dynamic approach are preferred.

Regarding the runtime type, I'm more interested in the mechanics of loading the plug-in and whatnot than designing the plug-in/app interface

EDIT: BTW the plugin would be a slave not a master. The basic action of a plug-in would be to that under a given situation, it would be called on to "do its thing" and be given an environment object that it should use to get what it needs to operate.


Solution

  • For compiled languages (where compiled means the program runs as a native executable, without any sort of virtual machine), you pretty much need to use some sort of platform-specific shared library approach. In Windows, this means using DLLs.

    You define your plugin interface in terms of a set of functions (with specific names, arguments, and calling conventions). Then, you load the addresses of the functions within the shared library and go to town. In Windows, this means using GetProcAddress(), and then casting the return value to a function pointer of the appropriate type in C, or whatever the equivalent is in the language you're using.

    Another option that may or may not be more desirable would be to run a virtual machine for another language from within your native application, and have plugins be code for that language. For example, you could run a Python VM using CPython and dynamically load Python modules.