From 61492cf90a42c3a7fc2cf1c143b60761a08f57ce Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 30 Apr 2020 14:13:56 -0700 Subject: [PATCH] Add ExceptionHelpers.ReportUnhandledError (#247) --- WinRT.Runtime/ExceptionHelpers.cs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/WinRT.Runtime/ExceptionHelpers.cs b/WinRT.Runtime/ExceptionHelpers.cs index 8f14fb29f..c7394b9c4 100644 --- a/WinRT.Runtime/ExceptionHelpers.cs +++ b/WinRT.Runtime/ExceptionHelpers.cs @@ -33,6 +33,9 @@ public static class ExceptionHelpers internal delegate int RoOriginateLanguageException(int error, IntPtr message, IntPtr langaugeException); private static RoOriginateLanguageException roOriginateLanguageException; + internal delegate int RoReportUnhandledError(IntPtr pRestrictedErrorInfo); + private static RoReportUnhandledError roReportUnhandledError; + static ExceptionHelpers() { IntPtr winRTErrorModule = Platform.LoadLibraryExW("api-ms-win-core-winrt-error-l1-1-1.dll", IntPtr.Zero, (uint)DllImportSearchPath.System32); @@ -41,6 +44,7 @@ static ExceptionHelpers() getRestrictedErrorInfo = Platform.GetProcAddress(winRTErrorModule); setRestrictedErrorInfo = Platform.GetProcAddress(winRTErrorModule); roOriginateLanguageException = Platform.GetProcAddress(winRTErrorModule); + roReportUnhandledError = Platform.GetProcAddress(winRTErrorModule); } else { @@ -175,7 +179,10 @@ public static unsafe void SetErrorInfo(Exception ex) // as our error so as to propagate the error through WinRT end-to-end. if (ex.TryGetRestrictedLanguageErrorObject(out var restrictedErrorObject)) { - setRestrictedErrorInfo(restrictedErrorObject.GetRef()); + using (restrictedErrorObject) + { + setRestrictedErrorInfo(restrictedErrorObject.ThisPtr); + } } else { @@ -203,6 +210,17 @@ public static unsafe void SetErrorInfo(Exception ex) } } + public static void ReportUnhandledError(Exception ex) + { + SetErrorInfo(ex); + if (getRestrictedErrorInfo != null && roReportUnhandledError != null) + { + Marshal.ThrowExceptionForHR(getRestrictedErrorInfo(out IntPtr ppRestrictedErrorInfo)); + using var restrictedErrorRef = ObjectReference.Attach(ref ppRestrictedErrorInfo); + roReportUnhandledError(restrictedErrorRef.ThisPtr); + } + } + public static int GetHRForException(Exception ex) { int hr = ex.HResult;