首页 技术 正文
技术 2022年11月14日
0 收藏 663 点赞 2,589 浏览 5401 个字
-------------------------------------------------------------------------------
1. 利用 windows 的API SetUnhandledExceptionFilter
-------------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>LONG WINAPI UnhandledExceptionFilter2(LPEXCEPTION_POINTERS pep)
{
int code = pep->ExceptionRecord->ExceptionCode;
int flags = pep->ExceptionRecord->ExceptionFlags;
printf("Exception Caught: Code = %i, Flags = %i, Desc = %s\n", code,
flags, GetExceptionDescription(code));
if (flags == EXCEPTION_NONCONTINUABLE)
{
MessageBox(NULL, "Cannot continue; that would only generate another exception!",
"Exception Caught", MB_OK);
return EXCEPTION_EXECUTE_HANDLER;
}
pep->ContextRecord->Eip -= 8;
return EXCEPTION_CONTINUE_EXECUTION;
}int testxcpt()
{
LPTOP_LEVEL_EXCEPTION_FILTER pOldFilter;
char *s = NULL;
int rc = FALSE; pOldFilter = SetUnhandledExceptionFilter(UnhandledExceptionFilter2);
printf("Generate exception? y/n: ");
fflush(stdin);
switch(getchar())
{
case 'y':
case 'Y':
*s = *s;
break;
case 'n':
case 'N':
s = "s";
rc = TRUE;
break;
default:
printf("I said enter y or n!\n");
}
SetUnhandledExceptionFilter(pOldFilter); return rc;
}int main()
{
if (testxcpt())
printf("testxcpt() succeeded\n");
else
printf("testxcpt() failed\n");
return 0;
}
-------------------------------------------------------------------------------
2. "excpt.h" - __try1
-------------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>
#include <excpt.h>char *GetExceptionDescription(LONG code);EXCEPTION_DISPOSITION MyHandler (struct _EXCEPTION_RECORD* er, void* buf, struct _CONTEXT* ctx, void* buf2)
{
printf("ExceptionCode = %08X %s\n"
"ExceptionFlags = %08X\n"
"ContextFlags = %08X\n"
"SegGs = %08X\n"
"SegFs = %08X\n"
"SegEs = %08X\n"
"SegDs = %08X\n"
"Edi = %08X\n"
"Esi = %08X\n"
"Ebx = %08X\n"
"Edx = %08X\n"
"Ecx = %08X\n"
"Eax = %08X\n"
"Ebp = %08X\n"
"Eip = %08X\n"
"SegCs = %08X\n"
"EFlags = %08X\n"
"Esp = %08X\n"
"SegSs = %08X\n",
er->ExceptionCode,
GetExceptionDescription(er->ExceptionCode),
er->ExceptionFlags,
ctx->ContextFlags,
ctx->SegGs,
ctx->SegFs,
ctx->SegEs,
ctx->SegDs,
ctx->Edi,
ctx->Esi,
ctx->Ebx,
ctx->Edx,
ctx->Ecx,
ctx->Eax,
ctx->Ebp,
ctx->Eip,
ctx->SegCs,
ctx->EFlags,
ctx->Esp,
ctx->SegSs
);
return ExceptionNestedException;
}int main(void)
{
__try1(MyHandler)
{
int *p=(int*)0x00001234;
*p=12;
}
__except1;
{
printf("Exception Caught");
}
return 0;
}
-------------------------------------------------------------------------------
3. libseh
-------------------------------------------------------------------------------
LibSEH 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 __try, __except, and __finally compound statements.在 mingw32 中使用最好GetExceptionCode() fix to -> GetExceptionCodeSEH()
GetExceptionInformation() fix to -> GetExceptionInformationSEH()#include <windows.h>
#include <stdio.h>/* The LibSEH header needs to be included */
#include <seh.h>
char *GetExceptionDescription(LONG code);int ExceptionFilter(unsigned int code, unsigned int excToFilter)
{
printf("ExceptionCode = %08X %s\n", code, GetExceptionDescription(code) );
if(code == excToFilter) return EXCEPTION_EXECUTE_HANDLER;
else return EXCEPTION_CONTINUE_SEARCH;
}int main()
{
__seh_try
{
int x = 0;
int y = 4;
y /= x;
}
__seh_except(ExceptionFilter(GetExceptionCodeSEH(), EXCEPTION_INT_DIVIDE_BY_ZERO))
{
printf("Divide by zero exception.\n");
}
__seh_end_except __seh_try
{
int *p=(int*)0x00001234;
*p=12;
}
__seh_except(ExceptionFilter(GetExceptionCodeSEH(), EXCEPTION_ACCESS_VIOLATION))
{
printf("Exception Caught.\n");
}
__seh_end_except return 0;
}Note:
http://www.programmingunlimited.net/siteexec/content.cgi?page=libseh
http://www.programmingunlimited.net/files/libseh-0.0.4.zip
-------------------------------------------------------------------------------
4. exceptions4c
-------------------------------------------------------------------------------
exceptions4c is a tiny, portable framework that brings the power of exceptions to your C applications. It provides a simple set of keywords (macros, actually) which map the semantics of exception handling you're probably already used to: try, catch, finally, throw. You can write try/catch/finally blocks just as if you were coding in Java. This way you will never have to deal again with boring error codes, or check return values every time you call a function.If you are using threads in your program, you must enable the thread-safe version of the library by defining E4C_THREADSAFE at compiler level.The usage of the framework does not vary between single and multithreaded programs. The same semantics apply. The only caveat is that the behavior of signal handling is undefined in a multithreaded program so use this feature with caution.在 mingw32 中, 编译时需要加 -DE4C_THREADSAFE 参数#include <e4c.h>
#include <stdio.h>int main(void)
{
printf("enter main()\n");
int * pointer = NULL;
int i=0;
e4c_context_begin(E4C_TRUE);
try
{
printf("enter try\n");
int oops = *pointer;
}
catch(RuntimeException)
{
const e4c_exception * exception = e4c_get_exception();
printf("Exception Caught.\n");
if(exception)
{
printf( " exception->name %s\n"
" exception->message %s\n"
" exception->file %s\n"
" exception->line %ld\n"
" exception->function %s\n",
exception->name,
exception->message,
exception->file,
exception->line,
exception->function );
}
}
finally
{
printf("finally.\n");
}
e4c_context_end();
return 0;
}Note:
https://github.com/guillermocalvo/exceptions4c
https://github.com/guillermocalvo/exceptions4c/archive/master.zip <- 3.0.6
https://github.com/guillermocalvo/exceptions4c/archive/v3.0.5.tar.gz

libseh-0.0.4__exceptions4c-3.0.6.7z

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,983
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,500
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,344
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,127
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,761
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,838