public static unsafe T HandleSO <T>(Func <T> action) { var exc = false; var callback = new PVECTORED_EXCEPTION_HANDLER(Handler); var handler = Kernel32.AddVectoredExceptionHandler(IntPtr.Zero, callback); if (handler == IntPtr.Zero) { throw new Win32Exception("AddVectoredExceptionHandler failed"); } var size = 32768; if (!Kernel32.SetThreadStackGuarantee(&size)) { throw new InsufficientExecutionStackException("SetThreadStackGuarantee failed", new Win32Exception()); } var result = default(T); try { result = action(); } catch (SEHException) when((uint)Marshal.GetExceptionCode() == Err) { exc = true; } if (handler != IntPtr.Zero) { Kernel32.RemoveVectoredExceptionHandler(handler); } if (!exc) { return(result); } if (Msvcrt._resetstkoflw() == 0) { throw new InvalidOperationException("_resetstkoflw failed"); } throw new StackOverflowException(); }
public static extern IntPtr AddVectoredExceptionHandler(uint First, PVECTORED_EXCEPTION_HANDLER Handler);
public static extern IntPtr AddVectoredExceptionHandler(IntPtr FirstHandler, PVECTORED_EXCEPTION_HANDLER VectoredHandler);
public static void AddCustomVectoredExceptionHandler(PVECTORED_EXCEPTION_HANDLER handler) { VectoredExceptionHandlers.Add(handler); }