Jump to content

I can't get the hang of using RUN in a variable


javip
 Share

Recommended Posts

I found this script around the forums and wanted to use it to learn how to take input from an input box and to work with variables for another projects i'm working on.

however i can't get how to keep the Variable from running itself since it contains RUN.

the variable is $TIL

I've modified it so the ping command takes input from the $strCOMPUTER variable but that's still kind of flaky as well.

the problem is that $TIL runs right at startup since it contains the RUN command.

I have tried taking out the RUN and leaving the rest then adding RUN to a function like so:

Func button1()
    Run($TIL)
EndFunc

this results in syntax errors from the initial variable at the top.

or adding the entire variable to a function itself so it happens with the button click, but again i get nothing.

i'm sure it's something simple that i'm not seeing but could anyone help me out?

variables seem to be the hardest part for me.

here is the full code.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Process.au3>
#include <GuiConstants.au3>
#include <GuiConstants.au3>
#include <Constants.au3>

#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 631, 231, 192, 141)

$Input1 = GUICtrlCreateInput("enter your ip or hostname here", 288, 24, 321, 21)
$Radio1 = GUICtrlCreateRadio("Persistant ping", 296, 64, 113, 17)
$Input2 = GUICtrlCreateInput("Your complete command here", 288, 104, 305, 21)
$Button1 = GUICtrlCreateButton("go", 288, 144, 81, 17)
$hEdit = GUICtrlCreateEdit("", 16, 24, 265, 177)

GUISetState()
#endregion ### END Koda GUI section ###
GUICtrlRead($Input1)
$TIL = Run(@ComSpec & " /c ping " & $strCOMPUTER, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

While 1

    $data = StdoutRead($TIL, 0, 1)
    If $data Then
        While 1
            $line = StdoutRead($TIL)
            If @error = -1 Then ExitLoop
            GUICtrlSetData($hEdit, $line, 1)
        WEnd
    EndIf

    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            ProcessClose($TIL)
            Exit
    EndSelect
WEnd

Func button1()
    Run($TIL)
EndFunc   ;==>button1
Link to comment
Share on other sites

I don't know how that part of the code got deleted but its:

$strCOMPUTER = "127.0.0.1"

That's the problem I'm having is how to keep the $TIL variable from running itself.

that whole line works perfectly but when i declare it outside it runs itself on execute.

Link to comment
Share on other sites

Does it need to be

$TIL = Run(@ComSpec & " /c ping " & $strCOMPUTER, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

what about

$TIL = @ComSpec & " /c ping " & $strCOMPUTER &", "& @SystemDir & ", " & @SW_HIDE & ", " & $STDERR_CHILD & " + " & $STDOUT_CHILD

*edit*

scratch that, that's just stupid.

This doesn't look right though

Func button1()

Run($TIL)

EndFunc ;==>button1

because basically what you are saying is

Run(Run(...

Edited by kaotkbliss

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Link to comment
Share on other sites

$strCOMPUTER = "127.0.0.1"
$TIL = 'Run(@ComSpec & " /c ping " & $strCOMPUTER, @SystemDir, @SW_HIDE, 0x1 + 0x2)'

Execute($TIL)

this... anyway another issues with your script:

You are including same libraries.

You are running Ping in hide mode. Is this what you want?

I change $STDIN_CHILD to 0x1 and $STDOUT_CHILD to 0x2 because i didn`t find the library where that two constants are declared.

Good Look

Link to comment
Share on other sites

This doesn't look right though

Func button1()

Run($TIL)

EndFunc ;==>button1

because basically what you are saying is

Run(Run(...

one of the things i had tried in the beginning was removing RUN from the var and leaving the rest of the command. so essentially it was RUN (rest of command here) didn't work though.

Link to comment
Share on other sites

instead of Run($TIL)

why not rewrite the original line there

Run(@ComSpec & " /c ping " & $strCOMPUTER, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Link to comment
Share on other sites

$strCOMPUTER = "127.0.0.1"
$TIL = 'Run(@ComSpec & " /c ping " & $strCOMPUTER, @SystemDir, @SW_HIDE, 0x1 + 0x2)'

Execute($TIL)

this... anyway another issues with your script:

You are including same libraries.

You are running Ping in hide mode. Is this what you want?

I change $STDIN_CHILD to 0x1 and $STDOUT_CHILD to 0x2 because i didn`t find the library where that two constants are declared.

Good Look

ping is running in hide mode because it outputs to an edit box.

here is the original code that i'm trying to learn off of. I added the $strHOST myself to see if it would work.

#include <GuiConstants.au3>
#include <Constants.au3>
GUICreate("GUI")
$hEdit = GUICtrlCreateEdit(" ", 5, 5, 300)
$hInput = GUICtrlCreateInput("Input your Hostname / IP Address here", 5, 220)
GUISetState()
$strHOST = "127.0.0.1"

Global $TIL = Run(@ComSpec &  " /c ping "  & $strHOST, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

While 1

    $data = StdoutRead($TIL, 0, 1)
    If $data Then
        While 1
            $line = StdoutRead($TIL)
            If @error = -1 Then ExitLoop
            GUICtrlSetData($hEdit, $line, 1)
        WEnd
    EndIf

    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            ProcessClose($TIL)
            Exit
    EndSelect
WEnd

I'm trying to accomplish a few things things with this:

1) Familiarize myself with variables

2) Send CMD output to Editboxes (I have quite a few BATCH files that I want to wrap in GUIs and make a bit more flexible with menu options)

3) Use input boxes for variable strings

Link to comment
Share on other sites

instead of Run($TIL)

why not rewrite the original line there

Run(@ComSpec & " /c ping " & $strCOMPUTER, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

Because I get this error:

: WARNING: $TIL: possibly used before declaration

if you look at the WHILE statements, that entire block references to $TIL so it can populate the edit box

with the output from $TIL

Link to comment
Share on other sites

my example using execute works fine

if i remove your execute call and the ' ' from the line it works fine and outputs everything to the editbox, but what i want to do is make it so when i hit the button then everything runs.

i'm assuming i set something up wrong here since this doesn't work. i searched and tried to see if i could find anything in the help file that reference using execute withing a function call for a button but couldn't find anything.

here is the code with your changes...how am i setting up the button incorrectly?

$strCOMPUTER = "127.0.0.1"
$TIL = 'Run(@ComSpec & " /c ping " & $strCOMPUTER, @SystemDir, @SW_HIDE, 0x1 + 0x2)'

Func button1()
    Execute($TIL)
EndFunc   ;==>button1


While 1

    $data = StdoutRead($TIL, 0, 1)
    If $data Then
        While 1
            $line = StdoutRead($TIL)
            If @error = -1 Then ExitLoop
            GUICtrlSetData($hEdit, $line, 1)
        WEnd
    EndIf

    $nMsg = GUIGetMsg()



    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            ProcessClose($TIL)
            Exit
            If $nMsg = $Button1 Then button1()

    EndSelect
WEnd
Link to comment
Share on other sites

Do you want something like this?

#include <GuiConstants.au3>
#include <Constants.au3>
Global $TIL
GUICreate("GUI")
$hEdit = GUICtrlCreateEdit(" ", 5, 5, 300)
$hInput = GUICtrlCreateInput("Input your Hostname / IP Address here", 5, 220)
$button1 = GUICtrlCreateButton("test", 5, 310, 150, 30)
GUISetState()

$strCOMPUTER = "www.autoitscript.com"

While 1
    $data = StdoutRead($TIL)
    If $data Then
        GUICtrlSetData($hEdit, $data, 1)
    EndIf
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            ProcessClose($TIL)
            Exit
        Case $nMsg = $button1
                        GUICtrlSetData($hEdit, "") ;This line erase the edit
            $TIL = Run(@ComSpec & " /c ping " & $strCOMPUTER, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    EndSelect
WEnd
Edited by monoscout999
Link to comment
Share on other sites

Do you want something like this?

that worked perfectly!

i used what i learned from how you setup the $TIL variable to setup the $strCOMPUTER variable.

i now have $strcomputer storing the input from $hInput.

could you explain to me what i was doing wrong in setting up the button? instead of a function you put it directly in the CASE of the button.

is there a specific reason i couldn't use it as a separate function and call it like that?

thanks for all the help!

Edited by pixeldotz
Link to comment
Share on other sites

You need to do this line and save the return of the function in a variable.. this case $TIL

$TIL = Run(@ComSpec & " /c ping " & $strCOMPUTER, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

Look at your first attempt on the first post of this thread... the only wrong command is this... Run($TIL) basicly you are using as parameter for the Run function the return of another run function... The PID.

If you only edit that you will get results.

Also the loop that you were using to fill the edit it seems a little confused to me and i remove becuause is unnesesary to me...

You don`t need to loop it.. only use the already existing main loop and a sentence to know when execute those lines,

$data = StdoutRead($TIL)
    If $data Then
this two lines do that.. those are the filters to avoid the loop enter to do some action where is not... If Stdout gives you error, dont do anything. so if theres no PID Stdout gives you error... If there is no $TIL variable to store Run function returns there is no PID so there is no Stdout so there is no need to write the edit... sorry if i became confusing, english is not my mother languaje.

Using Funtion example

#include <GuiConstants.au3>
#include <Constants.au3>
Global $TIL
GUICreate("GUI")
$hEdit = GUICtrlCreateEdit(" ", 5, 5, 300)
$hInput = GUICtrlCreateInput("Input your Hostname / IP Address here", 5, 220)
$button1 = GUICtrlCreateButton("test", 5, 310, 150, 30)
GUISetState()

$strCOMPUTER = "www.autoitscript.com"

While 1
    $data = StdoutRead($TIL)
    If $data Then
        GUICtrlSetData($hEdit, $data, 1)
    EndIf
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            ProcessClose($TIL)
            Exit
        Case $nMsg = $button1
            _Button1()
    EndSelect
WEnd
Func _Button1()
    GUICtrlSetData($hEdit, "") ;This line erase the edit
    $TIL = Run(@ComSpec & " /c ping " & $strCOMPUTER, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
EndFunc   ;==>_Button1
Link to comment
Share on other sites

You need to do this line and save the return of the function in a variable.. this case $TIL

$TIL = Run(@ComSpec & " /c ping " & $strCOMPUTER, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

Look at your first attempt on the first post of this thread... the only wrong command is this... Run($TIL) basicly you are using as parameter for the Run function the return of another run function... The PID.

If you only edit that you will get results.

Also the loop that you were using to fill the edit it seems a little confused to me and i remove becuause is unnesesary to me...

You don`t need to loop it.. only use the already existing main loop and a sentence to know when execute those lines,

$data = StdoutRead($TIL)
    If $data Then
this two lines do that.. those are the filters to avoid the loop enter to do some action where is not... If Stdout gives you error, dont do anything. so if theres no PID Stdout gives you error... If there is no $TIL variable to store Run function returns there is no PID so there is no Stdout so there is no need to write the edit... sorry if i became confusing, english is not my mother languaje.

Using Funtion example

#include <GuiConstants.au3>
#include <Constants.au3>
Global $TIL
GUICreate("GUI")
$hEdit = GUICtrlCreateEdit(" ", 5, 5, 300)
$hInput = GUICtrlCreateInput("Input your Hostname / IP Address here", 5, 220)
$button1 = GUICtrlCreateButton("test", 5, 310, 150, 30)
GUISetState()

$strCOMPUTER = "www.autoitscript.com"

While 1
    $data = StdoutRead($TIL)
    If $data Then
        GUICtrlSetData($hEdit, $data, 1)
    EndIf
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            ProcessClose($TIL)
            Exit
        Case $nMsg = $button1
            _Button1()
    EndSelect
WEnd
Func _Button1()
    GUICtrlSetData($hEdit, "") ;This line erase the edit
    $TIL = Run(@ComSpec & " /c ping " & $strCOMPUTER, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
EndFunc   ;==>_Button1

oh ok, i get the way you set it up as a function here. you declared the variable globally without it's parameters. then call it from

within the function.

again thanks for all the help. it's helped me a great deal in learning about functions and variables. makes reading the help and forums

easier to understand.

Link to comment
Share on other sites

the variable stores the function return... not the funtion itself, when you read the help of any function see the return.. then if you want to use that return put a $variable = before the function call...

Glad to help you

EDIT: i almost forget.. there sometimes that you need to declare the variable before use it in some situations... for ex.. the first example will run fine whit or whitout previus declaration of the variable $TIL... the second no, because every variable declared inside a function will be setted by default to a local scope, so it will become non-existent outside the function...

and again...sorry my bad english,i quit my teacher role for today.. cheers

Edited by monoscout999
Link to comment
Share on other sites

the variable stores the function return... not the funtion itself, when you read the help of any function see the return.. then if you want to use that return put a $variable = before the function call...

Glad to help you

EDIT: i almost forget.. there sometimes that you need to declare the variable before use it in some situations... for ex.. the first example will run fine whit or whitout previus declaration of the variable $TIL... the second no, because every variable declared inside a function will be setted by default to a local scope, so it will become non-existent outside the function...

and again...sorry my bad english,i quit my teacher role for today.. cheers

again, thanks for your explanations, it's helped me quite a bit.

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