Skip to content

Commit

Permalink
Collapse methods with same name into same COM slot
Browse files Browse the repository at this point in the history
The IOpenFileDialog declared in the .NET 7 in such way that methods from IFileDialog duplicated which lead to incorrect counting of COM slot location
  • Loading branch information
kant2002 committed Jan 18, 2023
1 parent 51570e3 commit 5d173d9
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 3 deletions.
132 changes: 132 additions & 0 deletions WinFormsComInterop.SourceGenerator.Tests/RuntimeCallableWrapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2427,6 +2427,138 @@ unsafe partial class C : global::Foo.IStr2
}
}
}
}";
Assert.AreEqual(expectedOutput, output);
}

[TestMethod]
public void InheritanceSameMethods()
{
string source = @"
namespace Foo
{
using System.Runtime.InteropServices;
[Guid(""22DD68D1-86FD-4332-8666-9ABEDEA2D24C"")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IStr
{
void Read(byte* pv, uint cb, uint* pcbRead);
string Name { get; }
}
[Guid(""22DD68D2-86FD-4332-8666-9ABEDEA2D24C"")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IStr2: IStr
{
void Read(byte* pv, uint cb, uint* pcbRead);
string Name { get; }
void Write(byte* pv, uint cb, uint* pcbRead);
}
[RuntimeCallableWrapper(typeof(IStr2))]
partial class C
{
}
}";
string output = this.GetGeneratedOutput(source, NullableContextOptions.Disable);

Assert.IsNotNull(output);

var expectedOutput = @"// <auto-generated>
// Code generated by COM Proxy Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// </auto-generated>
#nullable enable
using Marshal = System.Runtime.InteropServices.Marshal;
namespace Foo
{
[System.Runtime.Versioning.SupportedOSPlatform(""windows"")]
unsafe partial class C : global::Foo.IStr2
{
void global::Foo.IStr2.Read(byte* pv, uint cb, uint* pcbRead)
{
var targetInterface = new System.Guid(""22DD68D2-86FD-4332-8666-9ABEDEA2D24C"");
var result = Marshal.QueryInterface(this.instance, ref targetInterface, out var thisPtr);
if (result != 0)
{
throw new System.InvalidCastException();
}
try
{
var comDispatch = (System.IntPtr*)thisPtr;
var vtbl = (System.IntPtr*)comDispatch[0];
result = ((delegate* unmanaged<System.IntPtr, byte*, uint, uint*, int>)vtbl[3])(thisPtr, pv, cb, pcbRead);
if (result != 0)
{
Marshal.ThrowExceptionForHR(result);
}
}
finally
{
Marshal.Release(thisPtr);
}
}
string global::Foo.IStr2.Name
{
get
{
var targetInterface = new System.Guid(""22DD68D2-86FD-4332-8666-9ABEDEA2D24C"");
var result = Marshal.QueryInterface(this.instance, ref targetInterface, out var thisPtr);
if (result != 0)
{
throw new System.InvalidCastException();
}
try
{
var comDispatch = (System.IntPtr*)thisPtr;
var vtbl = (System.IntPtr*)comDispatch[0];
System.IntPtr retVal;
result = ((delegate* unmanaged<System.IntPtr, System.IntPtr*, int>)vtbl[4])(thisPtr, &retVal);
if (result != 0)
{
Marshal.ThrowExceptionForHR(result);
}
return retVal == System.IntPtr.Zero ? null : (string)Marshal.PtrToStringUni(retVal);
}
finally
{
Marshal.Release(thisPtr);
}
}
}
void global::Foo.IStr2.Write(byte* pv, uint cb, uint* pcbRead)
{
var targetInterface = new System.Guid(""22DD68D2-86FD-4332-8666-9ABEDEA2D24C"");
var result = Marshal.QueryInterface(this.instance, ref targetInterface, out var thisPtr);
if (result != 0)
{
throw new System.InvalidCastException();
}
try
{
var comDispatch = (System.IntPtr*)thisPtr;
var vtbl = (System.IntPtr*)comDispatch[0];
result = ((delegate* unmanaged<System.IntPtr, byte*, uint, uint*, int>)vtbl[5])(thisPtr, pv, cb, pcbRead);
if (result != 0)
{
Marshal.ThrowExceptionForHR(result);
}
}
finally
{
Marshal.Release(thisPtr);
}
}
}
}";
Assert.AreEqual(expectedOutput, output);
}
Expand Down
30 changes: 27 additions & 3 deletions WinFormsComInterop.SourceGenerator/WrapperGenerationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ public MethodGenerationContext CreateMethodGenerationContext(
return existingContext;
}

var originalMethod = method;
IMethodSymbol? previousMethod = null;
MethodGenerationContext? previousMethodContext = null;
if (method.ContainingType.AllInterfaces.Length != 0)
{
foreach (var parentInterface in method.ContainingType.AllInterfaces)
{
var sameMembers = parentInterface.GetMembers(method.Name);

// I do not distinguish between methods with same name and different parameters.
var previousMember = sameMembers.SingleOrDefault();
if (previousMember != null)
{
previousMethod = (IMethodSymbol)previousMember;
previousMethodContext = CreateMethodGenerationContext(classSymbol, previousMethod, ref comSlotNumber);
break;
}
}
}

var preserveSigAttribute = method.GetAttributes().FirstOrDefault(ad =>
{
var attributeName = ad.AttributeClass?.ToDisplayString();
Expand All @@ -101,13 +121,17 @@ public MethodGenerationContext CreateMethodGenerationContext(
});
var preserveSignature = preserveSigAttribute != null;
preserveSignature |= (method.MethodImplementationFlags & System.Reflection.MethodImplAttributes.PreserveSig) == System.Reflection.MethodImplAttributes.PreserveSig;
var methodContext = new MethodGenerationContext(method, classSymbol.Type, this)
var methodContext = new MethodGenerationContext(originalMethod, classSymbol.Type, this)
{
PreserveSignature = preserveSignature,
ComSlotNumber = comSlotNumber,
ComSlotNumber = previousMethodContext?.ComSlotNumber ?? comSlotNumber,
};
contextCache.Add(key, methodContext);
comSlotNumber++;
if (previousMethodContext is null)
{
comSlotNumber++;
}

return methodContext;
}

Expand Down

0 comments on commit 5d173d9

Please sign in to comment.