WPF Corrupted Exceptions. How to implement HandleProcessCorruptedStateExceptions

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() { // Add some kind of logging then terminate... } } 

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 { // All WPF applications should execute on a single-threaded apartment (STA) thread [STAThread] [HandleProcessCorruptedStateExceptions] [SecurityCritical] public static void Main() { CustomApplication app = new CustomApplication(); try { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); app.Run(); } catch (Exception) { System.Diagnostics.Debug.WriteLine("xx"); } } [HandleProcessCorruptedStateExceptions] [SecurityCritical] static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { System.Diagnostics.Debug.WriteLine("xx"); } } public class CustomApplication : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); MainWindow window = new MainWindow(); window.Show(); } } 
+4
source share
1 answer

After reading the damaged status exceptions section, it seems to me that the method that try..catch actually executes should use the attribute, you register handlers for some events, so your attributes are not affected.

Reading a little more seems that the behavior has changed between .NET 3.5 and 4.0, so you can try to do something as written in this article

What might work is to write the entry point yourself and start the WPF application using Initialize / Run, then mark the entry point method and write try / catch during the run.

+2
source

Source: https://habr.com/ru/post/1344309/


All Articles