-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Address COM interop case involving legacy wCode usage.
#117596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Address COM interop case involving legacy wCode usage.
#117596
Conversation
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.
There was a problem hiding this 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
COMExceptionwhenMarshal.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
wCodebehavior
src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/ComInterop/ExcepInfo.cs
Show resolved
Hide resolved
src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/ComInterop/ExcepInfo.cs
Outdated
Show resolved
Hide resolved
|
Tagging subscribers to this area: @cston |
|
/bg-a Android timeout |
|
/ba-g Android timeout |
|
/backport release/9.0-staging |
|
/backport to release/9.0-staging |
|
Started backporting to release/9.0-staging: https://github.com/dotnet/runtime/actions/runs/16283173445 |
The
wCodefield in theEXCEPINFOis 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 anExceptionusingMarshal.GetExceptionForHR(), but converting ashortto anint, will rarely result in a negative value which meansnullwill almost always be returned. This change checks if the exception isnulland if so, creates a simpleCOMExceptionwith the error code.Fixes #117568