How do I set up a signal handler for an access violation error in Windows in C?

I have a bad application that is causing a window access violation. This brings up a "crash dialog" on windows, and I don't want this to be displayed on user computers. On Linux I would set up a signal handler for SIGSEGV and just exit () in the sighandler function, but I'm on Windows and I know almost nothing about the Windows API.

As I understand it, Windows throws an ACCESS_VIOLATION exception when a segfault occurs. I guess this is a regular C ++ exception and can be caught, but the program I have to install is C, not C ++. How do I install a "signal handler" on windows? (assuming the concept of a signal exists, given that signal () and friends is a POSIX API)? Is this API implemented as a core API, or is it part of a POSIX compatibility layer that might be missing when deploying vanilla?

I am working with VS2008 on Win7

+3


source to share


1 answer


This is not 1 "regular C ++ exception". This is an OS trap.

In Linux and other Unix-like systems, OS traps are called "signals".

They are called Structured Exceptions on Windows. You will find a ton of information online about SEH, which stands for Structured Exception Handling. Official documentation at MSDN

The Windows C and C ++ compilers have special keywords for dealing with structured exceptions

__try {
}
__except( MyExceptionFilter(GetExceptionCode(), GetExceptionInfo()) ) {
}

      



Inside the exception filter, you can check the exception code and distinguish between access violations, divide by zero, floating point signals, etc. You can find out where the exception happened and log the stack trace (with some extra work). And you can choose whether the handler code block is executed.

At the bottom, these keywords are translated into code that sets up trap handlers using data structures such as vector tables of exception handlers. But you really want the compiler to help, not itself. Viewing the internal implementation is only of interest to compiler developers or if something goes wrong and you have to debug it.


1 Depending on your compilation options in Visual C ++, the parameters /EHa

and /EHs

, C ++ exceptions can be thrown on top of SEH (there is one specific exception code that means "Microsoft C ++ exception", and a pointer to the C ++ exception object is stored in the SEH parameters). When this happens, the stack merging with C ++ try/catch

and SEH is __try/__except/__finally

unified - both exceptions are unwound through both handler styles. The function is __set_se_translator

also interesting for people using both C ++ exception and SEH in the same program.

+6


source







All Articles