jgranto Posted August 20, 2013 Share Posted August 20, 2013 Hi all, I am trying to use AutoIT to wrap an existing script. I will then be compiling this into an exe using the built-in tool. The wrapped script returns an errorcode - for my example, below, I am using a simple batch script where I can exit with a code of 0 (success) or 1 (failure). I want AutoIT to capture this code, and then exit with the same code. Thus, if my sample batch script exits with 0, AutoIT should exit with 0. For my simple tests, I decided to use the Exit command. So, first I created two .au3 scripts, one with Exit(0), the other wiith Exit(1). I then compliled them into executables. These two scripts work perfectly - I can run them, and once complete I can check %ERRORLEVEL% and they contain the correct value. Now I want to embed my script and use the Exit command. So I created a simple batch file that simply contains this line: @exit /B %1 Here is my AutoIT script. You will see that I pass the errorlevel as a paramenter in the RunWait line. The MsgBox lines are just for debugging purposes. FileInstall ("C:scripttestsSend_Error.bat", "%TEMP%Send_Error.bat", 1) ; Maybe use ShellExecuteWait? RunWait ("%TEMP%Send_Error.bat 0","",@SW_HIDE) ; @error snags the errorlevel result of the RunWait. Local $Embedded_Error = @error ; Delete the script used in RunWait. FileDelete ("%TEMP%Send_Error.bat") ;Exit with %ERRORLEVEL% produced from RunWait script. MsgBox(4096, "Result","Embedded_Error = " & $Embedded_Error) ;Exit($Embedded_Error) If $Embedded_Error = 0 Then MsgBox(4096, "Result","Embedded_Error0 = " & $Embedded_Error) Exit(0) ElseIf $Embedded_Error < 0 Then MsgBox(4096, "Result","Embedded_Error- = " & $Embedded_Error) Exit(1) ElseIf $Embedded_Error > 0 Then MsgBox(4096, "Result","Embedded_Error+ = " & $Embedded_Error) Exit(2) EndIf The problem I am having is that the code above does not work. It compiles fine, and runs fine, but does not do what it should. No matter what errorlevel I define in RunWait, it always gets detected as 1. So, for example, I tried these lines: RunWait ("%TEMP%Send_Error.bat 0","",@SW_HIDE) RunWait ("%TEMP%Send_Error.bat 1","",@SW_HIDE) RunWait ("%TEMP%Send_Error.bat 2","",@SW_HIDE) For each of the lines above, the debugging MsgBox lines say that $Embedded_Error is 1. Even when it should be 0 or 2. Additionally, when I check %ERRORLEVEL% after execution of the exe, it always shows ERRORLEVEL 2, which you note is different from the $Embedded_Error value of 1. When I define $Embedded_Error explicitly as 0, 1, 2, etc., the MsgBox lines return the the explicitly defined value and the script exits with code 2, as it should. All that works fine. Thus, I seem to have one problem: I am not capturing the exit code of my embedded script properly using: Local $Embedded_Error = @error Can someone please help me out. I know this has got to be something really simple (and stupid) that I am missing. Thanks! Link to comment Share on other sites More sharing options...
Solution DW1 Posted August 20, 2013 Solution Share Posted August 20, 2013 FileInstall("C:\script\tests\Send_Error.bat", @TempDir & "\Send_Error.bat", 1); <<< Using @TempDir macro instead of %temp% as this is not batch. ; Maybe use ShellExecuteWait? Local $Embedded_Error = RunWait(@TempDir & "\Send_Error.bat 2", "", @SW_HIDE); <<< RunWait() returns the exit code so set it here. Also, Using @TempDir macro instead of %temp% as this is not batch. ; @error snags the errorlevel result of the RunWait. <<< This is not correct, read the helpfile on RunWait. ;~ Local $Embedded_Error = @error <<< @error doesn't contain the exit code, the return of RunWait() does ; Delete the script used in RunWait. FileDelete("%TEMP%\Send_Error.bat") ;Exit with %ERRORLEVEL% produced from RunWait script. MsgBox(4096, "Result", "Embedded_Error = " & $Embedded_Error) ;Exit($Embedded_Error) If $Embedded_Error = 0 Then MsgBox(4096, "Result", "Embedded_Error0 = " & $Embedded_Error) Exit (0) ElseIf $Embedded_Error < 0 Then MsgBox(4096, "Result", "Embedded_Error- = " & $Embedded_Error) Exit (1) ElseIf $Embedded_Error > 0 Then MsgBox(4096, "Result", "Embedded_Error+ = " & $Embedded_Error) Exit (2) EndIf See comments in code. Let me know if you have questions. AutoIt3 Online Help Link to comment Share on other sites More sharing options...
jgranto Posted August 20, 2013 Author Share Posted August 20, 2013 Thanks, you rock. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now