Skip to content
Draft
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
21 changes: 17 additions & 4 deletions src/Microsoft.Windows.CsWin32/Generator.FriendlyOverloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ private IEnumerable<MethodDeclarationSyntax> DeclareFriendlyOverload(
bool isManagedParameterType = this.IsManagedType(parameterTypeInfo);
MemorySize? memorySize = null;
bool mustRemainAsPointer = false;
bool isArray = false;
bool projectAsSpanBytes = false;
bool isPointerToStructWithFlexibleArray = parameterTypeInfo is PointerTypeHandleInfo { ElementType: HandleTypeHandleInfo pointedElement } && pointedElement.Generator.IsStructWithFlexibleArray(pointedElement);
if (this.FindInteropDecorativeAttribute(paramAttributes, MemorySizeAttribute) is CustomAttribute memorySizeAttribute)
{
Expand All @@ -214,7 +216,20 @@ private IEnumerable<MethodDeclarationSyntax> DeclareFriendlyOverload(
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 = isPointerToStructWithFlexibleArray;
if (isPointerToStructWithFlexibleArray)
{
if (improvePointersToSpansAndRefs)
{
// FlexibleSizeArrays are easier to work with if they project as Span<byte>. Callers can request the pointer verions
// via FriendlyOverloads.IncludePointerOverloads = true.
isArray = true;
projectAsSpanBytes = true;
}
else
{
mustRemainAsPointer = true;
}
}
}
else if (!improvePointersToSpansAndRefs)
{
Expand All @@ -239,7 +254,6 @@ private IEnumerable<MethodDeclarationSyntax> DeclareFriendlyOverload(

IdentifierNameSyntax origName = IdentifierName(externParam.Identifier.ValueText);

bool isArray = false;
bool isNullTerminated = false; // TODO
bool isCountOfBytes = false;
short? countParamIndex = null;
Expand Down Expand Up @@ -273,7 +287,6 @@ private IEnumerable<MethodDeclarationSyntax> DeclareFriendlyOverload(
isCountOfBytes = true;
}

bool projectAsSpanBytes = false;
if (improvePointersToSpansAndRefs && IsVoidPtrOrPtrPtr(externParam.Type))
{
// if it's memory-sized project as Span<byte>
Expand Down Expand Up @@ -1002,7 +1015,7 @@ bool TryHandleCountParam(TypeSyntax elementType, bool nullableSource)
if (externParam.Type is PointerTypeSyntax)
{
remainsRefType = false;
if (isCountOfBytes)
if (isCountOfBytes || isPointerToStructWithFlexibleArray)
{
// For parameters annotated as count of bytes, we need to switch the friendly parameter to Span<byte>
// and then cast to (ParamType*) when we call the p/invoke.
Expand Down
2 changes: 2 additions & 0 deletions test/CsWin32Generator.Tests/CsWin32GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ public async Task PointerReturnValueIsPreserved()
["CancelIoEx", "CancelIoEx", "SafeHandle hFile, global::System.Threading.NativeOverlapped* lpOverlapped"],
["ITypeInfo", "GetNames", "this winmdroot.System.Com.ITypeInfo @this, int memid, Span<winmdroot.Foundation.BSTR> rgBstrNames, out uint pcNames"],
["EnumProcessModules", "EnumProcessModules", "SafeHandle hProcess, Span<byte> lphModule, out uint lpcbNeeded"],
["AdjustTokenPrivileges", "AdjustTokenPrivileges", "SafeHandle TokenHandle, winmdroot.Foundation.BOOL DisableAllPrivileges, Span<byte> NewState, Span<byte> PreviousState, out uint ReturnLength"],
["IMAPIStatus", "FlushQueues", "nuint ulUIParam, ReadOnlySpan<byte> lpTargetTransport, uint ulFlags"],
];

[Theory]
Expand Down
5 changes: 4 additions & 1 deletion test/Microsoft.Windows.CsWin32.Tests/COMTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,11 @@ public void ITypeNameBuilder_ToStringOverload(bool allowMarshaling)
[Fact]
public void ReferencesToStructWithFlexibleArrayAreAlwaysPointers()
{
this.generator = this.CreateGenerator(DefaultTestGeneratorOptions with { FriendlyOverloads = DefaultTestGeneratorOptions.FriendlyOverloads with { IncludePointerOverloads = true } });
this.GenerateApi("IAMLine21Decoder");
Assert.All(this.FindGeneratedMethod("SetOutputFormat"), m => Assert.IsType<PointerTypeSyntax>(m.ParameterList.Parameters[0].Type));

// At least one of the methods should take pointers.
Assert.Contains(this.FindGeneratedMethod("SetOutputFormat"), m => m.ParameterList.Parameters[0].Type is PointerTypeSyntax);

// Assert that the 'unmanaged' declaration of the struct is the *only* declaration.
Assert.Single(this.FindGeneratedType("BITMAPINFO"));
Expand Down
5 changes: 4 additions & 1 deletion test/Microsoft.Windows.CsWin32.Tests/ExternMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ public void DefaultEntryPointIsNotEmitted()
[Fact]
public void ReferencesToStructWithFlexibleArrayAreAlwaysPointers()
{
this.generator = this.CreateGenerator(DefaultTestGeneratorOptions with { FriendlyOverloads = DefaultTestGeneratorOptions.FriendlyOverloads with { IncludePointerOverloads = true } });
this.GenerateApi("CreateDIBSection");
Assert.All(this.FindGeneratedMethod("CreateDIBSection"), m => Assert.IsType<PointerTypeSyntax>(m.ParameterList.Parameters[1].Type));

// At least one of the methods should take pointers.
Assert.Contains(this.FindGeneratedMethod("CreateDIBSection"), m => m.ParameterList.Parameters[1].Type is PointerTypeSyntax);

// Assert that the 'unmanaged' declaration of the struct is the *only* declaration.
Assert.Single(this.FindGeneratedType("BITMAPINFO"));
Expand Down
Loading