News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Format HRESULT message

Started by guga, July 18, 2014, 01:31:53 AM

Previous topic - Next topic

guga

A small piece of code to display error code for win32 and Hresult (as much as possible)



Proc ReportWinError:
    Arguments @Caption
    Local @String, @ErrCode
    Structure @StringtoAdd 64, @StringtoAdd_DataDis 0
    Uses ebx, ecx, edx

    call 'Kernel32.GetLastError' | mov D@ErrCode eax

    mov edx (&SUBLANG_DEFAULT shl 16 or &LANG_NEUTRAL) ; to retrieve potential HRESULT error, it is necessary to set FORMAT_MESSAGE_IGNORE_INSERTS equate
    lea ecx D@String
    call 'Kernel32.FormatMessageA' &FORMAT_MESSAGE_ALLOCATE_BUFFER__&FORMAT_MESSAGE_FROM_SYSTEM__&FORMAT_MESSAGE_IGNORE_INSERTS, &NULL, eax, edx, ecx, 256, &NULL
    ...If eax <> 0
        call 'User32.MessageBoxA' &NULL, D@String, D@Caption, &MB_ICONERROR
        call 'Kernel32.LocalFree' D@String
    ...Else
        ; release message, even in cases of failure
        call 'Kernel32.LocalFree' D@String
        call HRESULTtoWCode D@ErrCode
        ..If eax = 0
            lea ebx D@ErrCode
            C_call 'msvcrt.vsprintf' D@StringtoAdd, {B$ "Unknown Error code value =  0x%08X", 0}, ebx
            call 'User32.MessageBoxA' &NULL, D@StringtoAdd, D@Caption, &MB_ICONERROR
        ..Else
            mov edx (&SUBLANG_DEFAULT shl 16 or &LANG_NEUTRAL)
            lea ecx D@String
            call 'Kernel32.FormatMessageA' &FORMAT_MESSAGE_ALLOCATE_BUFFER__&FORMAT_MESSAGE_FROM_SYSTEM__&FORMAT_MESSAGE_IGNORE_INSERTS, &NULL, eax, edx, ecx, 256, &NULL
            call 'User32.MessageBoxA' &NULL, D@String, D@Caption, &MB_ICONERROR
            call 'Kernel32.LocalFree' D@String
        ..End_If
    ...End_If

EndP

[MAKE_HRESULT_IMM | ( (#1 shl 31) or (#2 shl 16) or #3)]

Proc HRESULTtoWCode:
    Arguments @ErrorCode

    mov eax D@ErrorCode
    .If_And eax >s= {MAKE_HRESULT_IMM &SEVERITY_ERROR, &FACILITY_ITF, 512}, eax <s=  ({MAKE_HRESULT_IMM &SEVERITY_ERROR, (&FACILITY_ITF+1), 0} -1)
        sub eax {MAKE_HRESULT_IMM &SEVERITY_ERROR, &FACILITY_ITF, 512}
        movzx eax ax
    .Else
        mov eax 0 ; no message found to be converted
    .End_If

EndP


example of usage:

     call ReportHResultError {B$ "Hello", 0}


References:
http://stackoverflow.com/questions/455434/how-should-i-use-formatmessage-properly-in-c
http://www.codeproject.com/Articles/12573/Lookup-and-Display-Win-COM-Error-Strings-With-On
http://blogs.msdn.com/b/oldnewthing/archive/2006/11/03/942851.aspx
http://stackoverflow.com/questions/7008047/is-there-a-way-to-get-the-string-representation-of-hresult-value-using-win-api
http://stackoverflow.com/questions/1843581/generate-diagnostic-message-for-hresult-codes
http://www.codeproject.com/Questions/152658/How-to-get-an-error-code-out-of-an-HRESULT
http://blogs.msdn.com/b/oldnewthing/archive/2007/11/28/6564257.aspx
Coding in Assembly requires a mix of:
80% of brain, passion, intuition, creativity
10% of programming skills
10% of alcoholic levels in your blood.

My Code Sites:
http://rosasm.freeforums.org
http://winasm.tripod.com