diff --git a/src/Microsoft.Windows.CsWin32/MetadataIndex.cs b/src/Microsoft.Windows.CsWin32/MetadataIndex.cs index 0c672f6d..9d03f6b7 100644 --- a/src/Microsoft.Windows.CsWin32/MetadataIndex.cs +++ b/src/Microsoft.Windows.CsWin32/MetadataIndex.cs @@ -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"); + } } } } diff --git a/test/Microsoft.Windows.CsWin32.Tests/HandleTests.cs b/test/Microsoft.Windows.CsWin32.Tests/HandleTests.cs index cba5875e..8c020e5d 100644 --- a/test/Microsoft.Windows.CsWin32.Tests/HandleTests.cs +++ b/test/Microsoft.Windows.CsWin32.Tests/HandleTests.cs @@ -128,6 +128,15 @@ public void HandleTypeDefsUseIntPtrAsFieldType(string handleType) Assert.Equal(nameof(IntPtr), Assert.IsType(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() {