home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 January / PCWorld_2000-01_cd.bin / Software / Servis / Devc / _SETUP.5 / Group18 / sehfix.c < prev    next >
C/C++ Source or Header  |  1997-10-21  |  1KB  |  61 lines

  1. /*
  2.  * sehfix.c
  3.  *
  4.  * A test program involving an exception handler that fixes the exception
  5.  * causing condition.
  6.  *
  7.  * In this code we install an exception handler my_handler and then a piece
  8.  * of inline assembly attempts to write at the address marked in eax, after
  9.  * setting eax to 10. This should produce an exception. The handler then
  10.  * changes the eax register of the exception context to be the address of
  11.  * a static variable and restarts the code. This should allow everything
  12.  * to continue.
  13.  */
  14.  
  15. #include <windows.h>
  16. #include <excpt.h>
  17.  
  18. #include "exutil.h"
  19.  
  20. int    x;
  21.  
  22. EXCEPTION_DISPOSITION
  23. my_handler (
  24.     struct _EXCEPTION_RECORD* pExceptionRec,
  25.     void* pEstablisherFrame,
  26.     struct _CONTEXT* pContextRecord,
  27.     void* pDispatcherContext
  28.     )
  29. {
  30.     printf ("In my exception handler!\n");
  31.     DumpExceptionRecord (pExceptionRec);
  32.     pContextRecord->Eax = (DWORD) &x;
  33.     return ExceptionContinueExecution;
  34. }
  35.  
  36. main ()
  37. {
  38.     x = 2;
  39.  
  40.     printf ("x = %d\n", x);
  41.  
  42.     WalkExceptionHandlers();
  43.  
  44.     __try1(my_handler)
  45.  
  46.     WalkExceptionHandlers();
  47.  
  48.     /* This assembly code should produce an exception. */
  49.     __asm__("movl $10,%%eax;movl $1,(%%eax);" : : : "%eax");
  50.  
  51.     __except1
  52.  
  53.     WalkExceptionHandlers();
  54.  
  55.     printf ("x = %d\n", x);
  56.  
  57.     printf ("Finished!\n");
  58. }
  59.  
  60.  
  61.