Skip to content

Conversation

AaronRobinsonMSFT
Copy link
Member

@AaronRobinsonMSFT AaronRobinsonMSFT commented Jul 14, 2025

The wCode field in the EXCEPINFO is a 16-bit signed integer. This is a legacy field for 16-bit Windows, but is still used in many COM servers. The current implementation assumed it would result in an Exception using Marshal.GetExceptionForHR(), but converting a short to an int, will rarely result in a negative value which means null will almost always be returned. This change checks if the exception is null and if so, creates a simple COMException with the error code.

Fixes #117568

The wCode field in the EXCEPINFO is a 16-bit signed integer. This
is a legacy field for 16-bit Windows, but is still used in many COM
servers. The current implementation assumed it would result in an
Exception using Marshal.GetExceptionForHR(), but converting a
short to an int, will rarely result in a negative value which means
null will almost always be returned. This change checks if the
exception is null and if so, creates a simple COMException with the
error code.
@AaronRobinsonMSFT AaronRobinsonMSFT added this to the 10.0.0 milestone Jul 14, 2025
@Copilot Copilot AI review requested due to automatic review settings July 14, 2025 05:34
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 fixes a bug in COM interop exception handling where legacy 16-bit wCode values were not properly converted to exceptions. The issue occurred because converting a short to an int rarely produces negative values, causing Marshal.GetExceptionForHR() to return null instead of a proper exception.

  • Adds null-checking logic to create a fallback COMException when Marshal.GetExceptionForHR() returns null
  • Updates method signatures and null checks to use modern C# nullable reference types and patterns
  • Improves code documentation with detailed comments explaining the legacy wCode behavior

Copy link
Contributor

Tagging subscribers to this area: @cston
See info in area-owners.md if you want to be subscribed.

@AaronRobinsonMSFT
Copy link
Member Author

/bg-a Android timeout

@AaronRobinsonMSFT
Copy link
Member Author

/ba-g Android timeout

@AaronRobinsonMSFT AaronRobinsonMSFT merged commit 74be414 into dotnet:main Jul 15, 2025
111 of 115 checks passed
@AaronRobinsonMSFT AaronRobinsonMSFT deleted the runtime117568 branch July 15, 2025 03:15
@AaronRobinsonMSFT
Copy link
Member Author

/backport release/9.0-staging

@AaronRobinsonMSFT
Copy link
Member Author

/backport to release/9.0-staging

Copy link
Contributor

Started backporting to release/9.0-staging: https://github.com/dotnet/runtime/actions/runs/16283173445

@github-actions github-actions bot locked and limited conversation to collaborators Aug 14, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Title: NullReferenceException in ComInterop.ExcepInfo when GetExceptionForHR returns null
3 participants