home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1992 / 14 / badapp.c < prev    next >
C/C++ Source or Header  |  1992-05-13  |  9KB  |  263 lines

  1. // BADAPP - Application Error Testbed for Windows 3.1
  2. // Copyright (C) 1992 Ray Duncan
  3. // Ziff Davis Publishing * PC Magazine
  4.  
  5. #define dim(x) (sizeof(x) / sizeof(x[0]))   // returns no. of elements
  6.  
  7. #include "windows.h"
  8. #include "badapp.h"
  9.  
  10. HANDLE hInst;                               // module instance handle
  11. HWND hFrame;                                // handle for frame window
  12.  
  13. char szShortAppName[] = "BadApp";              // short application name
  14. char szAppName[] = "BadApp - UAE Tester";     // long application name
  15. char szMenuName[] = "BadAppMenu";              // name of menu resource
  16.  
  17. struct decodeWord {                         // structure associates
  18.     UINT Code;                              // messages or menu IDs
  19.     LONG (*Fxn)(HWND, UINT, UINT, LONG); }; // with a function
  20.  
  21. //
  22. // Table of window messages supported by FrameWndProc()
  23. // and the functions which correspond to each message.
  24. //
  25. struct decodeWord messages[] = {
  26.     WM_PAINT, DoPaint,
  27.     WM_COMMAND, DoCommand,
  28.     WM_DESTROY, DoDestroy, } ;
  29.  
  30. //
  31. // Table of menubar item IDs and their corresponding functions.
  32. //
  33. struct decodeWord menuitems[] = {
  34.     IDM_EXIT, DoMenuExit,
  35.     IDM_GPFAULT, DoMenuGPFault,
  36.     IDM_HANG, DoMenuHang,
  37.     IDM_ZERODIV, DoMenuZeroDiv,
  38.     IDM_BADHDC, DoMenuBadHDC,
  39.     IDM_BADPTR, DoMenuBadPtr, } ;
  40.  
  41. //
  42. // WinMain -- entry point for this application from Windows.
  43. //
  44. INT APIENTRY WinMain(HANDLE hInstance,
  45.     HANDLE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
  46. {
  47.     MSG msg;
  48.  
  49.     hInst = hInstance;                      // save this instance handle
  50.  
  51.     if(!hPrevInstance)                      // if first instance,
  52.         if(!InitApplication(hInstance))     // register window class
  53.             return(FALSE);                  // exit if couldn't register
  54.  
  55.     if(!InitInstance(hInstance, nCmdShow))  // create this instance's window
  56.         return(FALSE);                      // exit if create failed
  57.  
  58.     while(GetMessage(&msg, NULL, 0, 0))     // while message != WM_QUIT
  59.     {
  60.         TranslateMessage(&msg);             // translate virtual key codes
  61.         DispatchMessage(&msg);              // dispatch message to window
  62.     }
  63.  
  64.     TermInstance(hInstance);                // clean up for this instance
  65.     return(msg.wParam);                     // return code = WM_QUIT value
  66. }
  67.  
  68. //
  69. // InitApplication --- global initialization code for this application.
  70. //
  71. BOOL InitApplication(HANDLE hInstance)
  72. {
  73.     WNDCLASS  wc;
  74.  
  75.     // set parameters for frame window class
  76.     wc.style = CS_HREDRAW|CS_VREDRAW;       // class style
  77.     wc.lpfnWndProc = FrameWndProc;          // class callback function
  78.     wc.cbClsExtra = 0;                      // extra per-class data
  79.     wc.cbWndExtra = 0;                      // extra per-window data
  80.     wc.hInstance = hInstance;               // handle of class owner
  81.     wc.hIcon = LoadIcon(hInst, "BadAppIcon");        // skull icon
  82.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);       // default cursor
  83.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); // background color 
  84.     wc.lpszMenuName =  szMenuName;          // name of menu resource
  85.     wc.lpszClassName = szShortAppName;      // name of window class
  86.  
  87.     return(RegisterClass(&wc));             // register class, return status
  88. }
  89.  
  90. //
  91. // InitInstance --- instance initialization code for this application.
  92. //
  93. BOOL InitInstance(HANDLE hInstance, int nCmdShow)
  94. {
  95.     hFrame = CreateWindow(                  // create frame window
  96.         szShortAppName,                     // window class name
  97.         szAppName,                          // text for title bar
  98.         WS_OVERLAPPEDWINDOW,                // window style
  99.         CW_USEDEFAULT, CW_USEDEFAULT,       // default position
  100.         CW_USEDEFAULT, CW_USEDEFAULT,       // default size
  101.         NULL,                               // no parent window
  102.         NULL,                               // use class default menu
  103.         hInstance,                          // window owner
  104.         NULL);                              // unused pointer
  105.  
  106.     if(!hFrame) return(FALSE);              // error, can't create window
  107.  
  108.     ShowWindow(hFrame, nCmdShow);           // make frame window visible
  109.     UpdateWindow(hFrame);                   // force WM_PAINT message
  110.     return(TRUE);                           // return success flag
  111. }
  112.  
  113. //
  114. // TermInstance -- instance termination code for this application.
  115. //
  116. BOOL TermInstance(HANDLE hinstance)
  117. {
  118.     return(TRUE);                           // return success flag
  119. }
  120.  
  121. //
  122. // FrameWndProc --- callback function for application frame window.
  123. //
  124. LONG FAR APIENTRY FrameWndProc(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  125. {
  126.     int i;                                  // scratch variable
  127.  
  128.     for(i = 0; i < dim(messages); i++)      // decode window message and
  129.     {                                       // run corresponding function
  130.         if(wMsg == messages[i].Code)
  131.             return((*messages[i].Fxn)(hWnd, wMsg, wParam, lParam));
  132.     }
  133.  
  134.     return(DefWindowProc(hWnd, wMsg, wParam, lParam));
  135. }
  136.  
  137. //
  138. // DoCommand -- process WM_COMMAND message for frame window by
  139. // decoding the menubar item with the menuitems[] array, then
  140. // running the corresponding function to process the command.
  141. // 
  142. LONG DoCommand(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  143. {
  144.     int i;                                  // scratch variable
  145.  
  146.     for(i = 0; i < dim(menuitems); i++)     // decode menu command and
  147.     {                                       // run corresponding function
  148.         if(wParam == menuitems[i].Code)
  149.             return((*menuitems[i].Fxn)(hWnd, wMsg, wParam, lParam));
  150.     }
  151.  
  152.     return(DefWindowProc(hWnd, wMsg, wParam, lParam));
  153. }
  154.  
  155. //
  156. // DoDestroy -- process WM_DESTROY message for frame window.
  157. // 
  158. LONG DoDestroy(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  159. {
  160.     PostQuitMessage(0);                     // force WM_QUIT message to
  161.     return(FALSE);                            // terminate the event loop
  162. }
  163.  
  164. //
  165. // DoPaint -- process WM_PAINT message for frame window.  Select
  166. // a pretty font, then display a message in the center of the window
  167. // to show that the program is alive.
  168. // 
  169. LONG DoPaint(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  170. {
  171.     HDC hdc;
  172.     PAINTSTRUCT ps;
  173.     RECT rect;
  174.     HFONT hfont;
  175.  
  176.     hdc = BeginPaint(hWnd, &ps);            // get device context
  177.     GetClientRect(hWnd, &rect);                // get client area dimensions
  178.     hfont = CreateFont(-36, 0, 0, 0, 700, TRUE, 0, 0, ANSI_CHARSET,
  179.         OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  180.         DEFAULT_QUALITY, (FF_MODERN << 4) + DEFAULT_PITCH,
  181.         "Ariel");
  182.     SelectObject(hdc, hfont);                // select new font
  183.     DrawText(hdc, "I'm Really Bad!", -1,     // paint text in window
  184.         &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);    
  185.     EndPaint(hWnd, &ps);                    // release device context
  186.     return(FALSE);                              
  187. }
  188.  
  189. //
  190. // DoMenuExit -- process File-Exit command from menu bar.
  191. // 
  192. LONG DoMenuExit(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  193. {
  194.     SendMessage (hWnd, WM_CLOSE, 0, 0L);    // send window close message    
  195.     return(FALSE);                            // to shut down the app
  196. }
  197.  
  198. //
  199. // DoMenuGPFault -- process GP Fault command from menu bar.  Force
  200. // a fault by dereferencing a null pointer.
  201. // 
  202. LONG DoMenuGPFault(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  203. {
  204.     char c;
  205.     char far *p = NULL;
  206.  
  207.     c = *p;                                    // dereference bogus pointer
  208.     return(FALSE);                            // to force a GP fault
  209. }
  210.  
  211. //
  212. // DoMenuHang -- process Infinite Loop command from menu bar.  
  213. // Execute an infinite loop until the user forces a local reboot
  214. // with Ctl-Alt-Del.
  215. // 
  216. LONG DoMenuHang(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  217. {
  218.     while(TRUE)
  219.     {
  220.         // execute practically forever 
  221.     }
  222.  
  223.     return(FALSE);
  224. }
  225.  
  226. //
  227. // DoMenuZeroDiv -- process Divide by Zero command from menu bar.
  228. // Declare some integers, then force a divide by zero fault.
  229. // 
  230. LONG DoMenuZeroDiv(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  231. {
  232.     int i, j, k = 0;
  233.  
  234.     i = j / k;
  235.     return(FALSE);
  236. }
  237.  
  238. //
  239. // DoMenuBadHDC -- process Bad HDC command from menu bar.  Call the
  240. // DrawText() API function with a bogus HDC.
  241. // 
  242. LONG DoMenuBadHDC(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  243. {
  244.     HDC hdc = 0;
  245.     RECT rect;
  246.  
  247.     GetClientRect(hWnd, &rect);                // get client area dimensions
  248.     DrawText(hdc, "Bogus HDC!", -1,         // use invalid HDC to paint text
  249.         &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);    
  250.     return(FALSE);                              
  251. }
  252.  
  253. //
  254. // DoMenuBadPtr -- process Bad Pointer command from menu bar.  Call the
  255. // MessageBox() API function with a NULL pointer to the message string.
  256. // 
  257. LONG DoMenuBadPtr(HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
  258. {
  259.     MessageBox(hWnd, NULL, NULL, MB_OK | MB_ICONSTOP);
  260.     return(FALSE);                            
  261. }
  262.  
  263.