Skip to content

Commit ed8d12c

Browse files
authored
Translate VARIANT to ComVariant when using COM source generators (#1554)
1 parent a217367 commit ed8d12c

4 files changed

Lines changed: 24 additions & 5 deletions

File tree

src/Microsoft.Windows.CsWin32/Generator.Constant.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ public partial class Generator
88
/// <inheritdoc/>
99
public void GenerateAllConstants(CancellationToken cancellationToken)
1010
{
11-
foreach (FieldDefinitionHandle fieldDefHandle in this.MetadataIndex.Apis.SelectMany(api => this.Reader.GetTypeDefinition(api).GetFields()))
11+
var fields = this.MetadataIndex.Apis.SelectMany(api => this.Reader.GetTypeDefinition(api).GetFields());
12+
var sortedFields = fields.OrderBy(fieldDefHandle => this.Reader.GetString(this.Reader.GetFieldDefinition(fieldDefHandle).Name));
13+
14+
foreach (FieldDefinitionHandle fieldDefHandle in sortedFields)
1215
{
1316
cancellationToken.ThrowIfCancellationRequested();
1417

src/Microsoft.Windows.CsWin32/Generator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ public void GenerateAllMacros(CancellationToken cancellationToken)
569569
return;
570570
}
571571

572-
foreach (KeyValuePair<string, MethodDeclarationSyntax> macro in Win32SdkMacros)
572+
foreach (KeyValuePair<string, MethodDeclarationSyntax> macro in Win32SdkMacros.OrderBy(x => x.Key))
573573
{
574574
cancellationToken.ThrowIfCancellationRequested();
575575

src/Microsoft.Windows.CsWin32/HandleTypeHandleInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ internal override TypeSyntaxAndMarshaling ToTypeSyntax(TypeSyntaxSettings inputs
159159
return new TypeSyntaxAndMarshaling(IdentifierName(specialName));
160160
}
161161
}
162-
else if (useComSourceGenerators && !inputs.AllowMarshaling && (simpleName is "VARIANT" or "VARIANT_unmanaged") && this.Generator.CanUseComVariant)
162+
else if (useComSourceGenerators && (simpleName is "VARIANT" or "VARIANT_unmanaged") && this.Generator.CanUseComVariant)
163163
{
164164
return new TypeSyntaxAndMarshaling(QualifiedName(ParseName("global::System.Runtime.InteropServices.Marshalling"), IdentifierName("ComVariant")));
165165
}

test/CsWin32Generator.Tests/CsWin32GeneratorTests.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public async Task TestNativeMethods()
6666
[Fact]
6767
public async Task CheckITypeCompIsUnmanaged()
6868
{
69-
// Request DebugPropertyInfo and we should see ITypeComp_unmanaged get generated because it has an embedded managed field
70-
this.nativeMethods.Add("DebugPropertyInfo");
69+
// Request BINDPTR and we should see ITypeComp_unmanaged get generated because it has an embedded managed field
70+
this.nativeMethods.Add("BINDPTR");
7171
await this.InvokeGeneratorAndCompileFromFact();
7272

7373
var iface = this.FindGeneratedType("ITypeComp_unmanaged");
@@ -565,4 +565,20 @@ public async Task CrossWinMD_IInspectable(
565565
this.nativeMethods.Add("ITestDerivedFromInspectable");
566566
await this.InvokeGeneratorAndCompile($"{nameof(this.CrossWinMD_IInspectable)}_{tfm}_{allowMarshaling}_{pinvokeClassName ?? "null"}");
567567
}
568+
569+
[Fact]
570+
public async Task TestComVariantReturnValue()
571+
{
572+
// IUIAutomationElement has methods that return VARIANT, they should be translated to ComVariant
573+
this.nativeMethods.Add("IUIAutomationElement");
574+
await this.InvokeGeneratorAndCompileFromFact();
575+
576+
var iface = this.FindGeneratedType("IUIAutomationElement");
577+
Assert.NotEmpty(iface);
578+
579+
// And when generating IDispatch explicitly it should have "real" methods on it.
580+
var methods = iface.SelectMany(t => t.DescendantNodes().OfType<MethodDeclarationSyntax>());
581+
var method = Assert.Single(methods, m => m.Identifier.Text == "GetCachedPropertyValue");
582+
Assert.Contains("ComVariant", method.ReturnType.ToString());
583+
}
568584
}

0 commit comments

Comments
 (0)