Skip to content

Commit

Permalink
Add ExceptionHelpers.ReportUnhandledError (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkoritzinsky authored Apr 30, 2020
1 parent 26564b4 commit 61492cf
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion WinRT.Runtime/ExceptionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -41,6 +44,7 @@ static ExceptionHelpers()
getRestrictedErrorInfo = Platform.GetProcAddress<GetRestrictedErrorInfo>(winRTErrorModule);
setRestrictedErrorInfo = Platform.GetProcAddress<SetRestrictedErrorInfo>(winRTErrorModule);
roOriginateLanguageException = Platform.GetProcAddress<RoOriginateLanguageException>(winRTErrorModule);
roReportUnhandledError = Platform.GetProcAddress<RoReportUnhandledError>(winRTErrorModule);
}
else
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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<IUnknownVftbl>.Attach(ref ppRestrictedErrorInfo);
roReportUnhandledError(restrictedErrorRef.ThisPtr);
}
}

public static int GetHRForException(Exception ex)
{
int hr = ex.HResult;
Expand Down

0 comments on commit 61492cf

Please sign in to comment.