Skip to content

Commit

Permalink
Offer SafeHandle overloads of methods with HGDIOBJ parameters
Browse files Browse the repository at this point in the history
Fixes #275
  • Loading branch information
AArnott committed Jan 10, 2024
1 parent efd80a7 commit a71ff04
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Microsoft.Windows.CsWin32/MetadataIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ void PopulateNamespace(NamespaceDefinition ns, string? parentNamespace)
}
}
}
else if (this.MetadataName == "Windows.Win32" && typeName == "HGDIOBJ")
{
// This "base type" struct doesn't have an RAIIFree attribute,
// but methods that take an HGDIOBJ parameter are expected to offer SafeHandle friendly overloads.
this.handleTypeReleaseMethod.Add(tdh, "DeleteObject");
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/HandleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ public void HandleTypeDefsUseIntPtrAsFieldType(string handleType)
Assert.Equal(nameof(IntPtr), Assert.IsType<IdentifierNameSyntax>(field.Declaration.Type).Identifier.ValueText);
}

[Fact]
public void SafeHandleOverloadsGeneratedFor_HGDIObj()
{
this.GenerateApi("GetObject");
Assert.Contains(
this.FindGeneratedMethod("GetObject"),
method => method!.ParameterList.Parameters[0].Type is IdentifierNameSyntax { Identifier: { ValueText: "SafeHandle" } });
}

[Fact]
public void ReleaseMethodGeneratedWithHandleStruct()
{
Expand Down

0 comments on commit a71ff04

Please sign in to comment.