Search code examples
stringfunctiondelphiexecution

Executing functions stored in a string


Lets say that there is a function in my Delphi app:

MsgBox and there is a string which has MsgBox in it.

I know what most of you are going to say is that its possible, but I think it is possible because I opened the compiled exe(compiled using delphi XE2) using a Resource Editor, and that resource editor was built for Delphi. In that, I could see most of the code I wrote, as I wrote it. So since the variables names, function names etc aren't changed during compile, there should a way to execute the functions from a string, but how? Any help will be appreciated.


EDIT:

What I want to do is to create a simple interpreter/scripting engine. And this is how its supposed to work:

There are two files, scr.txt and arg.txt

  • scr.txt contains:

    msg_show
    0
  • arg.txt contains:

    "Message"

And now let me explain what that 0 is:

  • First, scr.txt's first line is function name
  • second line tells that at which line its arguments are in the arg.txt, i.e 0 tells that "Message" is the argument for msg_show.

I hope my question is now clear.


Solution

  • I think I've already solved my problem! The answer is in this question's first answer.

    EDIT:
    But with that, as for a workaround of the problem mentioned in first comment, I have a very easy solution.
    You don't need to pass all the arguments/parameters to it. Just take my example:
    You have two files, as mentioned in the question. Now you need to execute the files. It is as simple as that:
    read the first line of scr.txt
    check if it's a function. If not, skip the line
    If yes, read the next line which tells the index where it's arguments are in arg.txt
    pass on the index(an integer) to the "Call" function.
    Now to the function which has to be executed, it should know how many arguments it needs. i.e 2
    Lets say that the function is "Sum(a,b : integer)".It needs 2 arguments
    Now let the function read the two arguments from arg.txt.
    And its done!
    I hope it will help you all.
    And I can get some rep :)