Extended Message Box for Win32

Tight_Coder_Ex Tight_Coder_Ex is online now Online Apr 3rd, 2005, 6:33 am |
0
There are numerous calls to Win32 API that return detailed information as to failure via GetLastError (). I've devised a method by which to get the textual information corresponding to error and the address of the call that caused the violation. If GetLastError () returns null, then it can serve as possibly a simpler way to use MessageBox (). I've only tested this on XP, but it failed miserably on a Sony Lapton running ME.

What you need to define is; style for message box, Caption & Text. If last error was not null, then you'll see

Error < 128> @ [ 40103A ]

Message from system

What you defined as message body text

and then dependant upon MessageBox style buttons for your response
Quick reply to this message  
Assembly Syntax
  1.  
  2. ; Message definition in .rdata
  3. Errm01 dd MB_YESNO | MB_ICONSTOP | MB_DEFBUTTON2 | MB_APPLMODAL
  4. db 'MESSAGE BOX DEMO', 0
  5. db 13, 10, 9, 'Do you want to continue', 0
  6.  
  7. ; Example of how you would call function
  8. xor eax, eax ; hWnd, owners window handle
  9. mov edx, Errm01 ; Message definition
  10. call MsgBox
  11.  
  12. ; ============================================================================================
  13. ; *** MSGBOX ***
  14.  
  15. ; ENTRY: EAX = hWnd
  16. ; EDX = Base pointer ot message parameters
  17.  
  18. ; LEAVE EAX = Users response as defined in MessageBox
  19.  
  20. ; .............................................................................................
  21. FMTM EQU FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
  22.  
  23. MsgBox push esi
  24. push edi ; Save essential registers
  25. push ebx
  26.  
  27. enter 0x1c8, 0 ; Frame for MessageBox parameters
  28. mov ebx, esp ; Let EBX point to base of these params
  29. add ebx, byte 24
  30. mov esi, edx
  31. cld ; Assure auto increment of indices
  32.  
  33. ; Due to the two modes of operation of this procedure, default to simple message
  34. ; box without extended information from GetLastError.
  35.  
  36. mov [ebx], eax ; hWnd, handle of owner window
  37. lodsd ; Get style
  38. mov [ebx+12], eax ; uType, style of message box
  39. mov [ebx+8], esi ; lpCaption, title of message box
  40. push esi
  41. call _lstrlenA@4 ; Determine length of title text
  42. inc eax ; Bump past strings terminating byte
  43. add esi, eax
  44. mov [ebx+4], esi ; lpText, text in message box
  45.  
  46. ; The single determining factor whether procedure operates in extended mode or not
  47. ; is return value from GetLastError. If it isn't null, then that means we want
  48. ; to show error code, address of offending call, systems text associated with error
  49. ; and message box text from parameter 2 of MessgeBox.
  50.  
  51. call _GetLastError@0 ; Get error code if it exists.
  52. and eax, eax
  53. jnz .Ext_Mode
  54.  
  55. ; NOTE: I've choosen to use this method because Extended Mode code is
  56. ; greater than 128 bytes which puts it past a near jump.
  57.  
  58. ; Display message box in either simple or extended mode, get response from
  59. ; operator and let calling procedure determine appropriate action.
  60.  
  61. .Done lea esp, [ebp - 0x1b0] ; Point to parameters intialized earlier.
  62. call _MessageBoxA@16
  63. leave
  64.  
  65. pop ebx
  66. pop edi ; Restore essential registers
  67. pop esi
  68.  
  69. ret ; EAX = Return value of MessageBox call.
  70.  
  71. ; -------------------------------------------------------------------------------------------
  72. ; Normally I would put this in .rdata, but sake of this demonstration I'll leave it in
  73. ; this segment
  74.  
  75. .OutFmt db 9, 'Error < %d > @ [ %X ] ', 13, 10, 13, 10, '%s%s', 0
  76. ; -------------------------------------------------------------------------------------------
  77.  
  78. ; To create the extended string contents for message box, establish the 6 params
  79. ; that are required by wsprint being; OutputBuffer, FormatStr, ErrorCode,
  80. ; CallAddress, ErrorCodeStr, MsgBoxText.
  81.  
  82. .Ext_Mode mov edi, esp ; Establish pointer to wsprintf params
  83. push eax ; Save error code from GetLastError
  84. mov eax, esp
  85. add eax, byte 0x3c ; Base of Destination Ascii buffer
  86. stosd ; 1st Param for wsprintf
  87. mov eax, .OutFmt ; Formatting definition
  88. stosd ; 2nd Param for wsprintf
  89. pop eax ; Restore Error code
  90. stosd ; 3rd Param for wsprintf
  91.  
  92. ; Procedure assumes that the first occurence of E8 before the call to MSGBOX was
  93. ; the API function that generated this error.
  94.  
  95. mov esi, edi ; Save next pointer to wsprintf param
  96. mov edi, [ebp+16] ; Get callers return
  97. sub edi, byte 6 ; Point to just before call to MSGBOX
  98. mov al, 0xE8 ; Searching for call instruction
  99. xor ecx, ecx
  100. dec ecx ; Kind of large but no matter
  101. std ; We want to search backward
  102. repnz scasb
  103. inc edi ; Points to call to API
  104. cld ; Re-establish direction to default
  105. mov eax, edi
  106. xchg esi, edi
  107. stosd ; 4th Param for wsprintf (Offending call).
  108.  
  109. ; Use FormatMessage to determine text associated with this error from system
  110.  
  111. xor eax, eax
  112. push eax ; Arguments = NULL (No insertion points)
  113. push eax ; nSize, not required
  114. push edi ; We'll use this location temporaritly
  115. push dword SUBLANG_DEFAULT << 10 | LANG_NEUTRAL
  116. push dword [edi - 8] ; Error code
  117. push eax
  118. push dword FMTM
  119. call _FormatMessageA@28
  120. and eax, eax
  121. jnz $+4
  122.  
  123. int 3 ; In the unlikely event FormatMessage fails.
  124.  
  125. mov eax, [edi+12]
  126. add edi, byte 4 ; Jump over value just save by FormatMessage
  127. stosd ; Save address to string
  128. call _wsprintfA ; Format entire message
  129.  
  130. pop eax ; Point to newly created string
  131. mov [edi+4], eax ; and save for MessageBox API
  132. push dword [edi-8] ; Recover memory allocated by FormatMessage
  133. call _LocalFree@4
  134. jmp .Done ; Complete display and operators response.
  135.  

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC