Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/Microsoft.Windows.CsWin32/Generator.FriendlyOverloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ private IEnumerable<MethodDeclarationSyntax> DeclareFriendlyOverload(MethodDefin
bool isOptional = (param.Attributes & ParameterAttributes.Optional) == ParameterAttributes.Optional;
CustomAttributeHandleCollection paramAttributes = param.GetCustomAttributes();
bool isReserved = this.FindInteropDecorativeAttribute(paramAttributes, "ReservedAttribute") is not null;
bool isRetained = this.FindInteropDecorativeAttribute(paramAttributes, "RetainedAttribute") is not null;
isOptional |= isReserved; // Per metadata decision made at https://github.com/microsoft/win32metadata/issues/1421#issuecomment-1372608090
bool isIn = (param.Attributes & ParameterAttributes.In) == ParameterAttributes.In;
bool isConst = this.FindInteropDecorativeAttribute(paramAttributes, "ConstAttribute") is not null;
Expand All @@ -164,9 +165,16 @@ private IEnumerable<MethodDeclarationSyntax> DeclareFriendlyOverload(MethodDefin
memorySize = DecodeMemorySizeAttribute(memorySizeAttribute);
}

// If there's no MemorySize attribute, we may still need to keep this parameter as a pointer if it's a struct with a flexible array.
if (memorySize is null)
if (isRetained)
{
// Retained means that the callee will keep the pointer beyond this call. To communicate that safety problem to the caller,
// the best we can do is project as a pointer so they know they need to think about it. See https://github.com/microsoft/CsWin32/issues/1066
// and linked issues for more info.
mustRemainAsPointer = true;
}
else if (memorySize is null)
{
// If there's no MemorySize attribute, we may still need to keep this parameter as a pointer if it's a struct with a flexible array.
mustRemainAsPointer = parameterTypeInfo is PointerTypeHandleInfo { ElementType: HandleTypeHandleInfo pointedElement } && pointedElement.Generator.IsStructWithFlexibleArray(pointedElement);
}
else if (!useSpansForPointers)
Expand Down
1 change: 1 addition & 0 deletions test/CsWin32Generator.Tests/CsWin32GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public async Task TestGenerateCoCreateableClass()
["SetupDiGetClassInstallParams", "SetupDiGetClassInstallParams", "SafeHandle DeviceInfoSet, winmdroot.Devices.DeviceAndDriverInstallation.SP_DEVINFO_DATA? DeviceInfoData, Span<byte> ClassInstallParams, out uint RequiredSize"],
["IEnumString", "Next", "this winmdroot.System.Com.IEnumString @this, Span<winmdroot.Foundation.PWSTR> rgelt, out uint pceltFetched"],
["PSCreateMemoryPropertyStore", "PSCreateMemoryPropertyStore", "in global::System.Guid riid, out void* ppv"],
["DeviceIoControl", "DeviceIoControl", "SafeHandle hDevice, uint dwIoControlCode, ReadOnlySpan<byte> lpInBuffer, Span<byte> lpOutBuffer, out uint lpBytesReturned, global::System.Threading.NativeOverlapped* lpOverlapped"],
];

[Theory]
Expand Down
Loading