Jump to content

RunWait behaviour on XP and W7


PTim
 Share

Recommended Posts

Hi,

I am a few weeks old to AutoIT, so please excuse me if I have missed something :).

I have observed that RunWait on XP always returns 0 - regardless of what the program it is running returns. On Windows 7 the behavior is as documented:

i.e. Success: the exit code of the program that was run.

I have 2 scripts that show this problem (sample scripts just to show the problem).

The first is a batch file - mytest.bat:

@Echo off

set myerr=1
if not exist file.exe goto myexit

echo %myerr%
exit /b %myerr%

:myexit



set myerr=99

echo %myerr%
exit /b %myerr%

So all this does is check to see if file.exe exists - if so will return 1; if not will return 99.

It also echo's the error code for debug purposes.

The second is an AutoIt script that uses RunWait to call mytest.bat:

#pragma compile(Console, True)
#include <Constants.au3>
#include <MsgBoxConstants.au3> 
Local $Result
Local $command

$Command = "mytest.bat"
$Result = RunWait($Command)

MsgBox($MB_SYSTEMMODAL, "Test", "Result: " & $Result)
Exit $Result

All this does or should do is get the $Result from RunWait - so whatever is returned from mytest.bat should be the value in $Result.  The message box will show the return code from the batch file.

This works fine on Windows 7 - i.e. it returns 99 or 1 depending on the presence of file.exe.

On XP it always returns 0.  

The debug echo's in mytest.batch correctly show either 99 or 1.

Has anyone else experienced this, and know a way round this, or am I doing something wrong?

Thanks in advance.

 

Link to comment
Share on other sites

Isn't this the same issue you already have a thread running about? Why did you open a second one to deal with the same problem?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Its the same examples I have used but not the same behavior.

I know what you mean it's ultimately the same behavior that I want to achieve, but a separate issue in my mind.

The other thread was about Exit command behavior, and how to get the exit code back to command prompt - i.e. %ErrorLevel%

So I have yet to post to that as resolved, since compiling as a console app resolves that issue.  I will do so, once I fully test that query out.

I did discover this as a side issue while working through that issue, I found this issue with RunWait on XP, which I thought best to raise as a separate thread, as to me anyway it is separate area for discussion. 

Hope that makes sense. If I should have kept that in the other thread then let me know, and apologies for breaking any rules.

 

Link to comment
Share on other sites

on your XP machines: is it really successful in invoking the BAT?

is @error <> 0 ?

#pragma compile(Console, True)
#include <Constants.au3>
#include <MsgBoxConstants.au3>

Local $Result
Local $command

$Command = @ScriptDir & "\mytest.bat"
$Result = RunWait("""" & $Command & """", @ScriptDir)

If @error Then
    MsgBox($MB_SYSTEMMODAL, "Error", "Failed to spawn: " & $Command)
Else
    MsgBox($MB_SYSTEMMODAL, "Test", "Result: " & $Result)
EndIf

Exit $Result

 

Link to comment
Share on other sites

Yes it is successfully entering the BAT.  When you run it the result of the lines: echo %myerr%  - gets returned to the cmd shell so 99 (when file.exe) does not exist.

Just tried your sample code and the line MsgBox($MB_SYSTEMMODAL, "Test", "Result: " & $Result) is invoked from the If statement which shows that it is successfully entering and executing the BAT.

Link to comment
Share on other sites

I have tried several things such as:

Instead of RunWait - Use $Result = ShellExecuteWait($Command)

Tried amending my original RunWait to things like : $Result = RunWait(@ComSpec & " /c " & "mytest.bat")   and various combinations of this type of command.

None of this makes any difference.

 

I then thought that possibly RunWait was not waiting for mytest.bat to complete, however I tested this out a couple of different ways:

1) I set $Result to a non zero value prior to the RunWait - alas $Result is always 0 on XP.

#pragma compile(Console, True)
#include <Constants.au3>
#include <MsgBoxConstants.au3> 
Local $Result
Local $command

$Result = 100
MsgBox($MB_SYSTEMMODAL, "First Result", "Result: " & $Result)
$Command = "mytest.bat"
$Result = RunWait($Command)
If @error Then
    MsgBox($MB_SYSTEMMODAL, "Error", "Failed to spawn: " & $Command)
Else
    MsgBox($MB_SYSTEMMODAL, "Test", "Result: " & $Result)
EndIf

Exit $Result

  So this proved that the value of $Result is being modified by the RunWait command.

I then put a delay in mybatch.bat:

@Echo off

set myerr=1
if not exist file.exe goto myexit

echo %myerr%
PING -n 21 127.0.0.1>nul

exit /b %myerr%

:myexit



set myerr=99
echo %myerr%
PING -n 21 127.0.0.1>nul

exit /b %myerr%

So the PING commands are a 20 second delay, this proved that the Autoit exe was waiting for the program/batch file executed by RunWait to complete before displaying the message box.

 

 

All I am trying to achieve is obtain the return code from mytest.bat  (in this example it will either be 99 or 1 depending on the existence of "file.exe".

There appears to be a distinct behavior of RunWait;  ShellExecuteWait. 

AutoIT is supported on XP SP3 - so is this a bug in AutoIt or is there some weird XP behavior that anyone knows about.

Any ideas appreciated.

Thanks in advance.

 

 

Link to comment
Share on other sites

Thanks francoiste!

I had done some research, but must have missed these articles you mentioned.  Based on my research and some tests performed I did think that this was an issue in Autoit rather than the OS - hence why I raised as a bug.  I would not have done so without trying to prove otherwise. 

Thankfully - you have found the underlying issue.  Thanks for your help.

I have modified my test code and using the wrapper solution in the second article - should work for me.

I just need to integrate these changes into my real code/scripts now.  Hopefully I can work around the issue in XP.

I will try and withdraw the bug raised.

 

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