Jump to content
Sign in to follow this  
JRowe

Limitations of Execute?

Recommended Posts

JRowe

I'm trying to understand the limitations of Execute().

I can store functions to variables easily enough, and execute them:

$TestFunction = MsgBox(0,"", "Easy Enough")
Execute($TestFunction)

I can also format strings as executable functions:

$TestFunction = 'MsgBox(0,' & '" ' & '"' & ', ' &'"'&'message'& '' &'"'& ')'
Execute($TestFunction)

I would like to be able to store arbitrary code to a string and then execute it. Includes obviously have to be done at the beginning of the program, in any case, but other than that, I think I'm just having problems with formatting.

How would I format the following as a string, usable by the Execute() function?

GUICreate("Test GUI",200,100)
GUISetState() 
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
Wend

I've been trying to use @CR and @LF without success, although that could be the answer.

Thanks!

Share this post


Link to post
Share on other sites
JRowe

A bit of clarification...

It's not for anything malicious: I'm working on a script designer, and I want to be able to execute user defined code after it's been parsed and sanitized. I'm trying to figure out how to do it generically before I get into the formatting.

I want to not have to launch autoit scripts directly, but have the sanitized functions read from a database and executed by a separate process. The ultimate goal would be scripts easily traded between user with no fear of malicious misuse, which is unfortunately the case with raw AutoIt.

Share this post


Link to post
Share on other sites
martin

I'm trying to understand the limitations of Execute().

I can store functions to variables easily enough, and execute them:

$TestFunction = MsgBox(0,"", "Easy Enough")
Execute($TestFunction)

I can also format strings as executable functions:

$TestFunction = 'MsgBox(0,' & '" ' & '"' & ', ' &'"'&'message'& '' &'"'& ')'
Execute($TestFunction)

I would like to be able to store arbitrary code to a string and then execute it. Includes obviously have to be done at the beginning of the program, in any case, but other than that, I think I'm just having problems with formatting.

How would I format the following as a string, usable by the Execute() function?

GUICreate("Test GUI",200,100)
GUISetState() 
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
Wend

I've been trying to use @CR and @LF without success, although that could be the answer.

Thanks!

I think you are not quite right with what Execute is doing.

When you do this

$TestFunction = MsgBox(0,"", "Easy Enough")
Execute($TestFunction)

you are just evaluating the return from MsgBox because I suppose the variable $TestFunction is converted to a string when it is passed to Execute which then returns the value.

If you do this

$TestFunction = 'MsgBox(0,"", "Easy Enough")'
Execute($TestFunction)

then the MsgBox appears when Execute is called.

To execute a several lines you would need to save them as an AutoIt script, say temp.au3, then run it using

Run('"' & @ProgramFilesDir & '\AutoIt3.exe "' & ' "' & @ScriptDir & '\temp.au3"')


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
JRowe

I think that there is a way to achieve what I'm trying to do without resorting to using Run().

I might have to design a whole set of generic functions and use parameters and calls, but I'd prefer to just be able to execute arbitrary sanitized autoit code within an existing process.

Share this post


Link to post
Share on other sites
crashdemons

try this:

Func _ExecuteScript($ScriptString)
    Local $ScriptArray, $LastLine, $i
    $ScriptArray=StringSplit(StringStripCR($ScriptString),@LF)
    $LastLine=UBound($ScriptArray)-1; because I don't trust $array[0]
    For $i=1 To $LastLine
        Execute($ScriptArray[$i])
    Next
EndFunc

Example:

$Script="MsgBox(0,'','huh?')"&@CRLF&"MsgBox(0,'','ohhh...')"&@CRLF&"MsgBox(0,'','Right, Must be a multi-line script!!!')"
_ExecuteScript($Script)

Func _ExecuteScript($ScriptString)
    Local $ScriptArray, $LastLine, $i
    $ScriptArray=StringSplit(StringStripCR($ScriptString),@LF)
    $LastLine=UBound($ScriptArray)-1; because I don't trust $array[0]
    For $i=1 To $LastLine
        Execute($ScriptArray[$i])
    Next
EndFunc

Note: if using strings like "$a=1" wont set $a to 1, you may have to use "Assign('a', 1)"

Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Share this post


Link to post
Share on other sites
JRowe

Awesome.

Is it possible to do this?

Nothing seems to break, but neither does it work as intended.

;Define the script to be executed
$Script="$a = 0"&@CRLF&"While $a <= 10"&@CRLF&"MsgBox(0, 'Value of $a is:', $a)"&@CRLF&"$a = $a + 1"&@CRLF&"WEnd"


;Check to see what's in the $ScriptVariable
MsgBox(0,"What's in $Script?", $Script)

;Execute the Script
_ExecuteScript($Script)


Func _ExecuteScript($ScriptString)
    Local $ScriptArray, $LastLine, $i
    $ScriptArray=StringSplit(StringStripCR($ScriptString),@LF)
    $LastLine=UBound($ScriptArray)-1; because I don't trust $array[0]
    For $i=1 To $LastLine
        Execute($ScriptArray[$i])
    Next
EndFunc

Share this post


Link to post
Share on other sites
JRowe

Lmao. I suppose that could have been worded better.

There are no overt errors being reported, but its not behaving the way I would expect it to.

:)

Share this post


Link to post
Share on other sites
Confuzzled

There are no overt errors being reported, but its not behaving the way I would expect it to.

Sounds like a Microsoft RTM release. Second nomination...

Share this post


Link to post
Share on other sites
Richard Robertson

The problem is that Execute is made for individual function calls. It cannot be used to perform loops, flow statements, or function definitions.

Share this post


Link to post
Share on other sites
crashdemons

I didnt intend my function for loops - but simply multilined function calls.

if you need a loop to be initialized from inside an execute, consider using the AdlibEnable function on a predefined function :)

Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Share this post


Link to post
Share on other sites
Richard Robertson

I was speaking to the OP.

Share this post


Link to post
Share on other sites
JRowe

Excellent. Thanks for the information, guys :)

Wish I had known that before... Looks like I have a boatload of custom functions to make, so that they can be used with parameters, and in that way have user defined scripts.

Share this post


Link to post
Share on other sites
martin

Excellent. Thanks for the information, guys :)

Wish I had known that before... Looks like I have a boatload of custom functions to make, so that they can be used with parameters, and in that way have user defined scripts.

I don't understand the approach or the logic here. You are saying that you don't want to use "Run" to run a script, so instead you are going to be running a script which will do it for you. If one script runs ok then what is going to be the problem running another one? Or to put it another way, if one script runs ok then it will be able to run another one.

The example you gave in the first post is

GUICreate("Test GUI",200,100)
GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
Wend

To write functions to deal with this type of situation plus the other logic need to make it useful, is impractical IMO without actually producing or creating a script and running it. You can't do it with crashdemons' function.

I have written scripts which create and run other scripts and I don't see the problem.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
Richard Robertson

Martin, I believe the user was looking more for an alternative to runtime specified #includes in a way.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.