Annotation of os232sdk/toolkt20/c/samples/vmm/vmm_init.c, revision 1.1.1.1

1.1       root        1: /*==============================================================*\
                      2:  *  Vmm_init.c - routines for initialization and exit processing       *
                      3:  *     Created 1990, Microsoft, IBM Corp.                              *
                      4:  *--------------------------------------------------------------*
                      5:  *                                                             *
                      6:  *  This module contains the code for application initialization*
                      7:  *  as well as the code for exit list processing               *
                      8:  *                                                             *
                      9:  *--------------------------------------------------------------*
                     10:  *                                                             *
                     11:  *  This source file contains the following functions:         *
                     12:  *                                                             *
                     13:  *     Init() - initialization routines                        *
                     14:  *     ExitProc(usTermCode) - exit list processing procedure   *
                     15:  *     GPFHandler(parg) - GPFault and guard page excep handler *
                     16:  *                                                             *
                     17: \*==============================================================*/
                     18: 
                     19: /*--------------------------------------------------------------*\
                     20:  *  Include files, macros, defined constants, and externs      *
                     21: \*--------------------------------------------------------------*/
                     22: 
                     23: #define  LINT_ARGS
                     24: 
                     25: #define  INCL_WIN
                     26: #define  INCL_DOSPROCESS
                     27: #define  INCL_DOSSIGNALS
                     28: #define  INCL_DOSMISC
                     29: #define  INCL_DOSMEMMGR
                     30: #include <os2.h>
                     31: #include <string.h>
                     32: #include "vmm_main.h"
                     33: #include "vmm_xtrn.h"
                     34: 
                     35: /*--------------------------------------------------------------*\
                     36:  *  Entry point declarations                                   *
                     37: \*--------------------------------------------------------------*/
                     38: 
                     39: ULONG  GPFHandler(PXCPT parg);
                     40: 
                     41: /*--------------------------------------------------------------*\
                     42:  *  Global variables                                           *
                     43: \*--------------------------------------------------------------*/
                     44: 
                     45: extern HWND    hwndMain;
                     46: 
                     47: EHS ehs = {0, &GPFHandler};
                     48: 
                     49: /****************************************************************\
                     50:  *  Initialization routine                                     *
                     51:  *--------------------------------------------------------------*
                     52:  *                                                             *
                     53:  *  Name:   Init()                                             *
                     54:  *                                                             *
                     55:  *  Purpose: Performs initialization functions required        *
                     56:  *             before the main window can be created.          *
                     57:  *                                                             *
                     58:  *  Usage:  Called once before the main window is created.     *
                     59:  *                                                             *
                     60:  *  Method:                                                    *
                     61:  *         - installs the routine ExitProc into the            *
                     62:  *             DosExitList chain                               *
                     63:  *         - registers all window classes                      *
                     64:  *         - performs any command line processing              *
                     65:  *                                                             *
                     66:  *  Returns:                                                   *
                     67:  *         TRUE - initialization is successful                 *
                     68:  *         FALSE - initialization failed                       *
                     69: \****************************************************************/
                     70: 
                     71: BOOL Init(VOID)
                     72: {
                     73:     /* Add ExitProc to the exit list to handle the exit processing */
                     74: #ifndef VER630
                     75:     if(DosExitList(EXLST_ADD, (PFNEXITLIST)ExitProc))
                     76:     {
                     77:        return FALSE;
                     78:     }
                     79: #endif
                     80: 
                     81: #ifdef LATER
                     82:     /* initialize the print dialog structures */
                     83:     InitPrintingDialogs();
                     84: #endif
                     85: 
                     86:     /* load application name from resource file */
                     87:     if(WinLoadString(hab, NULL, IDS_APPNAME, MAXAPPNAMELEN, szAppName) == 0)
                     88:     {
                     89:        return FALSE;
                     90:     }
                     91:     /* register the main client window class */
                     92:     if(WinRegisterClass(hab,
                     93:                        (PSZ)szAppName,
                     94:                        (PFNWP)MainWndProc,
                     95:                        CS_SIZEREDRAW | CS_SYNCPAINT | CS_CLIPCHILDREN,
                     96:                        0) == 0)
                     97:     {
                     98:        return FALSE;
                     99:     }
                    100: 
                    101:     if(DosSetExceptionHandler(&ehs) != 0)
                    102:     {
                    103:        return FALSE;
                    104:     }
                    105:     /*--------------------------------------------------*\
                    106:      *     Add any command line processing here        *
                    107:     \*--------------------------------------------------*/
                    108: 
                    109:     /* Turn off hard-error popup after GP-FAULT. No return codes
                    110:        checked as it is cosmetic and if it doesn't work the only side
                    111:        effect will be to have the default hard-error popup show up. */
                    112: 
                    113: 
                    114:     DosError(FERR_DISABLEEXCEPTION | FERR_DISABLEHARDERR);
                    115: 
                    116:     return TRUE;
                    117: 
                    118: }  /* Init() */
                    119: 
                    120: 
                    121: 
                    122: /****************************************************************\
                    123:  *  Exit list processing procedure                             *
                    124:  *--------------------------------------------------------------*
                    125:  *                                                             *
                    126:  *  Name:   ExitProc(usTermCode)                               *
                    127:  *                                                             *
                    128:  *  Purpose: Cleans up certain resources when the application  *
                    129:  *             terminates                                      *
                    130:  *                                                             *
                    131:  *  Usage:  Routine is called by DosExitList when the          *
                    132:  *         application exits                                   *
                    133:  *                                                             *
                    134:  *  Method: global resources, such as the main window and      *
                    135:  *         message queue, are destroyed and any system         *
                    136:  *         resources used are freed                            *
                    137:  *                                                             *
                    138:  *  Returns:  Returns EXLST_EXIT to the DosExitList handler    *
                    139:  *                                                             *
                    140: \****************************************************************/
                    141: 
                    142: VOID PASCAL FAR ExitProc(usTermCode)
                    143: USHORT usTermCode;     /* code for the reason for termination */
                    144: {
                    145: 
                    146:     WinDestroyWindow(hwndMainFrame);
                    147: 
                    148:     /*--------------------------------------------------*\
                    149:      *     Any other system resources used             *
                    150:      *     (e.g. memory or files) should be freed here *
                    151:     \*--------------------------------------------------*/
                    152: 
                    153:     WinDestroyMsgQueue(hmq);
                    154: 
                    155:     WinTerminate(hab);
                    156: 
                    157:     DosUnsetExceptionHandler(&ehs);
                    158: 
                    159:     DosExitList(EXLST_EXIT, 0L);    /* termination complete */
                    160: 
                    161:     /* This routine currently doesn't use the usTermCode parameter so *\
                    162:      * it is referenced here to prevent an 'Unreferenced Parameter'  *
                    163:     \* warning at compile time                                       */
                    164: 
                    165:     usTermCode;
                    166: 
                    167: }   /* ExitProc() */
                    168: 
                    169: /****************************************************************\
                    170:  *  Do nothing Guard Page Exception Handler                    *
                    171:  *--------------------------------------------------------------*
                    172:  *                                                             *
                    173:  *  Name:   GPFHandler(PXCPT)                                  *
                    174:  *                                                             *
                    175:  *  Purpose: To notify user when a guard page entered exception *
                    176:  *          occurs                                             *
                    177:  *                                                             *
                    178:  *  Usage:  Routine is called when a guard page is accessed    *
                    179:  *                                                             *
                    180:  *  Method: Whenever a guard page is accessed, a message       *
                    181:  *         box is put on the screen indicating this            *
                    182:  *                                                             *
                    183:  *  Returns:  Returns HANDLED if guard page exception,         *
                    184:  *           otherwise, returns NOT_HANDLED                    *
                    185: \****************************************************************/
                    186: 
                    187: ULONG  GPFHandler(parg)
                    188: PXCPT  parg;
                    189: 
                    190: {
                    191:     ULONG   flMemAttrs;
                    192:     ULONG   ulMemSize;
                    193: 
                    194:     /* Notify the user when they have accessed a guard page, and let
                    195:        the system default guard page handler handle the exception. */
                    196: 
                    197:     if(parg->ExceptionNum == XCPT_GPF)
                    198:     {
                    199:        WinMessageBox(HWND_DESKTOP,
                    200:                        hwndMain,
                    201:                        "Guard Page Accessed.\n\n"
                    202:                        "Calling Default Handler.\n",
                    203:                        szAppName,
                    204:                        13,
                    205:                        MB_OK | MB_ICONASTERISK);
                    206: 
                    207:        WinInvalidateRect(hwndMain,NULL,TRUE);
                    208: 
                    209:        return(NOT_HANDLED);
                    210:     }
                    211: 
                    212:     if(parg->ExceptionNum == XCPT_GP)
                    213:     {
                    214:        if (parg->ExceptionInfo[0] & XCPTF_PF)
                    215:        {
                    216: 
                    217:            ulMemSize = 1L;
                    218:            DosQueryMem((PVOID)(parg->pExceptionContext->EC_cr2),
                    219:                        &ulMemSize,&flMemAttrs);
                    220: 
                    221:            /* If the exception is due to accessing a page that has
                    222:               been allocated but not committed, then commit the page.
                    223:               Otherwise, GP Fault. */
                    224: 
                    225:            if (((flMemAttrs & PAG_FREE) || (flMemAttrs & PAG_COMMIT)) == 0)
                    226:            {
                    227: 
                    228:                WinMessageBox(HWND_DESKTOP,
                    229:                                hwndMain,
                    230:                                "The memory to be written to has not been"
                    231:                                " committed. \n\rAttempting to committ memory"
                    232:                                " before write.",
                    233:                                szAppName,
                    234:                                13,
                    235:                                MB_OK | MB_ICONASTERISK);
                    236: 
                    237:                 if (DosSetMem((PVOID)(parg->pExceptionContext->EC_cr2),
                    238:                           1L,PAG_DEFAULT | PAG_COMMIT) != 0L)
                    239:                    return(NOT_HANDLED);
                    240:                 else
                    241:                    return(HANDLED);
                    242:             }
                    243:        }
                    244: 
                    245:                /* At this point, we have a GP Fault. We cannot
                    246:                   recover, so raise the terminate process exception and
                    247:                   return (GP Fault) exception handled. */
                    248: 
                    249:                WinMessageBox(HWND_DESKTOP,
                    250:                                hwndMain,
                    251:                                "A non-recoverable General Protection Error "
                    252:                                "has occured.\n\rProgram Terminating.",
                    253:                                szAppName,
                    254:                                13,
                    255:                                MB_OK | MB_ICONHAND);
                    256: 
                    257:                return(NOT_HANDLED);
                    258:     }
                    259: 
                    260:     /* The exception is not one that we handle, so pass it on as
                    261:        NOT_HANDLED. */
                    262: 
                    263:     return(NOT_HANDLED);
                    264: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.