Skip to content

Commit df226e9

Browse files
authored
Skip arrays of SafeHandle params (#1567)
1 parent 963af2b commit df226e9

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

src/Microsoft.Windows.CsWin32/Generator.FriendlyOverloads.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,11 @@ private IEnumerable<MethodDeclarationSyntax> DeclareFriendlyOverload(
354354
bool hasOut = externParam.Modifiers.Any(SyntaxKind.OutKeyword);
355355
arguments[paramIndex] = arguments[paramIndex].WithRefKindKeyword(TokenWithSpace(hasOut ? SyntaxKind.OutKeyword : SyntaxKind.RefKeyword));
356356
}
357-
else if (isOut && !isIn && !isReleaseMethod && parameterTypeInfo is PointerTypeHandleInfo { ElementType: HandleTypeHandleInfo pointedElementInfo } && pointedElementInfo.Generator.TryGetHandleReleaseMethod(pointedElementInfo.Handle, paramAttributes, out string? outReleaseMethod) && !this.Reader.StringComparer.Equals(methodDefinition.Name, outReleaseMethod))
357+
else if (isOut && !isIn && !isReleaseMethod && parameterTypeInfo is PointerTypeHandleInfo { ElementType: HandleTypeHandleInfo pointedElementInfo } &&
358+
pointedElementInfo.Generator.TryGetHandleReleaseMethod(pointedElementInfo.Handle, paramAttributes, out string? outReleaseMethod) && !this.Reader.StringComparer.Equals(methodDefinition.Name, outReleaseMethod) &&
359+
(memorySize is null) && !isArray)
358360
{
361+
// NOTE: We don't handle scenarios where the parameter is [MemorySize] annotated (e.g. EnumProcessModules) or [NativeArrayInfo] (e.g. ITypeInfo.GetNames)
359362
if (this.RequestSafeHandle(outReleaseMethod) is TypeSyntax safeHandleType)
360363
{
361364
signatureChanged = true;

test/CsWin32Generator.Tests/CsWin32GeneratorTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ public async Task PointerReturnValueIsPreserved()
207207
["IWbemServices", "GetObject", "this winmdroot.System.Wmi.IWbemServices @this, SafeHandle strObjectPath, winmdroot.System.Wmi.WBEM_GENERIC_FLAG_TYPE lFlags, winmdroot.System.Wmi.IWbemContext pCtx, ref winmdroot.System.Wmi.IWbemClassObject ppObject, ref winmdroot.System.Wmi.IWbemCallResult ppCallResult"],
208208
// NativeOverlapped should be pointer even when not [Retained] as in CancelIoEx.
209209
["CancelIoEx", "CancelIoEx", "SafeHandle hFile, global::System.Threading.NativeOverlapped* lpOverlapped"],
210+
["ITypeInfo", "GetNames", "this winmdroot.System.Com.ITypeInfo @this, int memid, Span<winmdroot.Foundation.BSTR> rgBstrNames, out uint pcNames"],
211+
["EnumProcessModules", "EnumProcessModules", "SafeHandle hProcess, Span<byte> lphModule, out uint lpcbNeeded"],
210212
];
211213

212214
[Theory]

0 commit comments

Comments
 (0)