Skip to content
Open
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
7 changes: 4 additions & 3 deletions src/Microsoft.Windows.CsWin32/Generator.FriendlyOverloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ private IEnumerable<MethodDeclarationSyntax> DeclareFriendlyOverloads(MethodDefi
// wstrParam1
arguments[param.SequenceNumber - 1] = Argument(localWstrName);

// if (buffer != null && buffer.LastIndexOf('\0') == -1) throw new ArgumentException("Required null terminator is missing.", "Param1");
// if (Param1 != null && Param1.LastIndexOf('\0') == -1) throw new ArgumentException("Required null terminator is missing.", "Param1");
InvocationExpressionSyntax lastIndexOf = InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, origName, IdentifierName(nameof(MemoryExtensions.LastIndexOf))),
ArgumentList().AddArguments(Argument(LiteralExpression(SyntaxKind.CharacterLiteralExpression, Literal('\0')))));
Expand All @@ -465,15 +465,16 @@ private IEnumerable<MethodDeclarationSyntax> DeclareFriendlyOverloads(MethodDefi
leadingStatements.Add(LocalDeclarationStatement(
VariableDeclaration(externParam.Type).AddVariables(VariableDeclarator(localWstrName.Identifier).WithInitializer(EqualsValueClause(localName)))));

// Param1 = Param1.Slice(0, wstrParam1.Length);
// Preserve the null terminator in the result, which contractually was included in the input.
// Param1 = Param1.Slice(0, wstrParam1.Length + 1);
trailingStatements.Add(ExpressionStatement(AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
origName,
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, origName, IdentifierName(nameof(Span<char>.Slice))),
ArgumentList().AddArguments(
Argument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))),
Argument(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, localWstrName, IdentifierName("Length"))))))));
Argument(BinaryExpression(SyntaxKind.AddExpression, MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, localWstrName, IdentifierName("Length")), LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(1)))))))));
}
else if (isIn && isOptional && !isOut && isManagedParameterType && parameterTypeInfo is PointerTypeHandleInfo ptrInfo && ptrInfo.ElementType.IsValueType(parameterTypeSyntaxSettings) is true && this.canUseUnsafeAsRef)
{
Expand Down
2 changes: 1 addition & 1 deletion test/GenerationSandbox.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public void PathParseIconLocation_Friendly()
sourceString.AsSpan().CopyTo(buffer);
int result = PInvoke.PathParseIconLocation(ref buffer);
Assert.Equal(3, result);
Assert.Equal("hi there", buffer.ToString());
Assert.Equal("hi there\0", buffer.ToString());
}

[Fact]
Expand Down