-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.

8
  • Isn't this a duplicate of stackoverflow.com/questions/4186458/… Commented Mar 1, 2015 at 6:15
  • Yes it is posible to do so, but why on earth would you do that? Commented Mar 1, 2015 at 6:53
  • @JensBorrisholt, think script,soap services,names stored in databases etc. Commented Mar 1, 2015 at 7:06
  • @LURD you got a point. But I think I'll wait for nafees Commented Mar 1, 2015 at 7:24
  • 2
    Save you time and use an existing. Delphi Webscript fx code.google.com/p/dwscript Commented Mar 1, 2015 at 7:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

9 Comments

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.
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.
Answer to comment1: I've already found a workaround for that number of parameters.
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!
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.