Sign in to follow this  
Followers 0
Rick

exitcode of external program

12 posts in this topic

#1 ·  Posted (edited)

this is probably easy and i'm being daft but; i'm running a non autoit program and need to find out the exit code of it, its either 0 for success ("ok") or -1 ("cancel"). how can i find this errorlevel so I can act upon its answer?????

thanks for any help guys

Edited by Rick

Who needs puzzles when we have AutoIt!!

Share this post


Link to post
Share on other sites



this is probably easy and i'm being daft but; i'm running a non autoit program and need to find out the exit code of it, its either 0 for success ("ok") or -1 ("cancel"). how can i find this errorlevel so I can act upon its answer?????

thanks for any help guys

Return Value

Success: Returns the exit code of the program that was run.

Failure: Depends on RunErrorsFatal; see Remarks.


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

hmm i spoke too soon as runwait only returns success of the run program, not the exit code of it

Edited by Rick

Who needs puzzles when we have AutoIt!!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

hmm i spoke too soon as runwait only returns success of the run program, not the exit code of it

RunWait returns the exitcode of the run program. If you want your AutoIt script to also exit with the exitcode returned from the program run, then you could just do as shown below.

$exitcode = RunWait('program.exe')
Exit($exitcode)
Edited by MHz

Share this post


Link to post
Share on other sites

RunWait returns the exitcode of the run program. If you want your AutoIt script to also exit with the exitcode returned from the program run, then you could just do as shown below.

$exitcode = RunWait('program.exe')
Exit($exitcode)
yes thats fine if i want to exit my script, but i dont, its the run program that i know will have an errorlevel of 0 if ok is clicked, and -1 if cancel is clicked it is not an AutoIt program. Run will always seems to exit with 0 as it is successfully run, but i need to retreive the exit code of the "run" program, not of the success of AutoIt running it.

Who needs puzzles when we have AutoIt!!

Share this post


Link to post
Share on other sites

RunWait returns the exitcode of the run program.

Local $ExitCode = RunWait('MyProgram.exe')
MsgBox(0x40, 'Information', 'The program returned the exit code ' & $ExitCode & '.')
If $ExitCode = 0 Then
    ···
ElseIf $ExitCode = -1 Then
    ···
Else
    MsgBox(0x30, 'Warning', 'An unexpected exit code was generated.')
EndIf

Share this post


Link to post
Share on other sites

yes thats fine if i want to exit my script, but i dont, its the run program that i know will have an errorlevel of 0 if ok is clicked, and -1 if cancel is clicked it is not an AutoIt program. Run will always seems to exit with 0 as it is successfully run, but i need to retreive the exit code of the "run" program, not of the success of AutoIt running it.

Programs do not seem to follow the same returns as an AutoIt messagebox. Running a DOS batch script, clicking cancel on a selected nstaller returns an %errorlevel% of 1223 and a success 536870912 when successful. Using the exitcode of the RunWait() function yields the same return of 1223 on cancel and 536870912 on success. AutoIt returns the same exitcode as the DOS %errorlevel%.

From Helpfile for RunWait:

Success: Returns the exit code of the program that was run.

If you are refering to Run(), then it returns only the PID. 0 is returned on failure of no PID returned.

From either function of Run() or RunWait(), neither returns a success of 0 unless the program that you run returns zero for success.

Share this post


Link to post
Share on other sites

ok, thanks for your help guys, it looks like i'm not going to be able to do it, still it was a good idea at the time

thanks anyway


Who needs puzzles when we have AutoIt!!

Share this post


Link to post
Share on other sites

Nothing is impossible. You could invoke your script to run a second time and collect the runwait exitcode from it even though your main script continues to run...

Share this post


Link to post
Share on other sites

I'm not sure communication occurred here. Here's a script to do what I believe Rick wants:

; Run program and wait for it to exit,
   ; assigning the DOS exit code (errorlevel) to $return
    $return = RunWait("errorlevel.exe")
    
   ; Display a custom message based on the return
    Select
        Case  $return = 0
            MsgBox(0, "Info", "Errorlevel returned 0, so OK was clicked.")
        Case  $return = -1
            MsgBox(0, "Info", "Errorlevel returned -1, so Cancel was clicked.")
    EndSelect

Here's a script to be compiled to behave like the EXE Rick already has, exits with a 0 on OK or a -1 on Cancel. Please compile and place in the same folder with the above script to test.

; Show an OK / Cancel MessageBox and capture the choice
    $choice = MsgBox(1, "Choose", "     Click OK or Cancel")
    
   ; Exit with a custom ERRORLEVEL based on the choice
    If $choice = 1 Then
        Exit 0
    Else
        Exit -1
    EndIf

Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

Share this post


Link to post
Share on other sites

Can programs indeed return a negative exit code? I don't believe I've ever seen this -- at least not in my time of ErrorLevel checking in batch files.

Share this post


Link to post
Share on other sites

Can programs indeed return a negative exit code? I don't believe I've ever seen this -- at least not in my time of ErrorLevel checking in batch files.

The above examples actually run and work on WinXP, I don't know how they'd fare in an older OS...

Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

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  
Followers 0