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-chttp://www.codeproject.com/Articles/12573/Lookup-and-Display-Win-COM-Error-Strings-With-Onhttp://blogs.msdn.com/b/oldnewthing/archive/2006/11/03/942851.aspxhttp://stackoverflow.com/questions/7008047/is-there-a-way-to-get-the-string-representation-of-hresult-value-using-win-apihttp://stackoverflow.com/questions/1843581/generate-diagnostic-message-for-hresult-codeshttp://www.codeproject.com/Questions/152658/How-to-get-an-error-code-out-of-an-HRESULThttp://blogs.msdn.com/b/oldnewthing/archive/2007/11/28/6564257.aspx