From 3f5735062e22b5f9ce5316b952072e882d3b2c70 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Thu, 1 Sep 2022 09:35:09 -0600 Subject: [PATCH] Upgrade metadata to 31.0.4-preview --- Directory.Build.props | 2 +- src/Microsoft.Windows.CsWin32/Generator.cs | 10 ++++++++-- test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs | 1 + 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index e871bbf3..de17866f 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -36,7 +36,7 @@ true snupkg - 29.0.6-preview + 31.0.4-preview 0.1.7-alpha diff --git a/src/Microsoft.Windows.CsWin32/Generator.cs b/src/Microsoft.Windows.CsWin32/Generator.cs index fc6d16b6..3a2b773d 100644 --- a/src/Microsoft.Windows.CsWin32/Generator.cs +++ b/src/Microsoft.Windows.CsWin32/Generator.cs @@ -1740,7 +1740,7 @@ internal void RequestConstant(FieldDefinitionHandle fieldDefHandle) } } - internal bool TryGetTypeDefFieldType(TypeHandleInfo typeDef, [NotNullWhen(true)] out TypeHandleInfo? fieldType) + internal bool TryGetTypeDefFieldType(TypeHandleInfo? typeDef, [NotNullWhen(true)] out TypeHandleInfo? fieldType) { if (typeDef is HandleTypeHandleInfo handle) { @@ -3816,6 +3816,11 @@ private bool IsHandle(TypeDefinitionHandle typeDefHandle, out string? releaseMet || this.Reader.StringComparer.Equals(typeDef.Name, "HWND"); } + private bool IsSafeHandleCompatibleTypeDef(TypeHandleInfo? typeDef) + { + return this.TryGetTypeDefFieldType(typeDef, out TypeHandleInfo? fieldType) && this.IsSafeHandleCompatibleTypeDefFieldType(fieldType); + } + private bool IsSafeHandleCompatibleTypeDefFieldType(TypeHandleInfo? fieldType) { return fieldType is PointerTypeHandleInfo @@ -4382,7 +4387,8 @@ private IEnumerable DeclareFriendlyOverloads(MethodDefi Argument(LiteralExpression(doNotRelease ? SyntaxKind.FalseLiteralExpression : SyntaxKind.TrueLiteralExpression)).WithNameColon(NameColon(IdentifierName("ownsHandle"))))))); } } - else if (this.options.UseSafeHandles && isIn && !isOut && !isReleaseMethod && parameterTypeInfo is HandleTypeHandleInfo parameterHandleTypeInfo && this.TryGetHandleReleaseMethod(parameterHandleTypeInfo.Handle, out string? releaseMethod) && !this.Reader.StringComparer.Equals(methodDefinition.Name, releaseMethod)) + else if (this.options.UseSafeHandles && isIn && !isOut && !isReleaseMethod && parameterTypeInfo is HandleTypeHandleInfo parameterHandleTypeInfo && this.TryGetHandleReleaseMethod(parameterHandleTypeInfo.Handle, out string? releaseMethod) && !this.Reader.StringComparer.Equals(methodDefinition.Name, releaseMethod) + && !(this.TryGetTypeDefFieldType(parameterHandleTypeInfo, out TypeHandleInfo? fieldType) && !this.IsSafeHandleCompatibleTypeDefFieldType(fieldType))) { IdentifierNameSyntax typeDefHandleName = IdentifierName(externParam.Identifier.ValueText + "Local"); signatureChanged = true; diff --git a/test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs b/test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs index d6a8b6ea..6ee820cc 100644 --- a/test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs +++ b/test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs @@ -325,6 +325,7 @@ public void InterestingAPIs( "CIDLData_CreateFromIDArray", // Method with out parameter of a possibly marshaled interop type shared with the BCL, "ID3D12Resource", // COM interface with base types "OpenTrace", // the CloseTrace method called by the SafeHandle returns WIN32_ERROR. The handle is ALWAYS 64-bits. + "QueryTraceProcessingHandle", // uses a handle that is always 64-bits, even in 32-bit processes "ID2D1RectangleGeometry")] // COM interface with base types string api, bool allowMarshaling)