How best to add Plugin Capability to a Delphi program

前端 未结 9 1495
小鲜肉
小鲜肉 2020-12-23 08:55

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

相关标签:
9条回答
  • 2020-12-23 09:16

    Actually, the accepted answer to the question you cite is quite appropriate for Delphi as well. Your plug-ins will be DLLs, and you can dictate that they should export a function with a certain name and signature. Then, your program will load the DLL (with LoadLibrary) and get the address of the function (with GetProcAddress). If the DLL doesn't load, or the function isn't there, then the DLL is not a plug-in for your application.

    After you have the address for the DLL, you can call it. You can pass the function an interface to something that represents the parts of your application that you wish to expose. You can also require that the function return an interface with methods that your application will call at various times.

    When testing your plug-in system, it will be wise to write a plug-in yourself with a language other than Delphi. That way, you can be more confident that you haven't inadvertently required everyone to use Delphi.

    0 讨论(0)
  • 2020-12-23 09:19

    At first I went for BPL and DLL base plugins. And found them hard to mantain.

    If you use BPL system, then you need to match BPL version with EXE version. This includes Delphi updates which can break something. I found out (the hard way) that if I need to include all my plugins with every release, there is no point in having plugins at all.

    Then I switched to plain DLL plugins. But that system just complicated code base. And that's not a good thing.

    While crusing the net I found out Lua embedded script language, and delivered with it. Lua is 150K DLL, embedding bytecode compiler, interpreter and very simple and smart dynamic programing language.

    My plugins are simple lua scripts. Easily mantaind and made. There are premade Delphi examples, so you can export any class or procedure as Lua table or function. GUI or not. For example I had TurboPower Abbrevia in my app for zipping. I exported my zipping class to lua, and all plugins now can call zip('.', 'dir.zip') and unzip(). Then I switched to 7zip and only implemented old class to use 7zip. All plugins work as they did, with support for new zip('.', 'dir.7z').

    I made TLuaAction which calls Execute(), Update(), Hint() procedure from its script.

    Lua allso have it's own plugin system that makes it easy to add funcionality to it. Eg luacom make is easy to use COM automation, luainterface allows calling .net from lua. See luaforge for more. There is Lua IDE made in Delphi, with source.

    0 讨论(0)
  • 2020-12-23 09:22

    I tried to make an overview of all such options some time ago. Together with my readers/commenters we built this list:

    • DLL/BPL loaded from the program.
    • DLL/BPL loaded from a sandbox (which could be another copy of the program or a specialized "server" application and which communicates with the main program via messages/sockets/msmq/named pipes/mailslots/memory mapped files).
    • COM (in any of its variants).
    • DDE (please not).
    • External program that communicates via stanard input/output.
    • External program that communicates via files (file name is a parameter to the program).
    • External program that works via drop folder (useful for batch processing).
    • External program that communicates with windows messages/windows sockets/msmq/named pipes/mailslots/memory mapped files/database publish-subscribe.
    0 讨论(0)
提交回复
热议问题