如何最好地添加插件功能到Delphi程序 [英] How best to add Plugin Capability to a Delphi program

查看:185
本文介绍了如何最好地添加插件功能到Delphi程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望增加用户在Delphi中开发的程序中编写插件的功能。该程序是单个可执行文件,不使用任何DLL。



这将允许用户社区将扩展程序写入我的程序来访问内部数据,并添加他们可能会发现有用的功能。



我看过这篇文章:添加建议插件功能?,但其答案似乎不可转让给Delphi程序。



如果可能,我希望添加此功能并保留我的应用程序作为一个单一的可执行文件,不需要任何DLL或其他模块。



你知道任何资源,组件或文章,建议如何在Delphi中最好地做到这一点,还是做你有自己的建议吗?

解决方案

我想问的第一个问题是,你需要插件才能访问你的主机应用程序,还是添加自己的UI元素?或者插件是否限于向您的主机应用程序查询和/或提供数据?



后者更容易,打开了两种可能性。其他人已经提到了DLL,这是第一个走的路。一些注意事项适用 - 一般来说,您应该仅使用Windows API中使用的数据类型与dll进行连接。这样你可以确定插件DLL将会了解你的数据类型,无论他们创建了什么语言/编译器(例如,使用PChars,而不是字符串)。不要像Delphi一样通过Delphi类,如TStream一个DLL,这在某些情况下似乎是有效的,但是一般来说是不安全的,因为即使DLL是在Delphi中编译的,它可能是编译器的不同版本,与TStream是一样的)。 Google在Delphi中使用DLL,你会发现更多的提示。



还没有提到的另一种方法是在应用程序本身启用脚本。有几个非常有能力的第三方脚本引擎,无论是商业还是免费的,其中大多数都允许您使用脚本来交换Delphi对象。这些库中的一些仅支持Pascal作为脚本语言,其他库可以让您使用Basic(或许更适合初学者用户)或其他语言。请参阅 http://www.remobjects.com/free.aspx



我最喜欢的脚本解决方案目前是来自 http://mmm-experts.com/Products.aspx?ProductID=3 它可以通过RTTI与您的课程精美连接,并允许您在Delphi应用程序中执行Python代码,以及在Python脚本中使用Delphi类。鉴于Python的普及,如果您希望吸引开发人员到您的项目,这可能是一个可行的解决方案。但是,每个用户都需要安装Python发行版。



在我看来,从潜在的插件编写者的角度来看,如果使用脚本而不是选择DLL,则入门的障碍较低。 >

现在,回到我的初始问题:如果您需要插件与您的用户界面进行交互,事情会变得更加复杂,例如通过放置控件。一般来说,DLL不能用于这样做。 Borland / CodeGear认可的方式是使用包(BPL)。使用BPL,您可以访问和实例化插件提供的类,就像在主机应用程序中声明的一样。捕获的是,所有BPL必须与您的主要应用程序相同的确切版本和Delphi的版本进行编译。在我看来,这使得包完全不切实际,因为很难期望世界各地的所有潜在的插件作家将使用与之相同的版本的Delphi。一个主要的陷阱。



为了解决这个问题,我尝试了一种不同的方法:继续使用DLL,并开发插件的语法来描述它需要的UI,然后在主机应用程序中自己创建UI。 (XML是一种方便的方式来表达UI,因为您可以免费获得父母/嵌套的概念。)UI描述语法可以包括当控件的内容或状态发生变化时触发DLL的回调。此方法将会将插件限制在应用程序已经使用或已注册的VCL控件集中。这不是一个更重要的工作,而BPL肯定是。



Google也适用于Delphi插件框架。有一些现成的解决方案,但据我所知,他们通常使用BPL,其有用的有用。


I am looking to add the capability for users to write plugins to the program I have developed in Delphi. The program is a single executable with no DLLs used.

This would allow the user community to write extensions to my program to access the internal data and add capabilities that they may find useful.

I've seen the post at: Suggestions for Adding Plugin Capability? but its answers don't seem transferrable to a Delphi program.

I would like, if possible, to add this capability and keep my application as a single executable without any DLLs or additional modules required.

Do you know of any resources, components or articles that would suggest how to best do this in Delphi, or do you have your own recommendation?

解决方案

The first question I would ask is, do you need the plugins to access the UI of your host application, or add any UI elements of their own? Or will the plugins be limited to querying and/or supplying data to your host app?

The latter is much easier and opens up two possibilities. Others have already mentioned DLLs, which is the first way to go. Certain caveats apply - in general you should be interfacing with a dll using only the data types that are used in Windows API. THat way you can be sure that the plugin DLLs will understand your data types, no matter what language/compiler they were created in. (So for example, use PChars, not strings. Do not as a rule pass Delphi classes such as TStream to a DLL. This will appear to work in some cases, but is unsafe in general, because even if the DLL was compiled in Delphi, it may have been a different version of the compiler, with a slightly different idea of what TStream is). Google for using DLLs in Delphi, and you'll find plenty more tips.

Another way that hasn't been mentioned yet is to enable scripting in your application itself. There are several very capable 3rd-party scripting engines, both commercial and free, and most of them allow you to exchange Delphi objects with the script. Some of these libraries support only Pascal as the scripting language, others will let you use Basic (perhaps better for beginner users) or other languages. See for example RemObjects Pascal Script (free) at http://www.remobjects.com/free.aspx .

My favorite scripting solution at the moment is Python for Delphi (P4D, also free) from http://mmm-experts.com/Products.aspx?ProductID=3 It can interface beautifully with your classes via RTTI, and allows you to execute Python code in your Delphi app, as well as use Delphi classes in Python scripts. Given the popularity of Python, this may be a viable solution if you want to attract developers to your project. However, every user will need to have a Python distribution installed.

It seems to me that the barrier to entry, from the point of view of potential plugin writers, is lower if you use scripting than if you choose DLLs.

Now, back to my initial question: things get much more complicated if you need the plugins to interact with your user interface, e.g. by placing controls on it. In general, DLLs cannot be used to do this. The Borland/CodeGear-sanctioned way is to use packages (BPLs). With BPLs, you can access and instantiate classes offered by a plugin as if they were declared in your host application. The catch is, all BPLs must be compiled with the same exact version and build of Delphi that your main application is. In my opinion this makes packages completely impractical, since it's hard to expect that all the potential plugin writers around the world will be using the same version of Delphi you are. A major pitfall.

To get around this, I have experimented with a different approach: keep using DLLs, and develop a syntax for the plugin to describe the UI it needs, then create the UI yourself in the host app. (XML is a convenient way to express the UI, since you get the concept of parenting / nesting for free.) The UI description syntax can include callbacks to the DLL triggered when the contents or state of the control changes. This method will restrict plugins to the set of VCL controls your application already uses or has registered, though. And it's not a one-nighter job, while BPLs certainly are.

Google for "Delphi plugin framework", too. There are some ready-made solutions, but as far as I know they usually use BPLs, with their limited usefulness.

这篇关于如何最好地添加插件功能到Delphi程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆