Fix NullReferenceException in friendly overloads of COM methods with optional pointer parameters #1109
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Optional parameters cannot be expressed with the
ref
modifier on COM methods in order to avoid a pointer type. Doing so disallows the possibility of passing in null. EvenUnsafe.NullRef<T>()
does not work because the .NET interop layer throwsNullReferenceException
when that's done.Instead, we leave it as a pointer type.
An alternative to this may be to declare the parameter as an array that allows
null
or exactly 1 element. But the API wouldn't describe itself very well, and enforcement wouldn't exist, leading to possible runtime failures.Fixes #1081
AI generated summary
This pull request includes a significant number of changes to various files in the codebase. The most important changes include adding a new enum to categorize different elements being generated in
Generator.cs
, modifying methods inGenerator.Com.cs
to generate appropriate type syntax based on the generating element, and adding a new test method inCOMTests.cs
to verify the declaration of COM methods.Main interface changes:
src/Microsoft.Windows.CsWin32/Generator.cs
: Added an enum calledGeneratingElement
to categorize different elements being generated and modified theGetNamespaceForPossiblyNestedType
method to include an additional parameter for theCreateParameterList
method. [1] [2]src/Microsoft.Windows.CsWin32/Generator.Com.cs
: Introduced changes to add new parameters to various methods for generating appropriate type syntax based on the generating element. [1] [2] [3] [4]test/Microsoft.Windows.CsWin32.Tests/COMTests.cs
: Added a new test method to verify the declaration of COM methods that accept[Optional, In]
parameters.Other important changes:
src/Microsoft.Windows.CsWin32/Generator.Constant.cs
: Modified theCreateConstant
method to include a new parameterforElement
and modified theToTypeSyntax
method to include a new parameterGeneratingElement.Constant
. [1] [2]src/Microsoft.Windows.CsWin32/Generator.Extern.cs
: Added a new parameterforElement
to theCreateParameterList
method and theToTypeSyntax
method. [1] [2] [3]src/Microsoft.Windows.CsWin32/Generator.FriendlyOverloads.cs
: Modified theCreateConstant
method to add a new parameterforElement
to theToTypeSyntax
method.src/Microsoft.Windows.CsWin32/Generator.Handle.cs
: Added a new parameter to theToTypeSyntax
method and modified the code to pass a specific value based on the generating element. [1] [2] [3]Please note that due to the large number of changes and limited space, only a subset of the changes are listed here.