The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: cobold on July 24, 2011, 11:05:34 AM

Title: Weird phun with GetLastError
Post by: cobold on July 24, 2011, 11:05:34 AM
Hi,

if you assemble the following snippet without the inkey or ustr$, GetLastError reports: 0
But if you use inkey or ustr$ EVEN AFTER the call to GetLastError it will report: 203

Just out of curiosity: Can someone figure out what's going on here. I have no idea.


include \masm32\include\masm32rt.inc

.CODE
start:
    call main
    ;inkey
    exit

main PROC
    LOCAL   dwLastErr       :DWORD,
            szError[1024]   :BYTE

    invoke GetLastError
    mov dwLastErr,eax
    invoke FormatMessage,FORMAT_MESSAGE_FROM_SYSTEM,0,dwLastErr,0,ADDR szError,1024,0
    print str$(dwLastErr)," "
    print ADDR szError,13,10

    print ustr$(dwLastErr)          ; Err 203 ? how could this call to ustr$ affect LastErr
                                    ; AFTERWARDS?

    ret
main ENDP

end start
Title: Re: Weird phun with GetLastError
Post by: dedndave on July 24, 2011, 12:54:40 PM
GetLastError reports whatever the last error was   :bg
perhaps the 203 (ERROR_ENVVAR_NOT_FOUND) was some OS internal error that was corrected or otherwise accounted for

some functions may require you to use SetLastError to clear the previous error before calling the function

i doubt the inkey has anything to do with the reported error, at least directly
the inkey macro uses the MSVCRT
perhaps when the MSVCRT initializes, it looks for an environment variable that you do not have

Title: Re: Weird phun with GetLastError
Post by: cobold on July 24, 2011, 01:01:03 PM
Thanks for the reply. In the meantime I found out that this only occurs if you link with msvcrt. So it seems this library causes Err 203.
Title: Re: Weird phun with GetLastError
Post by: dedndave on July 24, 2011, 01:31:08 PM
as i mentioned, it probably looks for some environment variable that you do not have
when it fails to locate it, it uses the default setting
although - that makes me curious what variable it's looking for   :P
maybe you can set the location of the MSVCRT with a var
that would be a handy one to know, as you could easily test software using different versions of MSVCRT

on a quick browse, i see it looks at PATH and COMSPEC - you probably have those

here we are...
maybe it's the TZ var (TimeZone)
http://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
it appears to be an obsolete var that the MSVCRT looks at for backward compatibilty
Title: Re: Weird phun with GetLastError
Post by: cobold on July 24, 2011, 02:09:45 PM
I think upon initializing the module ist looks for __MSVCRT_HEAP_SELECT (thks to olly):


CPU Disasm
Address   Hex dump          Command                                  Comments
77BFA176  |.  33DB          XOR EBX,EBX
77BFA178  |.  53            PUSH EBX                                 ; /Size => 0
77BFA179  |.  53            PUSH EBX                                 ; |Buffer => NULL
77BFA17A  |.  BF E81FBE77   MOV EDI,77BE1FE8                         ; |ASCII "__MSVCRT_HEAP_SELECT"
77BFA17F  |.  57            PUSH EDI                                 ; |Name => "__MSVCRT_HEAP_SELECT"
77BFA180  |.  FFD6          CALL ESI                                 ; \KERNEL32.GetEnvironmentVariableA