home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / os2 / vmm / vmm_init.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  10KB  |  207 lines

  1. /*static char *SCCSID = "@(#)vmm_init.c    6.12 92/02/18";*/
  2. /*==============================================================*\
  3.  *  VMM_INIT.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 "vmm.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. #ifndef VER630
  49.     if(DosExitList(EXLST_ADD, ExitProc))
  50.     {
  51.         return FALSE;
  52.     }
  53. #endif
  54.  
  55.                        /* load application name from resource file */
  56.     if(!WinLoadString(hab, (HMODULE)0, IDS_APPNAME, MAXAPPNAMELEN, szAppName))
  57.         return FALSE;
  58.     if(!WinLoadString(hab, 0, IDS_UNTITLED, MESSAGELEN, szUntitled))
  59.         return FALSE;
  60.                           /* register the main client window class */
  61.     if(WinRegisterClass(hab,
  62.                         (PSZ)szAppName,
  63.                         (PFNWP)MainWndProc,
  64.                         CS_SIZEREDRAW | CS_SYNCPAINT | CS_CLIPCHILDREN,
  65.                         0) == 0)
  66.         return FALSE;
  67.  
  68.     /* Turn off hard-error popup after GP-FAULT. No return codes
  69.        checked as it is cosmetic and if it doesn't work the only side
  70.        effect will be to have the default hard-error popup show up. */
  71.  
  72.     DosError(FERR_DISABLEEXCEPTION | FERR_DISABLEHARDERR);
  73.  
  74.     return TRUE;
  75.  
  76. }  /* Init() */
  77.  
  78. /****************************************************************\
  79.  *  Exit list processing procedure                              *
  80.  *--------------------------------------------------------------*
  81.  *                                                              *
  82.  *  Name:    ExitProc(usTermCode)                               *
  83.  *                                                              *
  84.  *  Purpose: Cleans up certain resources when the application   *
  85.  *           terminates                                         *
  86.  *                                                              *
  87.  *  Usage:   Routine is called by DosExitList when the          *
  88.  *           application exits                                  *
  89.  *                                                              *
  90.  *  Method:  global resources, such as the main window and      *
  91.  *           message queue, are destroyed and any system        *
  92.  *           resources used are freed                           *
  93.  *                                                              *
  94.  *  Returns: Returns EXLST_EXIT to the DosExitList handler      *
  95.  *                                                              *
  96. \****************************************************************/
  97. VOID APIENTRY ExitProc(ULONG usTermCode)
  98.                                   /* code for the reason for termination */
  99. {
  100.    WinDestroyWindow(hwndMainFrame);
  101.    /*--------------------------------------------------*\
  102.     *      Any other system resources used             *
  103.     *      (e.g. memory or files) should be freed here *
  104.    \*--------------------------------------------------*/
  105.    WinDestroyMsgQueue(hmq);
  106.  
  107.    WinTerminate(hab);
  108.  
  109.    DosExitList(EXLST_EXIT, (PFNEXITLIST)0L);    /* termination complete */
  110.  
  111.    /* This routine currently doesn't use the usTermCode parameter so *\
  112.     *  it is referenced here to prevent an 'Unreferenced Parameter'  *
  113.    \*  warning at compile time                                       */
  114.    return;
  115. }                                                          /* ExitProc() */
  116.  
  117. /****************************************************************\
  118.  *  Do nothing Guard Page Exception Handler                     *
  119.  *--------------------------------------------------------------*
  120.  *                                                              *
  121.  *  Name:    GPFHandler(PXCPT)                                  *
  122.  *                                                              *
  123.  *  Purpose: To notify user when a guard page entered exception *
  124.  *           occurs                                             *
  125.  *                                                              *
  126.  *  Usage:   Routine is called when a guard page is accessed    *
  127.  *                                                              *
  128.  *  Method:  Whenever a guard page is accessed, a message       *
  129.  *           box is put on the screen indicating this.          *
  130.  *                                                              *
  131.  *  Returns: Returns HANDLED if guard page exception,           *
  132.  *           otherwise, returns NOT_HANDLED                     *
  133. \****************************************************************/
  134. ULONG GPFHandler(PEXCEPTIONREPORTRECORD parg)
  135. {
  136.     ULONG   flMemAttrs=0;
  137.     ULONG   ulMemSize;
  138.     CHAR    szText[MESSAGELEN];
  139.     USHORT  usRet;
  140.  
  141.     /* Notify the user when they have accessed a guard page, and let
  142.        the system default guard page handler handle the exception. */
  143.  
  144.     if(parg->ExceptionNum == XCPT_GUARD_PAGE_VIOLATION)
  145.     {
  146.         MessageBox(hwndMain, IDMSG_ACCESSGUARDPAGE, "Note !",
  147.                         MB_MOVEABLE | MB_OK | MB_ICONASTERISK, FALSE);
  148.  
  149.         WinInvalidateRect(hwndMain,NULL,TRUE);
  150.  
  151.         return(XCPT_CONTINUE_EXECUTION);
  152.     }
  153.     if(parg->ExceptionNum == XCPT_ACCESS_VIOLATION)
  154.     {
  155.         if (parg->ExceptionInfo[1])
  156.         {
  157.             ulMemSize = 1L;
  158.             DosQueryMem((PVOID)(parg->ExceptionInfo[1]),
  159.                         &ulMemSize,&flMemAttrs);
  160.  
  161.             /* If the exception is due to accessing a page that has
  162.                been allocated but not committed, then commit the page.
  163.                Otherwise, GP Fault. */
  164.  
  165.             if (((flMemAttrs & PAG_FREE) || (flMemAttrs & PAG_COMMIT)) == 0)
  166.             {
  167.                WinLoadMessage(hab, (HMODULE)NULL, IDMSG_COMMITTTOWRITE,
  168.                                     MESSAGELEN, (PSZ)szText);
  169.                WinAlarm(HWND_DESKTOP, WA_ERROR);
  170.                usRet = WinMessageBox(HWND_DESKTOP,
  171.                                      hwndMain,
  172.                                      szText,
  173.                                      (PSZ)"Warning !",
  174.                                      IDM_MSGBOX,
  175.                                      MB_YESNO | MB_ICONASTERISK);
  176.                switch(usRet)
  177.                {
  178.                   case MBID_YES:
  179.                       if (DosSetMem((PVOID)(parg->ExceptionInfo[1]),
  180.                                  1L,PAG_DEFAULT | PAG_COMMIT) != 0L)
  181.                       {
  182.                          DosExit(EXIT_PROCESS, RETURN_ERROR);
  183.                          return(XCPT_CONTINUE_SEARCH);
  184.                       }
  185.                       else                        /* Commit successfully */
  186.                          return(XCPT_CONTINUE_EXECUTION);
  187.  
  188.                   case MBID_NO:
  189.                       DosExit(EXIT_PROCESS, RETURN_ERROR);
  190.                       return(XCPT_CONTINUE_SEARCH);
  191.                }
  192.             }
  193.         }
  194.     /* At this point, we have a GP Fault. We cannot recover, so raise the
  195.     terminate process exception and return (GP Fault) exception handled. */
  196.  
  197.         MessageBox(hwndMain, IDMSG_PROTECTIONERROR, "Error !",
  198.                     MB_MOVEABLE | MB_OK | MB_ICONHAND, TRUE);
  199.  
  200.         DosExit(EXIT_PROCESS, RETURN_ERROR);
  201.         return(XCPT_CONTINUE_SEARCH);
  202.     }
  203.  /* The exception is not one that we handle, so pass it on as NOT_HANDLED. */
  204.     DosExit(EXIT_PROCESS, RETURN_ERROR);
  205.     return(XCPT_CONTINUE_SEARCH);
  206. }
  207.