I am trying to catch Corrected State Exceptions (CES) in my WPF application. I just want to register an error before exiting. My application uses legacy Win32 / COM DLLs, therefore, I need to catch them. My code to catch them is below. (I added HandleProcessCorruptedStateExceptions in several places because it does not work in the handler itself). The fragment that generates the crash is located below the handler. However, I still see the System Error dialog box, and my helpers never fire ... Any help is appreciated
public partial class App : Application { [HandleProcessCorruptedStateExceptions] [SecurityCritical] protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); AppDomain.CurrentDomain.FirstChanceException += new EventHandler<FirstChanceExceptionEventArgs>(CurrentDomain_FirstChanceException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException); } [HandleProcessCorruptedStateExceptions] [SecurityCritical] void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { EatIt(); } [HandleProcessCorruptedStateExceptions] [SecurityCritical] void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { EatIt(); } [HandleProcessCorruptedStateExceptions] [SecurityCritical] void CurrentDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs e) { EatIt(); } private void EatIt() {
Fragment that generates a crash
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { CrashIt(); } unsafe static void CrashIt() { var obj = new byte[1]; var pin = GCHandle.Alloc(obj, GCHandleType.Pinned); byte* p = (byte*)pin.AddrOfPinnedObject(); for (int ix = 0; ix < 256; ++ix) *p-- = 0; GC.Collect(); } }
I changed the startup code to attach the application with a try / catch clause. Still no success. Does anyone really know how to make this stuff work ?. (I am still getting a windows error dialog)
public class EntryPoint {
source share