From 919122a11cdac3862281bc9ae16092d74f019bd9 Mon Sep 17 00:00:00 2001 From: Jan Vorlicek Date: Wed, 16 Jul 2025 01:25:35 +0200 Subject: [PATCH 1/2] Fix NativeAOT COM custom query interface exceptioon handling After fixing a bug in the related stuff in coreclr, the regression test has revealed that the NativeAOT version has a similar issue. The exception stemming from the custom query interface are not caught and transformed to HRESULT, so they are reported as unhandled exceptions. This change adds try / catch to catch the exception and translate it to HRESULT. --- .../Runtime/InteropServices/ComWrappers.cs | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs index c607bbb05d5413..db35a60aac0033 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs @@ -314,15 +314,22 @@ public int QueryInterface(in Guid riid, out IntPtr ppvObject) } else { - Guid riidLocal = riid; - switch (customQueryInterface.GetInterface(ref riidLocal, out ppvObject)) + try { - case CustomQueryInterfaceResult.Handled: - return HResults.S_OK; - case CustomQueryInterfaceResult.NotHandled: - break; - case CustomQueryInterfaceResult.Failed: - return HResults.COR_E_INVALIDCAST; + Guid riidLocal = riid; + switch (customQueryInterface.GetInterface(ref riidLocal, out ppvObject)) + { + case CustomQueryInterfaceResult.Handled: + return HResults.S_OK; + case CustomQueryInterfaceResult.NotHandled: + break; + case CustomQueryInterfaceResult.Failed: + return HResults.COR_E_INVALIDCAST; + } + } + catch (Exception ex) + { + return ex.HResult; } } } From 9c09c9cf5023e3adc5a792e36bba13e9abe7720c Mon Sep 17 00:00:00 2001 From: Jan Vorlicek Date: Wed, 16 Jul 2025 01:33:53 +0200 Subject: [PATCH 2/2] PR feedback --- .../src/System/Runtime/InteropServices/ComWrappers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs index db35a60aac0033..9df46043178eda 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs @@ -329,7 +329,7 @@ public int QueryInterface(in Guid riid, out IntPtr ppvObject) } catch (Exception ex) { - return ex.HResult; + return Marshal.GetHRForException(ex); } } }