开发者

Best way to implement plugin support

开发者 https://www.devze.com 2023-02-06 14:12 出处:网络
i have a software in c++ MFC that have some public interface for plugin using C exports. I´m about to开发者_C百科 open the software for external 3 party development.

i have a software in c++ MFC that have some public interface for plugin using C exports.

I´m about to开发者_C百科 open the software for external 3 party development. But using a C export are for hardcore programmers.

What the best way to implement it ? I heard about LUA, but want to know about other possibilities.


I think this really depends on the type of program and your target audience. WoW sure wouldn't have as many addons as it does had it required C and a compiler to write them. But I sure wouldn't want to write a large addon in Lua.

So, before anything else you need to decide whether you want to use a scripting language or a compiled language. If you target casual users then a scripting language might be a better choice. If you target professional developers (say, a financial package whose addons are developed by in-house programmers) then compiled language might work best.

Oh, and also - why don't you ask a bunch of potential plugin developers?


If you have C export programming examples that work for you I suggest you put your pre-conceived "hardcore" notions aside and as Nike says "just do it."

It may well be easier than you expected.


We use a c-naming convention and the dlopen\LoadLibrary family of functions to load a plugin (.dll or .so), find the 3 needed methods (create_plugin(), destroy_plugin(), and get_type().

A plugin implementer merely has to make sure that the equivalent (in whatever language they use) is visibile in the library

extern "C" PLUGIN_API plugin_interface* create_plugin( arg_pack* );
extern "C" PLUGIN_API void destroy_plugin( plugin_interface* );
extern "C" PLUGIN_API const char* get_type();

Depending on what you need your plugin to do, arg_pack can be replaced with specific arguments.

Check out the documentation for [gmodule][1] to see a cross-platform library to do this type of thing.


Want to attract folks? JavaScript is hot. Embed the v8 engine.

http://code.google.com/apis/v8/embed.html


Implement a binary interface using exported C functions to create and return interfaces.

// your_plugin_header

// Plugins must implement this interface
struct PluginInterface {
  virtual void Release()=0;
  virtual void Method()=0;
  virtual void Method2()=0;
};


// Plugin dll's must export this function that creates a new object that implements
// the PluginInterface;
extern "C" bool CreatePluginObject(PluginInterface**);

Then, implement an example plugin as a Lua / Javascript / Python bridge, and bundle that with your software. Out the box you support both a tightly coupled C++ interface, PLUS a highly scriptable plugin interface, AND, if anyone prefers a different script, they can do it themselves.

0

精彩评论

暂无评论...
验证码 换一张
取 消