
Note: This is possibly alpha grade code. But then again, it might work very well. It seems to work in my limited testing so far.
NewsAs of 6-28-2010, the latest version is v0.0.3, which fixes several issues.
About LibSEHLibSEH is a compatibility layer that allows one to utilize the Structured
Exception Handling facility found in Windows within GNU C/C++ for Windows (MINGW32, CYGWIN).
In other compilers, SEH is built into the compiler as a language extension. In other
words, this syntax is not standard C or C++, where standard in this case includes any
ANSI standard. Usually, support for this feature is implemented through
#include <windows.h>
#include <stdio.h>
int ExceptionFilter(unsigned int code, unsigned int excToFilter)
{
if(code == excToFilter) return EXCEPTION_EXECUTE_HANDLER;
else return EXCEPTION_CONTINUE_SEARCH;
}
int main()
{
int x = 0;
int y = 4;
__try
{
y /= x;
}
__except(ExceptionFilter(GetExceptionCode(), EXCEPTION_INT_DIVIDE_BY_ZERO))
{
printf("Divide by zero exception.\n");
}
return 0;
}
This is only supported in Microsoft C/C++ and Digital Mars C/C++. They are not standard language constructs.
LibSEH allows programs intended for GNU C/C++ to utilize this feature, with a high degree of source-level compatibility and minimal existing code changes. To modify the above program in order to compile it on GNU C/C++ with LibSEH, two extra lines must be added:
#include <windows.h>
#include <stdio.h>
#include <seh.h> /***** LINE 1 of additional code *****/
int ExceptionFilter(unsigned int code, unsigned int excToFilter)
{
if(code == excToFilter) return EXCEPTION_EXECUTE_HANDLER;
else return EXCEPTION_CONTINUE_SEARCH;
}
int main()
{
int x = 0;
int y = 4;
__try
{
y /= x;
}
__except(ExceptionFilter(GetExceptionCode(), EXCEPTION_INT_DIVIDE_BY_ZERO))
{
printf("Divide by zero exception.\n");
}
__end_except /***** LINE 2 of additional code *****/
return 0;
}
Not very much to it, really. The seh.h header includes other headers, and when preprocessed
under Microsoft C/C++ and Digital Mars C/C++ (untested), the
All of this allows the same source to be compiled on GNU C/C++ and Microsoft C/C++ (and most likely, Digital Mars C/C++).
The library also supports the
int main()
{
int x = 0;
int y = 4;
__try
{
__try {
y /= x;
}
__finally {
printf("Leaving __try/__finally compound statement.\n");
}
__end_finally /****** NOTE THE __end_finally, also required ******/
}
__except(ExceptionFilter(GetExceptionCode(), EXCEPTION_INT_DIVIDE_BY_ZERO))
{
printf("Divide by zero exception.\n");
}
__end_except
return 0;
}
The
Yes, it's true. LibSEH does not have the exact same behavior as builtin SEH in Microsoft C/C++. Here are some of the currently known differences (and I could be incorrect, as I haven't tested all of these):
These differences only affect when LibSEH is really being used, like when using GNU C/C++.
The order of execution of filter expressions and
__try {
__try {
*(int*)0 = 4;
}
__finally {
/* Do something */
}
__end_finally
}
__except(filter_expr) {
/* Do something */
}
__end_except
The
A note concerning C++: LibSEH does not unwind the stack in the same way that the C++ runtime library does, and as a consequence, destructors are not called when objects go out of scope from stack unwinding when dealing with an exception. This can lead to resource leaks if one isn't careful. The library does include a C++ interface (which translates SEH (system) exceptions into C++ exceptions), however.
Changesv0.0.3 fixes some GCC 3.x compatibility issues.
Versions v0.0.2 and up are compatible with GCC 4.x, whereas v0.0.1 is not. v0.0.2 also fixes some issues that apply to optimized builds.
When Problems OccurIf a problem occurs with a program that uses libseh in terms of SEH (exceptions not being caught that occur, stack variables mysteriously changing in subroutines that use __try/__except, etc), it is suggested that one compile the application without optimizations. Bug reports are always welcome.
Download (alpha)libseh-0.0.3.zip (Source code)
libseh-0.0.2.zip (Source code)
libseh-0.0.1.zip (Source code)