Author Topic: More verbose message boxes  (Read 2996 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
More verbose message boxes
« on: February 26, 2021, 03:41:39 pm »
The below code is for Windows 32 and 64 bit. It's similar to what I did for my WPFMessageBox library but this uses the native message boxes in Windows. Take those first few commented lines from the bas code and paste them at the top of your program (and uncomment them). That turns over the error handling to the functions I'm giving you. These functions can be used to give more verbose error messages in your applications, especially for ones that utilize the WinAPI. See below for a screenshot of an example:

 
Screenshot 2021-02-26 152442.png


Code: QB64: [Select]
  1. 'ON ERROR GOTO ERRHANDLE
  2. 'ERRHANDLE:
  3. 'IF ERR THEN
  4. '    IF _INCLERRORLINE THEN
  5. '        CriticalError ERR, _INCLERRORLINE, _INCLERRORFILE$
  6. '        RESUME NEXT
  7. '    ELSE
  8. '        CriticalError ERR, _ERRORLINE, ""
  9. '        RESUME NEXT
  10. '    END IF
  11. 'END IF
  12. $If MESSAGEBOX = UNDEFINED Then
  13.         Function MessageBox& (ByVal Zer0 As Long, message As String, title As String, Byval MBType As _Unsigned Long)
  14.     End Declare
  15.     $Let MESSAGEBOX = TRUE
  16.  
  17. Declare Library ".\preproc"
  18.     Function __FUNCTION_NAME$ ()
  19.     Function __FUNCTION_NAME_FULL$ ()
  20.  
  21. Sub SoftError (title As String, message As String)
  22.     Const MB_OK = 0 'OK button only
  23.     Const MB_YESNO = 4 'Yes & No
  24.     Const MB_ICONSTOP = 16 'Error stop sign icon
  25.     Const MB_ICONEXCLAMATION = 48 'Exclamation-point icon
  26.     Const MB_SETFOCUS = 65536 'Set message box as focus
  27.     Const IDOK = 1 'OK button pressed
  28.     Const IDYES = 6 'Yes button pressed
  29.     Const IDNO = 7 'No button pressed
  30.  
  31.     Dim As Long Answer
  32.     Answer = MessageBox(0, message + Chr$(0), title + Chr$(0), MB_OK + MB_SETFOCUS + MB_ICONEXCLAMATION)
  33.  
  34. Sub CriticalError (ERRR, ERRLINE, INCL As String)
  35.     Const MB_OK = 0 'OK button only
  36.     Const MB_YESNO = 4 'Yes & No
  37.     Const MB_ICONSTOP = 16 'Error stop sign icon
  38.     Const MB_ICONEXCLAMATION = 48 'Exclamation-point icon
  39.     Const MB_SETFOCUS = 65536 'Set message box as focus
  40.     Const IDOK = 1 'OK button pressed
  41.     Const IDYES = 6 'Yes button pressed
  42.     Const IDNO = 7 'No button pressed
  43.  
  44.     Dim As String message
  45.     message = "The program ran into a problem that it couldn't handle." + Chr$(10) + "Error Code: " + LTrim$(Str$(ERRR)) + Chr$(10) + Chr$(10) + _ErrorMessage$ + " on line " + LTrim$(Str$(ERRLINE))
  46.     If INCL <> "" Then
  47.         message = message + " in " + INCL
  48.     Else
  49.         message = message + " in main module"
  50.     End If
  51.     message = message + Chr$(10) + "For more information about this issue and possible fixes, visit https://www.qb64.org/forum" + Chr$(10) + "Continue?"
  52.     Dim Answer As Long
  53.     Answer = MessageBox(0, message + Chr$(0), "Error: " + LTrim$(Str$(ERRR)) + Chr$(0), MB_YESNO + MB_SETFOCUS + MB_ICONSTOP)
  54.     Select Case Answer
  55.         Case IDYES
  56.             Exit Sub
  57.         Case IDNO
  58.             System
  59.     End Select
  60.  
  61. Sub CriticalErrorWinAPI (additionalInfo As String)
  62.     Const MB_OK = 0 'OK button only
  63.     Const MB_YESNO = 4 'Yes & No
  64.     Const MB_ICONSTOP = 16 'Error stop sign icon
  65.     Const MB_ICONEXCLAMATION = 48 'Exclamation-point icon
  66.     Const MB_SETFOCUS = 65536 'Set message box as focus
  67.     Const IDOK = 1 'OK button pressed
  68.     Const IDYES = 6 'Yes button pressed
  69.     Const IDNO = 7 'No button pressed
  70.  
  71.     Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H00000100
  72.     Const FORMAT_MESSAGE_FROM_SYSTEM = &H00001000
  73.     Const FORMAT_MESSAGE_IGNORE_INSERTS = &H00000200
  74.     Const LANG_NEUTRAL = &H00
  75.     Const SUBLANG_DEFAULT = &H01
  76.     $If GETLASTERROR = UNDEFINED Then
  77.         Declare Library
  78.             Function GetLastError& ()
  79.         End Declare
  80.         $Let GETLASTERROR = TRUE
  81.     $End If
  82.         Function FormatMessage& Alias FormatMessageA (ByVal dwFlags As Long, Byval lpSource As Long, Byval dwMessageId As Long, Byval dwLanguageId As Long, Byval lpBuffer As _Offset, Byval nSize As Long, Byval Arguments As _Offset)
  83.     End Declare
  84.         Function MAKELANGID& (ByVal p As Long, Byval s As Long)
  85.     End Declare
  86.     $If OFFSETTOSTRING = UNDEFINED Then
  87.         Declare CustomType Library ".\offsettostring"
  88.             Function offset_to_string$ Alias offset_to_offset (ByVal offset As _Offset)
  89.         End Declare
  90.         $Let OFFSETTOSTRING = TRUE
  91.     $End If
  92.     Dim As String message, ERRMSG
  93.     Dim As _Offset lpMsgBuf
  94.     Dim As Long msg, Answer, ERRR
  95.     ERRR = GetLastError
  96.     msg = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER Or FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, 0, ERRR, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), _Offset(lpMsgBuf), 0, 0)
  97.     ERRMSG = offset_to_string(lpMsgBuf)
  98.     If additionalInfo <> "" Then
  99.         message = "The program ran into a problem in function " + additionalInfo + Chr$(10)
  100.     Else
  101.         message = "The program ran into a problem that it couldn't handle." + Chr$(10)
  102.     End If
  103.     message = message + "Error Code: " + LTrim$(Str$(ERRR)) + Chr$(10) + Chr$(10) + ERRMSG + Chr$(10) + "For more information about this issue and possible fixes, visit https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes"
  104.     Select Case ERRR
  105.         Case 0
  106.             Exit Sub
  107.         Case 1 TO 499
  108.             message = message + "--0-499-"
  109.         Case 500 TO 999
  110.             message = message + "--500-999-"
  111.         Case 1000 TO 1299
  112.             message = message + "--1000-1299-"
  113.         Case 1300 TO 1699
  114.             message = message + "--1300-1699-"
  115.         Case 1700 TO 3999
  116.             message = message + "--1700-3999-"
  117.         Case 4000 TO 5999
  118.             message = message + "--4000-5999-"
  119.         Case 6000 TO 8199
  120.             message = message + "--6000-8199-"
  121.         Case 8200 TO 8999
  122.             message = message + "--8200-8999-"
  123.         Case 9000 TO 11999
  124.             message = message + "--9000-11999-"
  125.         Case 12000 TO 15999
  126.             message = message + "--12000-15999-"
  127.     End Select
  128.     message = message + Chr$(10) + Chr$(10) + "Continue?"
  129.     Answer = MessageBox(0, message + Chr$(0), "Error: " + LTrim$(Str$(ERRR)) + Chr$(0), MB_YESNO + MB_SETFOCUS + MB_ICONSTOP)
  130.     Select Case Answer
  131.         Case IDYES
  132.             Exit Sub
  133.         Case IDNO
  134.             System
  135.     End Select

Code: C++: [Select]
  1. //preproc.h
  2. #define __VARIABLE_NAME(Variable) (#Variable)
  3. #define __FUNCTION_NAME() (__func__)
  4. #define __FUNCTION_NAME_FULL() (__PRETTY_FUNCTION__)
  5. #define __SRC_LINE() (__LINE__)
  6. #define __SRC_FILE() (__FILE__)
  7. #define __STATIC_COUNT() (__COUNTER__)
  8. #define __COMPILE_DATE() (__DATE__)
  9. #define __COMPILE_TIME() (__TIME__)
  10. #define __SRC_LAST_EDIT() (__TIMESTAMP__)
  11. #ifdef QB64_WINDOWS
  12. #define __WIN32() (_WIN32)
  13. #define __WIN64() (_WIN64)
  14. #endif
  15. #define __INC_DEPTH() (__INCLUDE_LEVEL__)
  16. #define __BASE_FILE() (__BASE_FILE__)

Code: C++: [Select]
  1. //offsettostring.h
  2.   void *offset_to_offset(void* offset){
  3.    return offset;
  4.    }
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: More verbose message boxes
« Reply #1 on: February 26, 2021, 10:15:06 pm »
And that is impressive too!