home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / warptlk3.zip / TOOLKIT / SAMPLES / OS2 / DLLAPI / DLAPI_IN.C < prev    next >
C/C++ Source or Header  |  1995-08-24  |  9KB  |  172 lines

  1. /*static char *SCCSID = "@(#)dlapi_in.c    6.8 92/02/18";*/
  2. /*==============================================================*\
  3.  *  DLAPI_IN.C - routines for initialization and exit processing*
  4.  *      (C) Copyright IBM Corporation 1992.                     *
  5.  *--------------------------------------------------------------*
  6.  *                                                              *
  7.  *  This module contains the code for application initialization*
  8.  *  as well as the code for exit list processing                *
  9.  *                                                              *
  10.  *--------------------------------------------------------------*
  11.  *                                                              *
  12.  *  This source file contains the following functions:          *
  13.  *                                                              *
  14.  *      Init() - initialization routines                        *
  15.  *      ExitProc(usTermCode) - exit list processing procedure   *
  16.  *      GPFHandler(parg) - GPFault and guard page excep handler *
  17.  *                                                              *
  18. \*==============================================================*/
  19.  
  20. /*--------------------------------------------------------------*\
  21.  *  Include files, macros, defined constants, and externs       *
  22. \*--------------------------------------------------------------*/
  23. #include "dllapi.h"
  24.  
  25. /****************************************************************\
  26.  *  Initialization routine                                      *
  27.  *--------------------------------------------------------------*
  28.  *                                                              *
  29.  *  Name:    Init()                                             *
  30.  *                                                              *
  31.  *  Purpose: Performs initialization functions required before  *
  32.  *           the main window can be created.                    *
  33.  *                                                              *
  34.  *  Usage:   Called once before the main window is created.     *
  35.  *                                                              *
  36.  *  Method:  - installs the routine ExitProc into the           *
  37.  *              DosExitList chain                               *
  38.  *           - registers all window classes                     *
  39.  *           - performs any command line processing             *
  40.  *                                                              *
  41.  *  Returns: TRUE - initialization is successful                *
  42.  *           FALSE - initialization failed                      *
  43.  *                                                              *
  44. \****************************************************************/
  45. BOOL Init(VOID)
  46. {
  47.     /* Add ExitProc to the exit list to handle the exit processing */
  48.     if(DosExitList(EXLST_ADD, ExitProc))
  49.     {
  50.         return FALSE;
  51.     }
  52.                        /* load application name from resource file */
  53.     if(!WinLoadString(hab, (HMODULE)0, IDS_APPNAME, MAXAPPNAMELEN, szAppName))
  54.         return FALSE;
  55.     if(!WinLoadString(hab, 0, IDS_UNTITLED, MESSAGELEN, szUntitled))
  56.         return FALSE;
  57.                           /* register the main client window class */
  58.     if(WinRegisterClass(hab,
  59.                         (PSZ)szAppName,
  60.                         (PFNWP)MainWndProc,
  61.                         CS_SIZEREDRAW | CS_SYNCPAINT | CS_CLIPCHILDREN,
  62.                         0) == 0)
  63.         return FALSE;
  64.  
  65.     /* Turn off hard-error popup after GP-FAULT. No return codes
  66.        checked as it is cosmetic and if it doesn't work the only side
  67.        effect will be to have the default hard-error popup show up. */
  68.  
  69.     DosError(FERR_DISABLEEXCEPTION | FERR_DISABLEHARDERR);
  70.  
  71.     return TRUE;
  72. }                                                         /* Init() */
  73.  
  74. /****************************************************************\
  75.  *  Exit list processing procedure                              *
  76.  *--------------------------------------------------------------*
  77.  *                                                              *
  78.  *  Name:    ExitProc(usTermCode)                               *
  79.  *                                                              *
  80.  *  Purpose: Cleans up certain resources when the application   *
  81.  *           terminates                                         *
  82.  *                                                              *
  83.  *  Usage:   Routine is called by DosExitList when the          *
  84.  *           application exits                                  *
  85.  *                                                              *
  86.  *  Method:  global resources, such as the main window and      *
  87.  *           message queue, are destroyed and any system        *
  88.  *           resources used are freed                           *
  89.  *                                                              *
  90.  *  Returns: Returns EXLST_EXIT to the DosExitList handler      *
  91.  *                                                              *
  92. \****************************************************************/
  93. VOID APIENTRY ExitProc(ULONG usTermCode)
  94.                                   /* code for the reason for termination */
  95. {
  96.    WinDestroyWindow(hwndMainFrame);
  97.    /*--------------------------------------------------*\
  98.     *      Any other system resources used             *
  99.     *      (e.g. memory or files) should be freed here *
  100.    \*--------------------------------------------------*/
  101.    WinDestroyMsgQueue(hmq);
  102.  
  103.    WinTerminate(hab);
  104.  
  105.    DosExitList(EXLST_EXIT, (PFNEXITLIST)0L);    /* termination complete */
  106.  
  107.    /* This routine currently doesn't use the usTermCode parameter so *\
  108.     *  it is referenced here to prevent an 'Unreferenced Parameter'  *
  109.    \*  warning at compile time                                       */
  110.  
  111.    return;
  112. }                                                          /* ExitProc() */
  113.  
  114. /****************************************************************\
  115.  *  Do nothing Guard Page Exception Handler                     *
  116.  *--------------------------------------------------------------*
  117.  *                                                              *
  118.  *  Name:    GPFHandler(PXCPT)                                  *
  119.  *                                                              *
  120.  *  Purpose: To notify user when a guard page entered exception *
  121.  *           occurs                                             *
  122.  *                                                              *
  123.  *  Usage:   Routine is called when a guard page is accessed    *
  124.  *                                                              *
  125.  *  Method:  Whenever a guard page is accessed, a message       *
  126.  *           box is put on the screen indicating this.          *
  127.  *                                                              *
  128.  *  Returns: Returns HANDLED if guard page exception,           *
  129.  *           otherwise, returns NOT_HANDLED                     *
  130. \****************************************************************/
  131. ULONG GPFHandler(EXCEPTIONREPORTRECORD *parg,
  132.                  EXCEPTIONREGISTRATIONRECORD *pRegisRecord,
  133.                  PCONTEXTRECORD  pContextRecord,
  134.                  PVOID pvSpare)
  135. {
  136.     LONG  sRet;
  137.     CHAR  szText[MESSAGELEN];
  138.  
  139.     if(parg->ExceptionNum == XCPT_FLOAT_DIVIDE_BY_ZERO ||
  140.        parg->ExceptionNum == XCPT_INTEGER_DIVIDE_BY_ZERO)
  141.     {
  142.        MessageBox(hwndMain, IDMSG_DIVIDEZERO, "Error !",
  143.                 MB_OK | MB_ERROR | MB_MOVEABLE, TRUE);
  144.        DosExit(EXIT_PROCESS, RETURN_ERROR);
  145.        return(XCPT_CONTINUE_SEARCH);
  146.     }
  147.     /* Notify the user when they have accessed a guard page, and let
  148.        the system default guard page handler handle the exception. */
  149.     if(parg->ExceptionNum == XCPT_GUARD_PAGE_VIOLATION)
  150.     {
  151.        WinLoadMessage(hab, (HMODULE)NULL, IDMSG_ACCESSGUARDPAGE, MESSAGELEN,
  152.                       (PSZ)szText);
  153.        WinAlarm(HWND_DESKTOP, WA_ERROR);
  154.  
  155.        sRet = WinMessageBox(HWND_DESKTOP, hwndMain, szText, "Note !",
  156.                             IDM_MSGBOX, MB_MOVEABLE | MB_OKCANCEL | MB_ERROR);
  157.        switch (sRet)
  158.        {
  159.           case MBID_OK:
  160.              pFlList = pBackup;
  161.              break;
  162.  
  163.           case MBID_CANCEL:
  164.              DosExit(EXIT_PROCESS, RETURN_ERROR);
  165.              break;
  166.        }
  167.        WinInvalidateRect(hwndMain,NULL,TRUE);
  168.        return(XCPT_CONTINUE_EXECUTION);
  169.     }
  170.     return(XCPT_CONTINUE_SEARCH);
  171. }
  172.