Sign in to follow this  
Followers 0
Locoblade

Autoit exe not setting Exit code

6 posts in this topic

Hi All

Im trying to run a simple Autoit script to save files using the FileSaveDialog dialog box. If the user clicks "Cancel", Autoit internally returns an error code (@error) which you can capture using an If statement, but then if you try to set the exit code using the Exit() command, it simply doesn't convey that error code back to the parent process that called it, whether it be a wshshell.run or simply from a DOS command line. Im basing my code on the standard code in the help menu so I know it should be OK, but doesn't work for me....

$var = FileSaveDialog( "Choose a name.", "c:\", "(*.*)", 2)
If @error Then
    MsgBox(4096,"","Save cancelled.")
    Exit(1)
Else
    MsgBox(4096,"","You chose " & $var)
    Exit(0)
EndIf

I run the above exe from XP command line using

start /wait savefile.exe

echo %errorelevel%

If I chose a file I get the "You Chose..." dialog box and %errorlevel% is 0 as expected

However, if I cancel, I get the "Save Cancelled" dialog box so the If routine is working, but %errorlevel% is still 0 despite the Exit() function telling autoit to exit with code 1.

This is on XP SP3, the strange thing is if I compile and run the same code (with the same version of Autoit) on my Win7 64 bit laptop, the code works fine and returns the correct errorlevel to the Windows 7 command line window, but this isnt an option as we need to be able to support this utility on XP machines at work.

Ive had a hunt through these forums and found a couple of similar threads from a few years ago, but they seemed to be based on the user running from a command line not waiting for the exit code before echoing the errorlevel.

Help!!!

Share this post


Link to post
Share on other sites



I think you are seeing the errorlevel returned by START, not by savefile.exe. You could use START to kick off savefile.bat, and inside that batch file run savefile.exe and then echo %errorlevel%. You'll get the right answer.

;)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Hi

Thanks for the response. If I remove the start /wait and simply run savefile.exe from the command line then click cancel to theoretically exit(1), then echo the error level, I still get 0, so I don't think thats the issue.

Its the same story when running the exe from Wshshell.run in VBS, you can normally follow a wshshell.run command with something like "If Err.Number <> 0 then" to catch a non zero error code, but again it returns zero.

cheers

Chris

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Once again, the %ERRORLEVEL% only exists within the same command shell the .exe is running in. Anything that spawns a shell for it to run in will not see the %ERRORLEVEL% when it returns.

I compiled your script as Test1.exe and run it from Test1.bat:

.\test1.exe
echo %ERRORLEVEL%

I get the right answer with that every time.

;)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Which OS are you running?

This works fine in Win7 64 Bit but even using the exact same script as you have above, this does not work on several different builds of Windows XP and Windows PE 2 (Vista 32 bit based deployment OS)

Here is my batch (test.cmd)

.\test.exe
echo %errorlevel%

This is the result:

Run the batch and click cancel - I get the relevant message box...

Posted Image

Click OK and....

Posted Image

Share this post


Link to post
Share on other sites

Ran on Vista Business 32-bit. If I was going to run it on 64-bit, I would compile for it though. Did you add this?

#AutoIt3Wrapper_UseX64=Y

;)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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