-1

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.

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
Nafees
  • 31
  • 1
  • 4

2 Answers2

2

I want to make a simple scripting engine.

In order to execute arbitrary code stored as text, you need a compiler or an interpreter. Either you need to write one yourself, or embed one that already exists. Realistically, the latter option is your best option. There are a number available but in my view it's hard to look past dwscript.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
-3

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 :)

Community
  • 1
  • 1
Nafees
  • 31
  • 1
  • 4
  • What happens if the string contains the name of a function that takes a different number of parameters? Or with different types? And by anyone's definition, a scripting engine will support more than function calls. It would support branching and looping, and the creation of functions. It would have some form of I/O. – David Heffernan Mar 01 '15 at 08:25
  • Also bear in mind that allowing your users to call arbitrary functions in your program allows them scope to break your program. You need to sandbox the user's code. Carefully limit its scope. – David Heffernan Mar 01 '15 at 08:56
  • Answer to comment1: I've already found a workaround for that number of parameters. – Nafees Mar 01 '15 at 09:32
  • 1
    The answer contains no information. Link only answers are not useful to future visitors. Anyway, it sounds like you've already decided to go your own way here. Good luck! – David Heffernan Mar 01 '15 at 09:45
  • This isn't helpful to anyone, not even you. In my opinion. – David Heffernan Mar 01 '15 at 12:09
  • It is helpful to me at least, well then, what did _you_ tell? Create an interpreter? Well then, I told _how_ to create it! – Nafees Mar 01 '15 at 12:27
  • Not really. What you have has such limited functionality as to be next to useless. Try dwscript for comparison. – David Heffernan Mar 01 '15 at 12:37
  • 1
    Who in the world do you think is going to tolerate coding in this janky "scripting language" you've devised? It's terrible, and will serve only as a selling point for your competitors. – Rob Kennedy Mar 01 '15 at 14:40
  • and I forgot to mention, its actually not a scripting language, what I meant to say was a script that the interpreter would understand, not humans. And I have a compiler that can convert an easier language into this. – Nafees Mar 26 '15 at 09:30