@ERRTEXT oddity

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,376
39
Albuquerque, NM
prospero.unm.edu
#1
This is of absolutely no importance, but it bothers me that I can't understand what's going on here.

Code:
C:\>echo %@errtext[0]
The operation completed successfully.

C:\>set n=0

C:\>echo %@errtext[%n]
The system could not find the environment option that was entered.

C:\>echo %@errtext[%n%]
The operation completed successfully.

C:\>echo %n %@errtext[%n]
0 The system could not find the environment option that was entered.

C:\>echo %n %@errtext[%n%]
0 The system could not find the environment option that was entered.

C:\>echo %n% %@errtext[%n%]
0 The operation completed successfully.

C:\>for /l %i in ( 0, 1, 0 ) echo %i %@errtext[%i]
0 The system could not find the environment option that was entered.

C:\>
If variable N is set to zero, then how can %@ERRTEXT[%N] possibly differ from %@ERRTEXT[0] ?
 

samintz

Scott Mintz
May 20, 2008
1,203
11
Solon, OH, USA
#2
I can verify that. And up the ante a little:
[C:\] echo %@errtext[0]
The operation completed successfully.

[C:\] set n=0

[C:\] echo %@errtext[%n]
The system could not find the environment option that was entered.

[C:\] echo %@errtext[ %n ]
TCC: (Sys) The parameter is incorrect.
"%@errtext[ 0 ]"

[C:\] echo %@errtext[ 0 ]
TCC: (Sys) The parameter is incorrect.
"%@errtext[ 0 ]"

[C:\] echo %@errtext[%n ]
The system could not find the environment option that was entered.

[C:\] echo %@errtext[ %n]
TCC: (Sys) The parameter is incorrect.
"%@errtext[ 0]"

-Scott

Charles Dye <> wrote on 08/10/2010 04:19:17 PM:


> This is of absolutely no importance, but it bothers me that I can't
> understand what's going on here.
>
>
> Code:
> ---------
> C:\>echo %@errtext[0]
> The operation completed successfully.
>
> C:\>set n=0
>
> C:\>echo %@errtext[%n]
> The system could not find the environment option that was entered.
>
> C:\>echo %@errtext[%n%]
> The operation completed successfully.
>
> C:\>echo %n %@errtext[%n]
> 0 The system could not find the environment option that was entered.
>
> C:\>echo %n %@errtext[%n%]
> 0 The system could not find the environment option that was entered.
>
> C:\>echo %n% %@errtext[%n%]
> 0 The operation completed successfully.
>
> C:\>for /l %i in ( 0, 1, 0 ) echo %i %@errtext[%i]
> 0 The system could not find the environment option that was entered.
>
> C:\>
> ---------
> If variable N is set to zero, then how can %@ERRTEXT[%N] possibly
> differ from %@ERRTEXT[0] ?
>
>
>
>
 

rconn

Administrator
Staff member
May 14, 2008
10,096
85
#3
> This is of absolutely no importance, but it bothers me that I can't
> understand what's going on here.
Not much of a mystery -- since 0 is an invalid error number, @ERRTEXT calls
GetLastError() to get the *real* error. What GetLastError() is going to
return in this case may or may not be significant, as it depends on what the
return value of the last Windows API call prior to @ERRTEXT. There's no way
to magically guess what that might have been.

Rex Conn
JP Software
 
#5
On Tue, 10 Aug 2010 19:32:54 -0400, rconn <>
wrote:

|Not much of a mystery -- since 0 is an invalid error number, @ERRTEXT calls
|GetLastError() to get the *real* error. What GetLastError() is going to
|return in this case may or may not be significant, as it depends on what the
|return value of the last Windows API call prior to @ERRTEXT. There's no way
|to magically guess what that might have been.

Invalid?

FormatMessage() handles 0 just like any other error code. The system
error text for error id 0 is "The operation completed successfully."
Why complicate matters by treating it as invalid?
 

rconn

Administrator
Staff member
May 14, 2008
10,096
85
#6
> Invalid?
Yes.


> FormatMessage() handles 0 just like any other error code. The system
> error text for error id 0 is "The operation completed successfully."
> Why complicate matters by treating it as invalid?
This is ANCIENT code! (It never ceases to amaze me how something that
nobody has noticed for 15-20 years suddenly becomes the harbinger of the
fall of Western Civilization ...)

Because there are a lot of RTL functions that don't set an error when they
fail, and most of the rest return something like -1 or 0 or 1 instead of the
actual Windows error. If you get an error return <= 0, and you *know* that
there really was an error, then you can pass it to @ERRTEXT and get the real
error.

If you know there wasn't an error (as in this case), why are you calling
@ERRTEXT in the first place?

Rex Conn
JP Software
 
#7
On Tue, 10 Aug 2010 21:56:56 -0400, rconn <>
wrote:

|This is ANCIENT code! (It never ceases to amaze me how something that
|nobody has noticed for 15-20 years suddenly becomes the harbinger of the
|fall of Western Civilization ...)

Oh, come on. No one even said they'd lose any sleep over it. And
when observations which are suprising to some are made (regardless of
how long they went unnoticed) there's always a brief discussion. Then
you tell us how it is. We lose interest because it wasn't very
important in the first place. Several years later someone notices it
again and (having forgotten) we start the vicious circle once more.
Through all this Western Civilization stands.

|Because there are a lot of RTL functions that don't set an error when they
|fail, and most of the rest return something like -1 or 0 or 1 instead of the
|actual Windows error. If you get an error return <= 0, and you *know* that
|there really was an error, then you can pass it to @ERRTEXT and get the real
|error.

A bit baffled .. you can't pass something less than 0 to @ERRTEXT. I
suppose you meant pass 0 when you think there was an error but don't
know what it was.