Page 1 of 1

Problem with ErrorLevel

Posted: 09 Mar 2015, 11:54
by cybermcm

Since version Errorlevel behaves different. Example code:

Code: Select all

^!2:: ; loads Add/Remove programs
SetTitleMatchMode, 2
SetTitleMatchMode, Slow
IfWinExist, Add or Remove
	Sleep, 500
	WinActivate, Add or Remove
	Run, appwiz.cpl

if errorlevel <> 0
	msgbox, There seems to be an error :-( %ErrorLevel%
This script worked until now. New behavior: appwiz.cpl gets started but the error message also appears because ErrorLevel seems to be empty (it should be zero).
Is this a problem with my script or a bug?


Re: Problem with ErrorLevel

Posted: 09 Mar 2015, 13:57
by Coco-guest
Can you try: Run, appwiz.cpl,, UseErrorLevel
Docs wrote:Run: Does not set ErrorLevel unless UseErrorLevel (above) is in effect, in which case ErrorLevel is set to the word ERROR upon failure or 0 upon success.

Source: Run -> ErrorLevel

Re: Problem with ErrorLevel

Posted: 09 Mar 2015, 16:00
by cybermcm
Coco-guest wrote:Can you try: Run, appwiz.cpl,, UseErrorLevel
Thanks for pointing this out. It worked until the very last version (

Re: Problem with ErrorLevel

Posted: 09 Mar 2015, 19:15
by lexikos
It relates to this change:
Fixed thread interrupts to save and restore ErrorLevel more fully, which fixes:
  • Strings longer than 127 chars being truncated.
  • Objects being discarded.
  • Interruption by a timer sometimes causing ErrorLevel to be given hexadecimal formatting.
This had an unintended impact on backward-compatibility, so will be reverted soon and replaced with an alternative fix for the timer issue.

The problem is that previously, ErrorLevel was not reset when a new thread starts. Instead, ErrorLevel retains the value from the previous thread. (Since releasing v1.1.20.00 I've found comments by Chris indicating that this behaviour was intended, primarily for performance reasons.) When the new thread finishes, the value is restored to the value it had when the previous thread was interrupted. Currently in v1.1.20.00, ErrorLevel is cleared (made empty) when the new thread starts. This was unintended.

Note that a consequence of the old behaviour is that your script might not work correctly if some other thread was running and had a non-zero ErrorLevel. In addition to UseErrorLevel, you should set ErrorLevel := 0 in the other IF branch or at the start of the hotkey.

As it is (without UseErrorLevel), there's no point in checking ErrorLevel because it's not set by any of the commands you've used.

Re: Problem with ErrorLevel

Posted: 10 Mar 2015, 02:52
by cybermcm
Hi lexikos,

Thank you for your detailed explanation. I was confused because my script worked for years and now there was this new behavior. I'll use "UseErrorLevel" for my purpose.