본문 바로가기
프로그래밍/C·C++

VEH (Vectored Exception Handling)

by ITPro 2011. 8. 15.

참조1 : http://msdn.microsoft.com/en-us/library/ms681420(v=VS.85).aspx
참조2 : http://blogs.msdn.com/b/zhanli/archive/2010/06/25/c-tips-addvectoredexceptionhandler-addvectoredcontinuehandler-and-setunhandledexceptionfilter.aspx

VEH는 SEH의 확장된 개념으로써, 프레임 기반이 아니기 때문에 어떠한 곳에서든지 핸들러를 추가할 수 있다.

1.SetUnhandledExceptionFilter
LPTOP_LEVEL_EXCEPTION_FILTER WINAPI SetUnhandledExceptionFilter( __in LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter);

최상위 예외 필터를 설정한다. 하위 예외 필터에서 처리되지 않은 예외는 이 함수에서 지정한 예외 필터에서 처리한다.

2.AddVectoredExceptionHandler 
PVOID WINAPI AddVectoredExceptionHandler( __in ULONG First Handler, __in PVECTORED_EXCEPTION_HANDLER VectoredHandler);
Vectored Exception Handler를 설정한다. 첫번째 인자가 0으로 설정되면 예외 핸들러가 가장 나중에 처리가 되고 0이 아닌 값으로 설정되면 가장 먼저 처리가 된다.

3.AddVectoredContinueHandler
PVOID WINAPI AddVectoredContinueHandler( __in ULONG First Handler, __in PVECTORED_EXCEPTION_HANDLER VectoredHandler);
아Vectored Continue Handler를 설정한다. 인자는 AddVectoredExceptionHandler와 동일하다. 


4.예제
4-1.예제 코드 (참조2)
#include "stdafx.h"

LONG WINAPI MyVectorContinueHandler(PEXCEPTION_POINTERS p)
{
    printf("in my vectored continue handler\r\n");
    return EXCEPTION_CONTINUE_SEARCH;
}

LONG WINAPI MyVectorExceptionFilter(PEXCEPTION_POINTERS p)
{
    printf("in my vectored exception filter\r\n");
    return EXCEPTION_CONTINUE_SEARCH;
}

LONG WINAPI MyUnhandledExceptionFilter(PEXCEPTION_POINTERS p)
{
    printf("in my unhandled exception filter\r\n");
    return EXCEPTION_CONTINUE_SEARCH;
}

LONG MyExceptFilter()
{
    printf("in my filter\r\n");
    return EXCEPTION_CONTINUE_SEARCH;
}

int _tmain(int argc, _TCHAR* argv[])
{
    LPTOP_LEVEL_EXCEPTION_FILTER pOriginalFilter = 
        SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
    AddVectoredExceptionHandler(1,MyVectorExceptionFilter);
    AddVectoredContinueHandler(1,MyVectorContinueHandler);

   __try
   {
        //trigger an access violation here
        int* p;
        p = 0;
        *p = 10;
    }
    __except(MyExceptFilter())
    {
        printf("in my handler\r\n");
        printf("exception code: 0x%x\r\n",GetExceptionCode());
    } 
}
 
4-2.실행결과
in my vectored exception filter
in my filter
in my unhandled exception filter
in my vectored continue handler 
 
반응형

'프로그래밍 > C·C++' 카테고리의 다른 글

SEH(Structured Exception Handling)  (0) 2011.08.15
strrstr 함수 구현 (문자열 역순 탐색 함수)  (0) 2011.07.20
프로세스 리스트 얻기  (0) 2011.07.04
프로세스 제거하기  (0) 2011.07.04
프로세스 생성하기  (0) 2011.07.04