Click here to Skip to main content
15,886,795 members
Articles / Desktop Programming / Win32

Win32 Exceptions – OS Level Point of View

Rate me:
Please Sign up or sign in to vote.
4.71/5 (18 votes)
21 May 2010CPOL8 min read 52K   602   36  
Describes Win32 exception layering and discusses Structured Exception Handling (SEH) on OS level and on C++ compiler level.
/***************************************************************************************************************
 *
 * Filename: ExceptionExample2.cpp
 * 
 * Author:   Miki Rozloznik
 *
 * Date:     2010/03/26
 *
 * Implementation Description:
 *//** \file 
 *   The Example 2 for the Win32 Exceptions article.
 *//*
 *
 **************************************************************************************************************/

#include <excpt.h>
#include <windows.h>
#include <tchar.h>

BYTE Dummy = 0xFF;

void ExceptionMaker()
{
    __try
    {
        OutputDebugString(_T("  Trying to write something into bad address...\n"));
        __asm
        {
            mov EAX, 0          // write to EAX zero
            mov [EAX], 0xAA     // write something to zero address => access violation exception (0xC0000005)
        }

        if (Dummy == 0xAA)
            OutputDebugString(_T("  Writing address was successfully corrected.\n"));
    }
    __finally
    {
        OutputDebugString(_T("  ExceptionMaker() __finally block is in progress...\n"));
    }
}

int ExceptionAcceptFilter(struct _EXCEPTION_POINTERS* _ExceptionPointers)
{
    OutputDebugString(_T("    ExceptionAcceptFilter is in progress...\n"));

    // check if we caught access voilation as expected
    if (_ExceptionPointers->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
    {
        // if fault address iz zero, restart the faulting instruction
        if (_ExceptionPointers->ContextRecord->Eax == 0)
        {
            // correct access violation exception => change EAX to points somewhere we can successfully write
            _ExceptionPointers->ContextRecord->Eax = reinterpret_cast<DWORD>(&Dummy);

            // restart the faulting instruction
            return EXCEPTION_CONTINUE_EXECUTION;
        }
       
        // execute the exception handler otherwise
        return EXCEPTION_EXECUTE_HANDLER;
    }

    // this handler declines to handle this exception
    return EXCEPTION_CONTINUE_SEARCH;
}

int _tmain(int argc, _TCHAR* argv[])
{
    OutputDebugString(_T("_tmain is in progress...\n"));
    __try
    {
        __try
        {
            ExceptionMaker();
        }
        __finally
        {
            OutputDebugString(_T("__finally block is in progress...\n"));
        }
    }
    __except (ExceptionAcceptFilter(GetExceptionInformation()))
    {
        OutputDebugString(_T("__except block is in progress...\n"));
    }

    return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Eccam s.r.o.
Czech Republic Czech Republic
Senior Sofware Developer focused on embedded systems.
Company website: http://www.eccam.com

Comments and Discussions