How to add code inside the program at runtime (Delphi / Windows)?

I am working on Windows XP / Delphi 7. I need to add some procedures (or functions) inside an executable program, and I do not want to recompile it again after I have finished it.

I have a host application with 5 functions for sending various types of alarms, but there are other new types of alarms, so I have to perform new functions for sending these alarms, but I do not have to rebuild the host application. I have a class called TAlarmManager that called these functions.

Maybe a plugin ??? Ok, but how can I “insert” new features ??? A tutorial, guide, book, etc., to find out about this or any tips on how to do this.

I studied plugins (I'm completely new on this topic), but no one is “talking” about adding features to the host application. It seems to me that plugins add functionality on their own, I mean, they were developed using native code to do something, and not to "add" code to the host application ... How can I do this?

+3
source share
9 answers

For the technical side: how does the Delphi IDE do it? That would be the first place for me.

To understand plugins, you must understand that you cannot add new features. You could, but since the old code does not know what to call it, they will not be called.

So what you do, you add the function "register" or "run" in your plugin. This launch function receives a data structure as a parameter that it can examine or extend. In your case, this will be a list of alarms. Alarms always work the same way (my guess), so it can add additional alarms.

Then, after registering all the plugins, the main code simply goes through the list of alarms and calls the standard alarm function for each of them. He no longer cares where each alarm comes from and what he really does.

The key is here: you need to define the interface that both parties are subscribed to. The main code calls the interface functions, and your plugin code implements them.

+4
source

Another option available is to use a script component for your project. Works well: PascalScript . This will allow you to load external scripts after the fact, and then run them as needed to interact with your application. As Aaron suggested, you will also need to provide some kind of interface for your script to interact with your application.

+3
source

See also Delphi plugin system - bpl vs dll? here at stackoverflow.

+1
source

I don’t quite understand what you mean by “alarms,” so I make a few assumptions.

1) If you do not need additional code for alarms, I would try to force them to manage data. Store different types of alarms in a database or configuration file, which makes it easy to update applications in the field without recompiling or reinstalling.

2) If you need a special code for each alarm, you can use the runtime packages as plugins for your application. Find Delphi runtime packages to get some ideas and guides. Here are some links I found:

http://delphi.wikia.com/wiki/Creating_Packages

http://delphi.about.com/od/objectpascalide/a/bpl_vs_dll.htm

3) Scenarios, as already mentioned in skamradt. If this makes sense for your application, it may also allow your customers to write their own additional features without requiring recompilation on your part.

0
source

You almost certainly want to use Pascalscript, as skamradt suggests. You should start here and seriously consider this option. There are many possibilities that go beyond being able to serialize live code as text. The only drawback is maybe the speed of execution, but it may not matter for your application domain. I would support skamradt, but I do not have enough reputation to advance :)

0
source

Some time ago, I looked at a situation like what you are describing.

My answer was .DLLs. I put the variable code in .DLL, which was dynamically loaded, the name specified in the configuration file. I went through a record containing everything I knew about the situation.

In my case, there was only a successful / unsuccessful return and no exit to the screen, this worked quite well. (He commanded the machine.)

0
source

This is like a place where the scripting language or Domain-Specific Language may make sense. There are several approaches to this:

  • Implement alarm functions in VBscript files (.vbs written in notepad) that access your Delphi code through the COM API. Using the COM API gives you access to a wide range of programming tools for writing functions, including Delphi. This is the most inconvenient decision, but the easiest to make. It can also be useful for your sales process, and it's always good to think about how to sell things.

  • Embed your own function language in Delphi. This way you can make it so easy that your end users can write their own alarm functions. If you do this as an expression evaluator, you can write an alarm like 2 * T1> T2. There are several experts on evaluating expressions, and you can also write your own if they do not fit your needs.

  • Use a predefined programming language inside a Delphi application, for example, "Pascal Script", see http://www.remobjects.com/ps.aspx

0
source

You should take a look at PaxCompiler , for example, PascalScript allows you to load scripts, but you can even copy them beforehand for a more presentation. See the demos section for a solution to your problem.

As a note, the webpage does look bad, but the package is really powerful and stable.

0
source

I think scripting is good for this situation.
There are many script packages you can evaluate:

Sincerely.

0
source

All Articles