home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / FILEDLG6.ZIP / ERRMSG.ZIP / ERRMSG.C < prev    next >
Text File  |  1990-11-14  |  10KB  |  170 lines

  1. /****************************************************************************
  2.  * USHORT ErrMessageBox( HWND hwndOwner,PSZ pszCaption,USHORT usErrorCode,  *                       *
  3.  *                       PERRORMSG perrormsg,USHORT cErrMsgCnt, ... )       *
  4.  * Purpose          The ErrMessageBox function displays in a message        *
  5.  *                  box the error message associated with the given         *
  6.  *                  error code.                                             *
  7.  *                                                                          *
  8.  * Parameters       hwndOwner identifies the owner window of the            *
  9.  *                  message-box window. The owner window is activated       *
  10.  *                  when ErrMessageBox returns.                             *
  11.  *                                                                          *
  12.  *                  pszCaption points to the title of the message-box       *
  13.  *                  window. If this parameter is NULL, "Error" (the         *
  14.  *                  default title) is displayed. The maximum length         *
  15.  *                  of the text is device-dependent. If the text is         *
  16.  *                  too long, it will be clipped.                           *
  17.  *                                                                          *
  18.  *                  usErrorCode specifies the error value returned          *
  19.  *                  by an MS OS/2 function.                                 *
  20.  *                                                                          *
  21.  *                  perrormsg points to the user defined error message      *
  22.  *                  list. If this parameter is NULL then the default        *
  23.  *                  system error message list will be used.                 *
  24.  *                                                                          *
  25.  *                  cErrMsgCnt contains the number of items in the user     *
  26.  *                  defined error message list. This parameter is           *
  27.  *                  ignored if perrormsg is NULL.                           *
  28.  *                                                                          *
  29.  *                  The optional arguments are passed to the vsprintf       *
  30.  *                  function in order to allow error messages to have       *
  31.  *                  variable contents. NOTE THAT THIS FUNCTION REQUIRES     *
  32.  *                  THAT ALL POINTER ARGUMENTS (SUCH AS POINTERS TO         *
  33.  *                  STRINGS) MUST BE FAR POINTERS.                          *
  34.  *                                                                          *
  35.  * Return Value     The return value indicates the user's response to       *
  36.  *                  the message. It can be one of the following values:     *
  37.  *                                                                          *
  38.  *                  MBID_ABORT   Abort button was selected.                 *
  39.  *                  MBID_CANCEL  Cancel button was selected.                *
  40.  *                  MBID_ENTER   Enter button was selected.                 *
  41.  *                  MBID_IGNORE  Ignore button was selected.                *
  42.  *                  MBID_NO      No button was selected.                    *
  43.  *                  MBID_OK      OK button was selected.                    *
  44.  *                  MBID_RETRY   Retry button was selected.                 *
  45.  *                  MBID_YES     Yes button was selected.                   *
  46.  *                  MDID_ERROR   The WinMessageBox function failed--        *
  47.  *                                  an error occurred.                      *
  48.  *                                                                          *
  49.  *                  If a message box has a Cancel button, MBID_CANCEL       *
  50.  *                  is return if the ESCAPE key is pressed or if the        *
  51.  *                  Cancel button is selected. If the message box has       *
  52.  *                  no Cancel button, pressing the ESCAPE key has no        *
  53.  *                  effect.                                                 *
  54.  *                                                                          *
  55.  * Notes            If a message-box window is created as part of the       *
  56.  *                  processing of a dialog window, the dialog window        *
  57.  *                  should be made the owner of the message-box             *
  58.  *                  window.                                                 *
  59.  *                                                                          *
  60.  *                  If a system modal message-box window is created to      *
  61.  *                  tell the user that the system is running out of         *
  62.  *                  memory, the strings passed into this function           *
  63.  *                  should not be taken from a resource file because        *
  64.  *                  an attempt to load the resource file may fail due       *
  65.  *                  to lack of memory. Such a message-box window can        *
  66.  *                  safely use the hand icon (MB_ICONHAND), however,        *
  67.  *                  because this icon is always memory-resident.            *
  68.  *                                                                          *
  69.  *                  If the message box contains a help pushbutton, the      *
  70.  *                  message box window identifier will be set to the        *
  71.  *                  error code value.                                       *
  72.  *                                                                          *
  73.  *                  The total length of the error message string after      *
  74.  *                  it has been formatted and stored by vsprintf must       *
  75.  *                  not exceed 512 characters (including the terminating    *
  76.  *                  NULL character).                                        *
  77.  *                                                                          *
  78.  *                                                                          *
  79.  * Modifications -                                                          *
  80.  *      09-Oct-1989 : Initial version.                                      *
  81.  *      27-Nov-1989 : Added variable arguments.                             *
  82.  ****************************************************************************/
  83.  
  84.     #define INCL_WINDIALOGS
  85.     #define INCL_DOSSEMAPHORES
  86.     #include <os2.h>
  87.     #include <stdio.h>
  88.     #include <stdarg.h>
  89.  
  90.     #include "errmsg.h"
  91.     #include "errconst.h"
  92.  
  93. /****************************************************************************/
  94.     USHORT cdecl far _loadds _export ErrMessageBox( HWND hwndOwner,
  95.                                                     PSZ pszCaption,
  96.                                                     USHORT usErrorCode,
  97.                                                     PERRORMSG perrormsg,
  98.                                                     USHORT cErrMsgCnt,
  99.                                                     ... )
  100.     {
  101.         static ULONG  hsem = 0L;  // semaphore to control access to function
  102.         static CHAR   szBuf[512]; // buffer for formatted error message
  103.  
  104.         PERRORMSG   pList;      // ptr to list to be searched
  105.         USHORT      usCount;    // number of items in list
  106.         USHORT      i;          // counter
  107.         PSZ         pszMsg;     // ptr to error message
  108.         USHORT      flStyle;    // message box style
  109.         va_list     arglist;    // pointer to variable length argument list
  110.         USHORT      usResult;   // result code
  111.  
  112.     /* Make sure this is the only thread accessing this function */
  113.         DosSemRequest( &hsem,SEM_INDEFINITE_WAIT );
  114.  
  115.     /* Select error list that is to be searched */
  116.         if ( perrormsg == NULL )
  117.             { /* Use system error list */
  118.             if ( usErrorCode < 0x1000 || usErrorCode >= 0x8000 )
  119.                 {
  120.                 pList   = SysErrMsg;
  121.                 usCount = SysErrCount;
  122.                 }
  123.             else
  124.                 {
  125.                 pList   = PMErrMsg;
  126.                 usCount = PMErrCount;
  127.                 }
  128.             }
  129.         else
  130.             { /* Use user supplied error list */
  131.             pList   = perrormsg;
  132.             usCount = cErrMsgCnt;
  133.             }
  134.  
  135.     /* Search selected list for error code */
  136.         for ( i = 0; i < usCount; i++ )
  137.             if ( pList[i].usCode == usErrorCode )
  138.                 {
  139.                 pszMsg  = pList[i].pszMsg;
  140.                 flStyle = pList[i].flStyle;
  141.                 break;
  142.                 }
  143.  
  144.     /* If error code not in list then display default error message */
  145.         if ( i == usCount )
  146.             {
  147.             sprintf( szBuf,
  148.                      (perrormsg == NULL) ? szUnkSysError : szUnkAppError,
  149.                      usErrorCode,usErrorCode );
  150.             flStyle = MB_OK | MB_ICONEXCLAMATION;
  151.             }
  152.  
  153.     /* Error code found in list so format error message */
  154.         else
  155.             {
  156.             va_start( arglist,cErrMsgCnt );
  157.             vsprintf( szBuf,pszMsg,arglist );
  158.             va_end( arglist );
  159.             }
  160.  
  161.     /* Display message box */
  162.         usResult = WinMessageBox( HWND_DESKTOP,hwndOwner,szBuf,
  163.                                   pszCaption,usErrorCode,flStyle );
  164.  
  165.     /* Clear semaphore and return to caller */
  166.         DosSemClear( &hsem );
  167.         return usResult;
  168.     }
  169. /****************************************************************************/
  170.