Article ID: 173652
Article Last Modified on 11/21/2006
void main (int argc, char **argv)
{
__try
{
// all of code normally inside of main or WinMain here...
}
__except (MyUnFilter (GetExceptionInformation()))
{
OutputDebugString ("executed filter function\n");
}
}
This work around behaves slightly differently from an unhandled exception
filter in that it will only be called for the thread executing the main()
or WinMain() function. If you are debugging a multithreaded application,
then you will need to add a similar __try/__except block inside of each
thread function.
/*
Demonstrates how to use SEH so that unhandled exception filter
can trap unhandled instructions.
*/
#include <windows.h>
#include <stdio.h>
LONG WINAPI MyUnFilter (struct _EXCEPTION_POINTERS *lpExceptionInfo);
void main (void)
{
LPBYTE lpBuff = NULL;
SetUnhandledExceptionFilter (MyUnFilter);
*lpBuff = 54;
}
LONG WINAPI MyUnFilter (struct _EXCEPTION_POINTERS *lpExceptionInfo)
{
static int s_SigCount = 0;
DWORD dwExceptCode = lpExceptionInfo -> ExceptionRecord ->
ExceptionCode;
LONG lResult;
if (dwExceptCode == EXCEPTION_ACCESS_VIOLATION)
{
printf ("access violation caught by MyUnFilter\n");
++s_SigCount;
// test case to keep this from going on forever (since we're not
// fixing the AV)
if (s_SigCount < 5)
lResult = EXCEPTION_CONTINUE_EXECUTION;
else
lResult = EXCEPTION_EXECUTE_HANDLER;
}
else
lResult = EXCEPTION_CONTINUE_SEARCH;
return lResult;
}
Keywords: kbbug kbkernbase kbpending kbexcepthandling KB173652