Jump to content

Run() and get returned errorlevel


Recommended Posts

Its not irrelevant, the op has said on numerous occaisions he does want to wait for the proccess to end before continuing the script, but still wants the exit code.

Non of the code above can achieve that, it all waits for the process to end before continuing the script.

I believe

_ProcessGetExitCode($hPID) does just that, so while its being checked in a loop, the rest of the script can proceed

until such time as there is an exit code to use.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Developers

I don't think you're connecting the dots or trying to at least, I don't mean to be rude but check out some of the properties for the Run() function and try to come up with a method. I already showed you plenty.

Honnestly think you aren't connecting the dots here.

The Op was clear in his question about the need for the errorlevel but wanting to use Run in stead of RunWait.

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Take advantage of one of the forms of inter-script communication and launch a child script (via Run or ShellExecute) each time you need to launch an application. Send the child script the handle of the master script, the app name and parms for the program to run and have the child script execute a runwait(), when the target application finishes the child can return the app name and exit code to your master script (which has never paused execution) and terminate.

I adapted one of the commonly-used communication methods (WM_COPYDATA) in my (highly popular lol) Battle Checkers in the Examples Forum. The main program trades data with the AI modules in a manner that ought to work for you.

Edited by Spiff59
Link to comment
Share on other sites

Honnestly think you aren't connecting the dots here.

The Op was clear in his question about the need for the errorlevel but wanting to use Run in stead of RunWait.

Jos

And you didn't check my posts prior to that one? I can relate to the problems that this guy has had because it's happened to me before as well, but I spent my time thinking about it and eventually found a work around for it. The scripts I was writing (for him) wasn't specifically what he wanted but I created more options for him, and other users have chimed in on the post to help him for what he specifically needed. Edited by vfear
Link to comment
Share on other sites

  • 4 years later...

I was looking for a solution to this issue today in the forums and could not find anything that seemed to do the job required. IE. to return the exit errorlevel of an external command that was forked using the 'Run' command once it completes. I pulled a couple of functions from some other code I had that I probably aquired from someone else, but posting here because they do work

;Run your program
$PID = Run("MYPROGRAM.EXE")

;Retrieve the real windows process ID
$SYSTEMPID = _ProcessGetHandle($VAR)

;Do your code in the meantime until MYPROGRAM terminates
While ProcessExists($PID)
    Sleep(500)
WEnd

;When it's complete, retrieve the errorlevel
$ERRORLEVEL = _ProcessGetExitCode($SYSTEMPID)

;Display it
MsgBox($MB_SYSTEMMODAL, "Exit ERRORLEVEL is ", $ERRORLEVEL)

Exit

; Return handle of given PID
Func _ProcessGetHandle($iPID)
    Local Const $PROCESS_QUERY_INFORMATION = 0x0400
    Local $avRET = DllCall("kernel32.dll", "ptr", "OpenProcess", "int", $PROCESS_QUERY_INFORMATION, "int", 0, "int", $iPID)
    If @error Then
        Return SetError(1, 0, 0)
    Else
        Return $avRET[0]
    EndIf
EndFunc   ;==>_ProcessGetHandle

; Get process exit code from handle
Func _ProcessGetExitCode($hProc)
    Local $t_ExitCode = DllStructCreate("int")
    Local $avRET = DllCall("kernel32.dll", "int", "GetExitCodeProcess", "ptr", $hProc, "ptr", DllStructGetPtr($t_ExitCode))
    If @error Then
        Return SetError(1, 0, 0)
    Else
        Return DllStructGetData($t_ExitCode, 1)
    EndIf
EndFunc   ;==>_ProcessGetExitCode

 

Edited by robinj
spelling
Link to comment
Share on other sites

  • Developers

Highly doubt they are the same and think it has everything to do with the the original question. :) 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@robinj Those functions are now included in AutoIt.  Also, in your script do not close the process handle at the end of your script.  Look at the examples in the help file for _WinAPI_OpenProcess or _WinAPI_GetExitCodeProcess to do what you are requesting.  

_WinAPI_OpenProcess
_WinAPI_GetExitCodeProcess

 

Adam

 

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