C 在windows下的異常處理

2022-05-07 13:54:12 字數 731 閱讀 7439

專案大了**多了以後難免會出些問題導致程式崩潰,為了快速定位崩潰的位址與原因,引入了setunhandledexceptionfilter這個api。

久了後發現這個api有些情況下無法work,異常會被其他異常處理接管。這樣就無法定位到自己的崩潰原因。後面查了下,大致是在新的msvcr.dll中會接管異常處理,它們不關心的部分才會丟給我們,比如說_set_invalid_parameter_handler,_set_purecall_handler這些東西等等。索性直接把setunhandledexceptionfilter掛起接管過來,在呼叫它的地方全都直接返回true。

大致的**如下:

::setunhandledexceptionfilter(ccrashdump::toplevelexceptionfilter);

unsigned char code[16];

int size = 0;

code[size++] = 0x33;

code[size++] = 0xc0;

code[size++] = 0xc2;

code[size++] = 0x04;

code[size++] = 0x00;

dword dw = 0;

dword dwoldflag, dwtempflag;

virtualprotect(setunhandledexceptionfilter, size, page_readwrite, &dwoldflag);

windows 異常處理

0x00 異常處理結構 try except filter expression filter expression為以下值 exception execute handler exception continue search exception continue execution 0x01 文...

Windows系統異常處理

在c 中使用try catch不能捕捉作業系統丟擲的異常,如非法位址訪問 浮點異常等,而使用vc中的擴充套件特性 try except和 try finally可以捕捉這類異常.如 int func int i except exception execute handler return i vo...

異常處理(下)

異常與物件 工程中的異常應用 函式級try語法 小結問題 有時在工程中只關心是否產生 了異常,而不關心具體的異常型別,c 語言可以做到嗎?c 中的 catch語句可以使用 捕獲所有的異常 include include using namespace std int test int i if i ...