Skip to content

Commit

Permalink
Merge pull request #587 from microsoft/comoutptr328
Browse files Browse the repository at this point in the history
Add option to generate out IntPtr for COM output pointer parameters
  • Loading branch information
AArnott committed Jun 16, 2022
2 parents 4875c82 + 73f7e4c commit 4dd33b3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
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 @@ -4230,14 +4232,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 @@ -1427,9 +1438,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

0 comments on commit 4dd33b3

Please sign in to comment.