Jump to content

is there any way to "call by name" in script


 Share

Recommended Posts

I am new to the autoit.

i want to do "call by name" from the script like this:

$sFunction="myFun"

execute $myFun $para1 $para2.

it's a like a callback function.

I know from the Excel VBA, i can use "Run" to do it. but how can i achieve this from autoit?

Thanks

Link to comment
Share on other sites

As strange as it sounds, i supose that Call() is what you need :)

P.S

Wellcome to the forum and Happy New Year! Posted Image

Edited by MrCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Be aware that Call() has some limitations that will prove frustrating if you're trying to approximate a kind of reflection.

From the help file:

The function cannot be a built-in AutoIt function or plug-in function.

The function can pass arguments to functions, however, ByRef parameters are not supported; there is no way to retrieve the ByRef parameter.

A more robust approach is to construct the function call in a string ( with applicable params ) and pass it to Execute().

; =========================================================================
; Call() restriction Test #1
;       The function cannot be a built-in AutoIt function or plug-in function.

; This message box should appear
MsgBox( 0, "Test Control", "It Worked!" )

; This message box will not appear
Call ( "MsgBox", 0, "Test Call()", "It Worked!" )

; This message box should appear
$executeString = 'MsgBox( 0, "Test Execute()", "It Worked!" )'
Execute( $executeString )

; =========================================================================
; Call() restriction Test #2
;       The function can pass arguments to functions, however, 
;       ByRef parameters are not supported

Dim $num = 1

Func TestByRef( ByRef $x )
    $x = $x * 2
EndFunc

; Control
TestByRef( $num )
ConsoleWrite(" Control => " &$num & @CRLF)

; You may expect '4', but you'll only get '2'
Call ( "TestByRef", $num )
ConsoleWrite( " Call() => " & $num & @CRLF)

; This will correctly return '4'
$executeString = "TestByRef( $num )"
Execute( $executeString  )
ConsoleWrite(" Execute() => " &$num & @CRLF)
Edited by zfisherdrums
Link to comment
Share on other sites

Yep, Execute() is a powerfull funtcion:

_CallEx('MsgBox(64, "Test _CallEx()", "It Works!")' & @CRLF & 'ConsoleWrite("Now we exit..." & @CRLF)')

Func _CallEx($sCallString)
    Local $aCallString = StringSplit($sCallString, @CRLF)
    
    For $i = 1 To $aCallString[0]
        Execute($aCallString[$i])
    Next
EndFunc

:)

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Call any Function..

#include <WindowsConstants.au3>
Local $iList, $fList = @ScriptFullPath

GUICreate("Call Me", 220, 22, 10, 10, -1, BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
$List = GUICtrlCreateCombo("", 0, 0, 190, 25)
$btn = GUICtrlCreateButton("Call", 189, 0, 32, 22)
GUISetState()

$aArray = StringSplit(FileRead($fList), @LF)
For $x = 1 To UBound($aArray) - 1
    If StringLeft($aArray[$x], 4) = "Func" Then
        $aArray[$x] = StringLeft($aArray[$x], StringInStr($aArray[$x], "(") - 1)
        $iList &= StringTrimLeft($aArray[$x], 5) & "|"
    EndIf
Next
GUICtrlSetData($List, $iList)

While 1
    $msg = GUIGetMsg()
    If $msg = -3 Then Exit
    If $msg = $btn Then Call(GUICtrlRead($List))
WEnd

;************** Just Add Your Functions Below **************

Func MessageBox()
    MsgBox(0x0, "Great", "This is a message box")
EndFunc   ;==>MessageBox

Func MessageBox2($info = "Great")
    MsgBox(0x0, $info, "This is a message box also")
EndFunc   ;==>MessageBox2

Func MessageBox3()
    MessageBox2("From #3")
EndFunc   ;==>MessageBox3

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

Call any Function..

#include <WindowsConstants.au3>
Local $iList, $fList = @ScriptFullPath

GUICreate("Call Me", 220, 22, 10, 10, -1, BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
$List = GUICtrlCreateCombo("", 0, 0, 190, 25)
$btn = GUICtrlCreateButton("Call", 189, 0, 32, 22)
GUISetState()

$aArray = StringSplit(FileRead($fList), @LF)
For $x = 1 To UBound($aArray) - 1
    If StringLeft($aArray[$x], 4) = "Func" Then
        $aArray[$x] = StringLeft($aArray[$x], StringInStr($aArray[$x], "(") - 1)
        $iList &= StringTrimLeft($aArray[$x], 5) & "|"
    EndIf
Next
GUICtrlSetData($List, $iList)

While 1
    $msg = GUIGetMsg()
    If $msg = -3 Then Exit
    If $msg = $btn Then Call(GUICtrlRead($List))
WEnd

;************** Just Add Your Functions Below **************

Func MessageBox()
    MsgBox(0x0, "Great", "This is a message box")
EndFunc   ;==>MessageBox

Func MessageBox2($info = "Great")
    MsgBox(0x0, $info, "This is a message box also")
EndFunc   ;==>MessageBox2

Func MessageBox3()
    MessageBox2("From #3")
EndFunc   ;==>MessageBox3

8)

If I have the same Function Name in different au3 file. i want to call one in one specific file. how can i do it?

For example, i have:

File1.au3 myFunc()

File2.au3 myFunc()

Now i want to call the second myFunc(). but i won't include the File2.au3 ONLY coz sometimes i will call the first myFunc().

Link to comment
Share on other sites

If I have the same Function Name in different au3 file. i want to call one in one specific file. how can i do it?

For example, i have:

File1.au3 myFunc()

File2.au3 myFunc()

Now i want to call the second myFunc(). but i won't include the File2.au3 ONLY coz sometimes i will call the first myFunc().

You can't have two functions with the same name. Why do you need this?

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

You can't have two functions with the same name. Why do you need this?

Because I have some testsuite(1...n). i want to specify the testsuite name from the command line like

c:>RunTestSuite.au3 testsuite1

or

c:>RunTestSuite.au3 testsuite1 testsuite3

all my testsuite have one main entry: SuiteMain(). they use the unify function name.

so i need dynamic specify the function name

Link to comment
Share on other sites

Ahhh...I think I see what you're up to. You're wanting a test suite runner.

That's going to be a problem if SuiteMain() appears in multiple files and you are trying to run all of them in the same context.

One workaround would be to have a wrapper spawn unique processes for each test suite. Provided the filenames are consistent, this could be done at the command line like so:

for %f in (testsuite*.au3) do ( RunTestSuite.au3 "%f" )

Using this pattern, you could also generate log files for each individual test suite by redirecting StdOuts ( ConsoleWrite( ... )) to a file like so:

for %f in (testsuite*.au3) do ( RunTestSuite.au3 "%f" > "%~nf.log" )

Be advised that if you place any of the above command-lines in a BAT file, you'll need to replace all instances of '%' with '%%'.

Alternately, you could wrap this in a separate AutoIt script. That script would still have to spawn separate processes to avoid function-name collisions. For my money, the command-line method is easiest.

Edited by zfisherdrums
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...