简体   繁体   English

从应用程序公开DLL的API

[英]Exposing an API for DLLs from an app

I am developing a plugin system for an application. 我正在为应用程序开发插件系统。 The idea is to load some functions from plugins (loaded as DLLs) and use those functions in our scripting language hosted in app. 这个想法是从插件中加载某些功能(以DLL形式加载),并以应用托管的脚本语言使用这些功能。 I have to expose an API for the DLLs for them to interact with the app. 我必须公开DLL的API,以便它们与应用程序进行交互。 The API may change overtime and the older DLLs should not be invalidated. API可能会随着时间的推移而更改,并且较早的DLL不应无效。 Please give me some leads to read up on or please suggest ideas. 请给我一些线索以进行阅读或提出建议。 Thanks. 谢谢。

Duminda Duminda

A simple Solution for exposing your API to the plugins is to have another "core"-Library exposing the API and to link your plugins against it. 将您的API暴露给插件的一个简单解决方案是让另一个“核心”库公开该API,并将您的插件链接到该库。 This would be quite natural to the plugin developers. 对于插件开发者来说,这将是很自然的。

About API-Changes: The public API must not be changed, but can only be extended, eg added to. 关于API更改:不得更改公共API,而只能对其进行扩展(例如添加)。 There is no way to change an API without breaking clients. 没有中断客户端就无法更改API。

Here is a Link about evolving APIs without breaking Clients. 这是有关不断发展的API而又不破坏客户端的链接。 It is Java but most does also apply to C++. 它是Java,但大多数也适用于C ++。 http://wiki.eclipse.org/index.php/Evolving_Java-based_APIs http://wiki.eclipse.org/index.php/Evolving_Java-based_APIs

Be sure to also change the API in a binary compatible way. 确保还以二进制兼容的方式更改API。 C is easier in this regards than C++, so think about using C as your public API. 在这方面,C比C ++更容易,因此请考虑使用C作为公共API。 In C++ the PIMPL-Idiom helps, but one still cannot add new virtual methods. 在C ++中,PIMPL-Idiom有所帮助,但是仍然无法添加新的虚拟方法。

The 'industry standard' way is to make use of SWIG (Simplified Wrapper and interface Generator) which takes your C/C++ code and creates 'high-level' wrapper classes so your script languages can access your C/C++ code very easily. “行业标准”方式是利用SWIG (简化包装程序和接口生成器),该程序将使用您的C / C ++代码并创建“高级”包装程序类,以便您的脚本语言可以非常轻松地访问您的C / C ++代码。

SWIG is also free, for commercial aps too. SWIG也是免费的,也适用于商业应用。

SWIG is most commonly used to create high-level interpreted or compiled programming environments, user interfaces, and as a tool for testing and prototyping C/C++ software. SWIG最常用于创建高级解释或编译的编程环境,用户界面,以及作为测试和原型化C / C ++软件的工具。 SWIG is typically used to parse C/C++ interfaces and generate the 'glue code' required for the above target languages to call into the C/C++ code. SWIG通常用于解析C / C ++接口并生成上述目标语言调用C / C ++代码所需的“粘合代码”。

Here are some ideas: 以下是一些想法:

  • Make the API fully compatible with C. No classes, no STL types, all functions declared extern "C" . 使API与C完全兼容。没有类,没有STL类型,所有函数都声明为extern "C"
  • For versioning, embed the API version number in the function and struct names. 要进行版本控制,请将API版本号嵌入函数和结构名称中。 Once a version has been released, the function signatures and type definitions can not be changed without the risk of breaking backward compatibility. 发布版本后,将无法更改功能签名和类型定义,而不会破坏向后兼容性。
  • For convenience, you could provide a set of macros that map a function-name-without-version-number to a versioned function name. 为方便起见,您可以提供一组宏,这些宏将一个没有版本号的函数名映射到一个版本化的函数名。 When the macros are updated for a new version, this will not break compatibility with older versions, but it will make the client code easier to read. 当为新版本更新宏时,这不会破坏与旧版本的兼容性,但是会使客户端代码更易于阅读。

COM fits perfectly for what you require. COM非常适合您的需求。 It is language independent and the dlls can be loaded by your application at runtime. 它与语言无关,并且dll可以由您的应用程序在运行时加载。 Implementing ActiveX for your components makesthem sutatible for scripting too. 为您的组件实现ActiveX也使它们可用于脚本编写。
http://msdn.microsoft.com/en-us/library/ee663262(v=VS.85).aspx http://msdn.microsoft.com/en-us/library/ee663262(v=VS.85).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM