Skip to content

Commit

Permalink
Add Null static field to handles
Browse files Browse the repository at this point in the history
  • Loading branch information
elachlan committed Aug 5, 2022
1 parent 39757ab commit cdee3e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3885,6 +3885,12 @@ private IEnumerable<MemberDeclarationSyntax> CreateCommonTypeDefMembers(Identifi
// If this typedef struct represents a pointer, add an IsNull property.
if (fieldType is IdentifierNameSyntax { Identifier: { Value: nameof(IntPtr) or nameof(UIntPtr) } })
{
// public static readonly IntPtr Null;
yield return FieldDeclaration(VariableDeclaration(structName.WithTrailingTrivia(TriviaList(Space)))
.AddVariables(VariableDeclarator(IdentifierName("Null").Identifier)))
.AddModifiers(TokenWithSpace(SyntaxKind.PublicKeyword), TokenWithSpace(SyntaxKind.StaticKeyword), TokenWithSpace(SyntaxKind.ReadOnlyKeyword)) // operators MUST be public
.WithSemicolonToken(SemicolonWithLineFeed);

// internal static bool IsNull => value == default;
yield return PropertyDeclaration(PredefinedType(TokenWithSpace(SyntaxKind.BoolKeyword)), "IsNull")
.AddModifiers(TokenWithSpace(this.Visibility))
Expand Down
11 changes: 11 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,17 @@ public void HandleStructsHaveIsNullProperty(string handleName)
this.AssertGeneratedMember(handleName, "IsNull", "internal bool IsNull => Value == default;");
}

[Theory]
[InlineData("HANDLE")]
[InlineData("HGDIOBJ")]
[InlineData("HINSTANCE")]
public void HandleStructsHaveStaticNullField(string handleName)
{
// A null HGDIOBJ has a specific meaning beyond just the concept of an invalid handle:
// https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-selectobject#return-value
this.AssertGeneratedMember(handleName, "Null", "Null");
}

[Theory]
[InlineData("HANDLE")]
[InlineData("HGDIOBJ")]
Expand Down

0 comments on commit cdee3e4

Please sign in to comment.