Skip to content

Commit

Permalink
Give HWND an IntPtr field type
Browse files Browse the repository at this point in the history
Fixes #312
  • Loading branch information
AArnott committed Jun 16, 2022
1 parent a79abbd commit 4d6386c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3763,7 +3763,7 @@ private ClassDeclarationSyntax DeclareCocreatableClass(TypeDefinition typeDef)
private StructDeclarationSyntax DeclareTypeDefStruct(TypeDefinition typeDef, TypeDefinitionHandle typeDefHandle)
{
IdentifierNameSyntax name = IdentifierName(this.Reader.GetString(typeDef.Name));
bool isHandle = name.Identifier.ValueText == "HGDIOBJ";
bool isHandle = name.Identifier.ValueText is "HGDIOBJ" or "HWND";
foreach (CustomAttributeHandle attHandle in typeDef.GetCustomAttributes())
{
CustomAttribute att = this.Reader.GetCustomAttribute(attHandle);
Expand Down
15 changes: 15 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,21 @@ public void HandleStructsHaveIsNullProperty(string handleName)
this.AssertGeneratedMember(handleName, "IsNull", "internal bool IsNull => Value == default;");
}

[Theory]
[InlineData("HANDLE")]
[InlineData("HGDIOBJ")]
[InlineData("HWND")]
public void HandleTypeDefsUseIntPtrAsFieldType(string handleType)
{
this.generator = this.CreateGenerator();
Assert.True(this.generator.TryGenerate(handleType, CancellationToken.None));
this.CollectGeneratedCode(this.generator);
this.AssertNoDiagnostics();
StructDeclarationSyntax hwnd = Assert.IsType<StructDeclarationSyntax>(this.FindGeneratedType(handleType).Single());
FieldDeclarationSyntax field = hwnd.Members.OfType<FieldDeclarationSyntax>().Single();
Assert.Equal(nameof(IntPtr), Assert.IsType<IdentifierNameSyntax>(field.Declaration.Type).Identifier.ValueText);
}

[Fact]
public void NamespaceHandleGetsNoSafeHandle()
{
Expand Down

0 comments on commit 4d6386c

Please sign in to comment.