Jump to content

Recommended Posts

Posted (edited)

A new quick/small UDF.

#include-once
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>

Global $RUN_WRAPPER_PID
Global Enum _
        $RUNWRAPPER_ERR_SUCCESS, _
        $RUNWRAPPER_ERR_GENERAL, _
        $RUNWRAPPER_ERR_COUNTER

Global Enum _
        $RUNWRAPPER_EXT_DEFAULT, _
        $RUNWRAPPER_EXT_NOT_FINISHED_YET, _
        $RUNWRAPPER_EXT_COUNTER

If Not @Compiled And @ScriptName = 'Run_Wrapper.au3' Then _Example_for_Run_Wrapper()

Func _Example_for_Run_Wrapper()
    _Run_Wrapper('ping 8.8.8.8')
    If @error then Return SetError(@error, @extended, 0)
    While $RUN_WRAPPER_PID
        Sleep(10)

        _Run_Wrapper_GetStdout()
        If @error Then
            _Run_Wrapper_GetStderr()
            If @error Then ExitLoop
        EndIf
    WEnd
    MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, 'Information #' & @ScriptLineNumber, _
            _Run_Wrapper_GetStdout() & @CRLF & _
            _Run_Wrapper_GetStderr() _
            )
EndFunc   ;==>_Example_for_Run_Wrapper

Func _Run_Wrapper($sCommand)
    _Run_Wrapper_GetStdout(Null)
    _Run_Wrapper_GetStderr(Null)
    $RUN_WRAPPER_PID = Run(@ComSpec & " /c " & $sCommand, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    If @error Then Return SetError($RUNWRAPPER_ERR_GENERAL, $RUNWRAPPER_EXT_DEFAULT, 0)
    Return $RUN_WRAPPER_PID
EndFunc   ;==>_Run_Wrapper

Func _Run_Wrapper_GetStdout($v_Reset = Default)
    Local Static $s_StdOut = ""
    If IsKeyword($v_Reset) And $v_Reset = Null Then $s_StdOut = ''
    $s_StdOut &= StdoutRead($RUN_WRAPPER_PID)
    If @error Then Return SetError(@error, $RUNWRAPPER_EXT_DEFAULT, $s_StdOut)

    Return SetExtended($RUNWRAPPER_EXT_NOT_FINISHED_YET, $s_StdOut)
EndFunc   ;==>_Run_Wrapper_GetStdout

Func _Run_Wrapper_GetStderr($v_Reset = Default)
    Local Static $s_StdErr = ''
    If IsKeyword($v_Reset) And $v_Reset = Null Then $s_StdErr = ''
    $s_StdErr &= StderrRead($RUN_WRAPPER_PID)
    If @error Then Return SetError(@error, $RUNWRAPPER_EXT_DEFAULT, $s_StdErr)
    Return SetExtended($RUNWRAPPER_EXT_NOT_FINISHED_YET, $s_StdErr)
EndFunc   ;==>_Run_Wrapper_GetStderr

 

  Reveal hidden contents

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • mLipok changed the title to Run_Wrapper.au3 UDF - support topic
Posted (edited)

no.
This was my intentionall usage.

btw.
You can guess why I do that this way.

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

It's probably because you want to make sure that $bReset is actually boolean (True) as opposed to non-zero or non-empty string.

ConsoleWrite("True =  True: " & (True = True) & @CRLF)
ConsoleWrite("True == True: " & (True == True) & @CRLF)
ConsoleWrite("True =  1:    " & (True = 1) & @CRLF)
ConsoleWrite("True == 1:    " & (True == 1) & @CRLF)

If so, that means that the underlying code must be a little sloppy.  :)

Edited by TheXman
Posted
  On 1/19/2021 at 2:08 PM, TheXman said:

It's probably because you want to make sure that $bReset is actually boolean (True) as opposed to non-zero or non-empty string.

Expand  

Exactly.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Do you propose:

If IsBool($bReset) And $bReset Then $sOutputError = ''

instead my:

If $bReset == True Then $sOutputError = ''

?

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

Why force $bReset to actually be boolean?
AutoIt by default does the data type conversion. This happens all the time (in user scripts and UDFs) so (nearly) everyone knows what happens under the covers ;)

Even your "==" operator uses data type conversion as it "Tests if two strings are equal"

Edited by water

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted (edited)
  On 1/19/2021 at 2:28 PM, water said:

Why force $bReset to actually be boolean?
AutoIt by default does the data conversion. This happens all the time (in user scripts and UDFs) so (nearly) everyone knows what happens under the covers ;)

Expand  

I want to be sure that in this two following functions:
 

Func _Run_Wrapper($sCommand)
    _Run_Wrapper_GetStdout(True)
    _Run_Wrapper_GetStderr(True)
    $RUN_WRAPPER_PID = Run(@ComSpec & " /c " & $sCommand, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    If @error Then Return SetError($RUNWRAPPER_ERR_GENERAL, $RUNWRAPPER_EXT_DEFAULT, 0)
    Return $RUN_WRAPPER_PID
EndFunc   ;==>_Run_Wrapper

Func _Run_Wrapper_GetStdout($bReset = False)
    Local Static $sOutputRead = ""
    If $bReset == True Then $sOutputRead = ''
    $sOutputRead &= StdoutRead($RUN_WRAPPER_PID)
    If @error Then Return SetError(@error, $RUNWRAPPER_EXT_DEFAULT, $sOutputRead)

    Return SetExtended($RUNWRAPPER_EXT_NOT_FINISHED_YET, $sOutputRead)
EndFunc   ;==>_Run_Wrapper_GetStdout

the $sOutputError and $sOutputRead variables are cleared intentionally, and not by users mistake.

But after consideration in fact better will be to use Defalut (for getting) and Null (for clearing).

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)
  On 1/19/2021 at 2:29 PM, Nine said:

I like it way better with IsBool. 

You can Return SetError if not bool or like you did.  Either way is IMO more readable...

Expand  

Intention of $bReset was to have possibilty to reset stored Static variable.
So you have 2 option "RESET" or "GET" (as the Function name states).
Normally user should use only "GET" feature.
So as for now I think there is no need for additionall error reporting.

btw.
"RESET" should be done only internally by _Run_Wrapper()
User should not "RESET" static variable by them self, user should only use "Default GET" feature to get last results.

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

@mLipok
I agree with @Nine, but, as stated in the Help file about == operator:

  Quote

Tests if two strings are equal. Case-sensitive. The left and right values are converted to strings if they are not strings already. This operator should only be used if string comparisons need to be case-sensitive.

Expand  

So, you are not asserting that $bReset is bool, indeed, you are converting both sides to strings, isn't it?

Edited by FrancescoDiMuro

Click here to see my signature:

  Reveal hidden contents

 

Posted (edited)

hm... maybe HelpFile should be reviewed in this .... part.

Check this following code :

Global $RUN_WRAPPER_PID
Global Enum _
        $RUNWRAPPER_ERR_SUCCESS, _
        $RUNWRAPPER_ERR_GENERAL, _
        $RUNWRAPPER_ERR_COUNTER

Global Enum _
        $RUNWRAPPER_EXT_DEFAULT, _
        $RUNWRAPPER_EXT_NOT_FINISHED_YET, _
        $RUNWRAPPER_EXT_COUNTER

_Run_Wrapper_GetStdout('by mistake')
_Run_Wrapper_GetStderr(1234)

ConsoleWrite('! ' & _Run_Wrapper_GetStdout() & @CRLF)
ConsoleWrite('! ' & _Run_Wrapper_GetStderr() & @CRLF)


Func _Run_Wrapper_GetStdout($bReset = False)
    Local Static $sOutputRead = "SIMULATION OF NORMAL STDOUT RESULT"
    If $bReset == True Then $sOutputRead = ''
;~  If $bReset = True Then $sOutputRead = ''
;~  $sOutputRead &= StdoutRead($RUN_WRAPPER_PID)
    If @error Then Return SetError(@error, $RUNWRAPPER_EXT_DEFAULT, $sOutputRead)

    Return SetExtended($RUNWRAPPER_EXT_NOT_FINISHED_YET, $sOutputRead)
EndFunc   ;==>_Run_Wrapper_GetStdout

Func _Run_Wrapper_GetStderr($bReset = False)
    Local Static $sOutputError = 'SIMULATION OF NORMAL STDERR RESULT'
    If $bReset == True Then $sOutputError = ''
;~  If $bReset = True Then $sOutputError = ''
;~  $sOutputError &= StderrRead($RUN_WRAPPER_PID)
    If @error Then Return SetError(@error, $RUNWRAPPER_EXT_DEFAULT, $sOutputError)
    Return SetExtended($RUNWRAPPER_EXT_NOT_FINISHED_YET, $sOutputError)
EndFunc   ;==>_Run_Wrapper_GetStderr

and then change:
 

;~ If $bReset == True Then $sOutputRead = ''
If $bReset = True Then $sOutputRead = ''

 

and check again.

 

Edited by mLipok
typo in code... SIMALATION >> SIMULATION

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

@mLipok
Ok, I got it.
In the first example (with ==) the static variable is not emptied, while in the second one (=) it is.
That's because in the first example the condition between non-zero expression and True has been evalutated as False, while in the second one, since "everything that is not 0 is 1", then the variable has been emptied.
Not so implicit, but I have to agree with you about changing the Help file because it is a little bit misleading.
Thanks for the explanation :)

Click here to see my signature:

  Reveal hidden contents

 

Posted
  On 1/19/2021 at 2:55 PM, FrancescoDiMuro said:

Thanks for the explanation

Expand  

You're welcome.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

But lets take another example, that, I believe, proves that help file is correct (where both sides are converted to string) :

$bReset = True
ConsoleWrite ("bool vs string true " & ($bReset == "true") & @CRLF) ; False because "True" <> "true"
ConsoleWrite ("bool vs string True " & ($bReset == "True") & @CRLF) ; True because "True" == "True"

$bReset = true ; true is converted to True internaly
ConsoleWrite ("bool vs string true " & ($bReset == "true") & @CRLF) ; False because "True" <> "true"
ConsoleWrite ("bool vs string True " & ($bReset == "True") & @CRLF) ; True because "True" == "True"

$bReset = "True"
ConsoleWrite ("string True vs Bool " & ($bReset == true) & @CRLF) ; True because "True" == "True" (as true is converted first to True)
ConsoleWrite ("string True vs Bool " & ($bReset == True) & @CRLF) ; Same as previous but no conversion

$bReset = "true"
ConsoleWrite ("string true vs Bool " & ($bReset == true) & @CRLF) ; False because "true" <> "True" (as true is converted first to True)
ConsoleWrite ("string true vs Bool " & ($bReset == True) & @CRLF) ; False because "true" <> "True" but no conversion

 

Edited by Nine
Posted (edited)
  On 1/19/2021 at 4:22 PM, Nine said:

 

$bReset = True
ConsoleWrite ("bool vs string true " & ($bReset == "true") & @CRLF) ; False because "True" <> "true"
ConsoleWrite ("bool vs string True " & ($bReset == "True") & @CRLF) ; True because "True" == "True"

$bReset = true ; true is converted to True internaly
ConsoleWrite ("bool vs string true " & ($bReset == "true") & @CRLF) ; False because "True" <> "true"
ConsoleWrite ("bool vs string True " & ($bReset == "True") & @CRLF) ; True because "True" == "True"

 

Expand  

I notice that

True as a Boolean
is always converted as string to "True" even when you use
true as a Boolean

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
  On 1/19/2021 at 5:51 PM, mLipok said:

True as a Boolean
is always converted as string to "True" even when you use

Expand  

Yes it is because boolean true is first converted into boolean True internally.  This is my assumption, otherwise it doesn't make sense...

Posted

Boolean True
or
boolean true

are just "positive Boolean"

;)

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

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
×
×
  • Create New...