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

@ERRTEXT oddity

Discussion in 'Support' started by Charles Dye, Aug 10, 2010.

  1. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,300
    Likes Received:
    39
    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] ?
     
  2. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,187
    Likes Received:
    11
    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:


     
  3. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,854
    Likes Received:
    83
    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
     
  4. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,854
    Likes Received:
    83
    GIGO, with some PIBKAC thrown in ... :-)

    Rex Conn
    JP Software
     
  5. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,938
    Likes Received:
    30
    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?
     
  6. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,854
    Likes Received:
    83
    Yes.


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

    Joined:
    May 20, 2008
    Messages:
    7,938
    Likes Received:
    30
    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.
     

Share This Page