Skip to content

Conversation

jkoritzinsky
Copy link
Member

@jkoritzinsky jkoritzinsky commented Jul 15, 2025

Add the API proposed in #117438

Update built-in COM to use it.

Update source-generated COM to use it.

Fixes #117438
Fixes #116917

Copy link
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements support for respecting the ISupportErrorInfo interface when converting HRESULT values to exceptions in COM interop scenarios. The changes add a new overload to Marshal.GetExceptionForHR and Marshal.ThrowExceptionForHR that takes an interface ID and COM object pointer to check if the interface supports error information before using the error details.

Key changes include:

  • Adding new public API overloads that check ISupportErrorInfo before using error information
  • Updating both built-in COM and source-generated COM to use the enhanced error handling
  • Removing legacy native code implementations in favor of managed implementations

Reviewed Changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
System.Runtime.InteropServices.cs Adds new public API overloads for GetExceptionForHR and ThrowExceptionForHR with ISupportErrorInfo support
Marshal.cs Implements the core logic for checking ISupportErrorInfo and calling appropriate error handling
StubHelpers.cs Updates COM stub helpers to use new managed implementation instead of native QCall
ManagedHResultExceptionGeneratorResolver.cs Updates source generator to pass interface ID and object pointer to new API
ThrowExceptionForHRTests.cs Adds comprehensive tests for the new ISupportErrorInfo functionality
Multiple native files Removes obsolete native QCall implementations for COM exception handling
Comments suppressed due to low confidence (1)

src/libraries/System.Runtime.InteropServices/tests/System.Runtime.InteropServices.UnitTests/System/Runtime/InteropServices/Marshal/ThrowExceptionForHRTests.cs:114

  • This test assertion checks that an InvalidOperationException is returned when using an ArgumentException's HResult. This seems inconsistent - if the error info contains an InvalidOperationException but we're passing an ArgumentException's HResult, the test should verify that the error info takes precedence over the HResult when ISupportErrorInfo returns S_OK.
                Assert.IsType<InvalidOperationException>(Marshal.GetExceptionForHR(new ArgumentException().HResult, iid, pUnk));

Copy link
Member

@AaronRobinsonMSFT AaronRobinsonMSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Copy link
Member

@jtschuster jtschuster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should leave #116917 open until we have tests for the generator or file a new issue to track adding tests.

@AaronRobinsonMSFT
Copy link
Member

We should leave #116917 open until we have tests for the generator or file a new issue to track adding tests.

This has added testing for the API. I don't think we need testing for the COM source generator in this case. The fact that we use the API should be sufficient.

@AaronRobinsonMSFT
Copy link
Member

We should leave #116917 open until we have tests for the generator or file a new issue to track adding tests.

This has added testing for the API. I don't think we need testing for the COM source generator in this case. The fact that we use the API should be sufficient.

I'm saying this is okay because we already have error related testing. I don't think specifically testing the IErrorInfo behavior in this specific scenario is really needed.

…pServices/Marshal.cs

Co-authored-by: Aaron Robinson <arobins@microsoft.com>
@jkoritzinsky
Copy link
Member Author

/ba-g wasm timeouts for legs that don't run these tests.

@jkoritzinsky jkoritzinsky merged commit 541c68e into dotnet:main Jul 23, 2025
143 of 153 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Aug 23, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants