Posted on 2008-11-17 13:54
小明 閱讀(3285)
評論(0) 編輯 收藏 引用 所屬分類:
Win32 、
Debug
天下沒有不crash的程序。
Crash作為一個客觀存在的現實,每個程序員都應該意識到這個問題,才能最大限度的降低crash所帶來的危害。
讓我們先來看看幾種死亡對話框

Figure-1 Crash MessageBox in Windows 2000

Figure-2 Crash MessageBox in Windows XP
讓我們看看哪些因素影響程序Crash的時候的行為
1 . Error Mode--cut from microsoft--
Each process has an associated error mode that indicates to the system how the application is going to respond to serious errors. Serious errors include disk failure, drive-not-ready errors, data misalignment, and unhandled exceptions. An application can let the system display a message box informing the user that an error has occurred, or it can handle the errors. To handle these errors without user intervention, use the SetErrorMode function. After calling SetErrorMode and specifying appropriate flags, the system will not display the corresponding error message boxes.
--cut from microsoft--
也就是說我們可以調用來
SetErrorMode(SEM_NOGPFAULTERRORBOX)來屏蔽掉"Death MessageBox"的出現。
具體的API使用參考
http://msdn.microsoft.com/en-us/library/ms680621(VS.85).aspx另外值得一提的一點是,父子進程默認使用相同的ErrorMode,但是你可以在CreateProcess通過指定CREATE_DEFAULT_ERROR_MODE來disable.
2.注冊表中的HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug
有兩個key,第一個是
auto,指定程序crash的時候要不要自動的load debugger
如果為1,就不會顯示"Death MessageBox"而直接調用debugger了
另外一個是
debugger,指定默認的debugger.

最后上傳一個測試程序來體會一下
Click here to download TestCrash