News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Weird phun with GetLastError

Started by cobold, July 24, 2011, 11:05:34 AM

Previous topic - Next topic

cobold

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

dedndave

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


cobold

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.

dedndave

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

cobold

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