Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to generate out IntPtr for COM output pointer parameters #587

Merged
merged 1 commit into from
Jun 16, 2022
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
17 changes: 11 additions & 6 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ private enum FriendlyOverloadOf

internal SuperGenerator? SuperGenerator { get; set; }

internal GeneratorOptions Options => this.options;

internal string InputAssemblyName { get; }

internal MetadataIndex MetadataIndex { get; }
Expand Down Expand Up @@ -4216,14 +4218,17 @@ private IEnumerable<MethodDeclarationSyntax> DeclareFriendlyOverloads(MethodDefi
yield return (MethodDeclarationSyntax)templateFriendlyOverload;
}

if (this.options.AllowMarshaling && this.TryFetchTemplate("marshaling/" + externMethodDeclaration.Identifier.ValueText, out templateFriendlyOverload))
if (externMethodDeclaration.Identifier.ValueText != "CoCreateInstance" || !this.options.ComInterop.UseIntPtrForComOutPointers)
{
yield return (MethodDeclarationSyntax)templateFriendlyOverload;
}
if (this.options.AllowMarshaling && this.TryFetchTemplate("marshaling/" + externMethodDeclaration.Identifier.ValueText, out templateFriendlyOverload))
{
yield return (MethodDeclarationSyntax)templateFriendlyOverload;
}

if (!this.options.AllowMarshaling && this.TryFetchTemplate("no_marshaling/" + externMethodDeclaration.Identifier.ValueText, out templateFriendlyOverload))
{
yield return (MethodDeclarationSyntax)templateFriendlyOverload;
if (!this.options.AllowMarshaling && this.TryFetchTemplate("no_marshaling/" + externMethodDeclaration.Identifier.ValueText, out templateFriendlyOverload))
{
yield return (MethodDeclarationSyntax)templateFriendlyOverload;
}
}

#pragma warning disable SA1114 // Parameter list should follow declaration
Expand Down
8 changes: 8 additions & 0 deletions src/Microsoft.Windows.CsWin32/GeneratorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,13 @@ public record ComInteropOptions
/// Gets an array of "interface.method" or "interface" strings that identify methods and interfaces that should be generated with <see cref="PreserveSigAttribute"/>.
/// </summary>
public ImmutableArray<string> PreserveSigMethods { get; init; } = ImmutableArray.Create<string>();

/// <summary>
/// Gets a value indicating whether to emit methods that return COM objects via output parameters using <see cref="IntPtr"/> as the parameter type instead of the COM interface.
/// </summary>
/// <remarks>
/// This may be useful on .NET when using ComWrappers. See <see href="https://github.com/microsoft/CsWin32/issues/328">this issue</see> for more details.
/// </remarks>
public bool UseIntPtrForComOutPointers { get; init; }
}
}
10 changes: 10 additions & 0 deletions src/Microsoft.Windows.CsWin32/PointerTypeHandleInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection.Metadata;
using System.Runtime.InteropServices;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.Windows.CsWin32.FastSyntaxFactory;

namespace Microsoft.Windows.CsWin32;
Expand Down Expand Up @@ -35,6 +36,15 @@ internal override TypeSyntaxAndMarshaling ToTypeSyntax(TypeSyntaxSettings inputs
}
else if (xIn || xOut)
{
if (elementTypeDetails.Type is PredefinedTypeSyntax { Keyword: { RawKind: (int)SyntaxKind.ObjectKeyword } } && inputs.Generator is not null && inputs.Generator.Options.ComInterop.UseIntPtrForComOutPointers)
{
bool isComOutPtr = inputs.Generator?.Reader is not null && customAttributes is not null && customAttributes.Value.Any(ah => inputs.Generator.IsAttribute(inputs.Generator.Reader.GetCustomAttribute(ah), Generator.InteropDecorationNamespace, "ComOutPtrAttribute"));
return new TypeSyntaxAndMarshaling(IdentifierName(nameof(IntPtr)))
{
ParameterModifier = Token(SyntaxKind.OutKeyword),
};
}

// But we can use a modifier to emulate a pointer and thereby enable marshaling.
return new TypeSyntaxAndMarshaling(elementTypeDetails.Type, elementTypeDetails.MarshalAsAttribute, elementTypeDetails.NativeArrayInfo)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Microsoft.Windows.CsWin32/settings.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
"uniqueItems": true,
"pattern": "^[\\w_]+(?:\\.[\\w_]+)?$"
}
},
"useIntPtrForComOutPointers": {
"description": "Emits methods that return COM objects via output parameters using IntPtr as the parameter type instead of the COM interface. This may be useful on .NET when using ComWrappers. See https://github.com/microsoft/CsWin32/issues/328 for more details.",
"type": "boolean",
"default": false
}
}
},
Expand Down
15 changes: 13 additions & 2 deletions test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,17 @@ public void ComOutPtrTypedAsOutObject()
Assert.Contains(this.FindGeneratedMethod(methodName), m => m.ParameterList.Parameters.Last() is { } last && last.Modifiers.Any(SyntaxKind.OutKeyword) && last.Type is PredefinedTypeSyntax { Keyword: { RawKind: (int)SyntaxKind.ObjectKeyword } });
}

[Fact]
public void ComOutPtrTypedAsIntPtr()
{
const string methodName = "CoCreateInstance";
this.generator = this.CreateGenerator(new GeneratorOptions { ComInterop = new() { UseIntPtrForComOutPointers = true } });
Assert.True(this.generator.TryGenerate(methodName, CancellationToken.None));
this.CollectGeneratedCode(this.generator);
this.AssertNoDiagnostics();
Assert.Contains(this.FindGeneratedMethod(methodName), m => m.ParameterList.Parameters.Last() is { } last && last.Modifiers.Any(SyntaxKind.OutKeyword) && last.Type is IdentifierNameSyntax { Identifier: { ValueText: "IntPtr" } });
}

[Fact]
public void AmbiguousApiName()
{
Expand Down Expand Up @@ -1412,9 +1423,9 @@ public void TBButton([CombinatorialMemberData(nameof(SpecificCpuArchitectures))]
}

[Theory, PairwiseData]
public void FullGeneration(bool allowMarshaling, [CombinatorialMemberData(nameof(AnyCpuArchitectures))] Platform platform)
public void FullGeneration(bool allowMarshaling, bool useIntPtrForComOutPtr, [CombinatorialMemberData(nameof(AnyCpuArchitectures))] Platform platform)
{
var generatorOptions = new GeneratorOptions { AllowMarshaling = allowMarshaling };
var generatorOptions = new GeneratorOptions { AllowMarshaling = allowMarshaling, ComInterop = new() { UseIntPtrForComOutPointers = useIntPtrForComOutPtr } };
this.compilation = this.compilation.WithOptions(this.compilation.Options.WithPlatform(platform));
this.generator = this.CreateGenerator(generatorOptions);
this.generator.GenerateAll(CancellationToken.None);
Expand Down