Skip to content

Commit

Permalink
fix: Added DispID offset in case of ComVisible false methods (#246)
Browse files Browse the repository at this point in the history
* fix: Added DispID offset in case of ComVisible false methods or properties

* fix: Convert CRLF to LF
  • Loading branch information
marklechtermann committed Apr 19, 2024
1 parent f1754d6 commit 5589399
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/dscom.test/tests/MemIdTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,15 @@ public void IDispatchInterfaceWith3MethodsAndOneComVisibleFalse_MemIdAndVTableSi
using var method1 = testInterface!.GetFuncDescByName("Method1");
method1.Should().NotBeNull();
method1!.Value.oVft.Should().Be(0);
method1!.Value.memid.Should().Be((int)Constants.BASE_OLEAUT_DISPID);

using var method2 = testInterface!.GetFuncDescByName("Method2");
method2.Should().BeNull();

using var method3 = testInterface!.GetFuncDescByName("Method3");
method3.Should().NotBeNull();
method3!.Value.oVft.Should().Be(0);
method3!.Value.memid.Should().Be(((int)Constants.BASE_OLEAUT_DISPID) + 2);
}

[Fact]
Expand All @@ -491,13 +493,15 @@ public void IDispatchInterfaceWith3PropertiesAndOneComVisibleFalse_MemIdAndVTabl
using var method1 = testInterface!.GetFuncDescByName("Property1");
method1.Should().NotBeNull();
method1!.Value.oVft.Should().Be(0);
method1!.Value.memid.Should().Be((int)Constants.BASE_OLEAUT_DISPID);

using var method2 = testInterface!.GetFuncDescByName("Property2");
method2.Should().BeNull();

using var method3 = testInterface!.GetFuncDescByName("Property3", INVOKEKIND.INVOKE_PROPERTYGET);
method3.Should().NotBeNull();
method3!.Value.oVft.Should().Be(0);
method3!.Value.memid.Should().Be(((int)Constants.BASE_OLEAUT_DISPID) + 4);
}

[Fact]
Expand All @@ -522,13 +526,15 @@ public void DualInterfaceWith3MethodsAndOneComVisibleFalse_MemIdAndVTableSizeAre
using var method1 = testInterface!.GetFuncDescByName("Method1");
method1.Should().NotBeNull();
method1!.Value.oVft.Should().Be(56);
method1!.Value.memid.Should().Be((int)Constants.BASE_OLEAUT_DISPID);

using var method2 = testInterface!.GetFuncDescByName("Method2");
method2.Should().BeNull();

using var method3 = testInterface!.GetFuncDescByName("Method3");
method3.Should().NotBeNull();
method3!.Value.oVft.Should().Be(72);
method3!.Value.memid.Should().Be(((int)Constants.BASE_OLEAUT_DISPID) + 2);
}

[Fact]
Expand All @@ -553,13 +559,15 @@ public void DualInterfaceWith3PropertiesAndOneComVisibleFalse_MemIdAndVTableSize
using var method1 = testInterface!.GetFuncDescByName("Property1");
method1.Should().NotBeNull();
method1!.Value.oVft.Should().Be(56);
method1!.Value.memid.Should().Be((int)Constants.BASE_OLEAUT_DISPID);

using var method2 = testInterface!.GetFuncDescByName("Property2");
method2.Should().BeNull();

using var method3 = testInterface!.GetFuncDescByName("Property3", INVOKEKIND.INVOKE_PROPERTYGET);
method3.Should().NotBeNull();
method3!.Value.oVft.Should().Be(88);
method3!.Value.memid.Should().Be(((int)Constants.BASE_OLEAUT_DISPID) + 4);
}

[Theory]
Expand Down
2 changes: 1 addition & 1 deletion src/dscom/DispatchIdCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void AddDispatchId(uint idGenerated, uint? idFromAttribute, MemberInfo m
}
}

private uint GetNextFreeDispId()
internal uint GetNextFreeDispId()
{
var dispId = _currentDispId;
_currentDispId++;
Expand Down
9 changes: 9 additions & 0 deletions src/dscom/writer/InterfaceWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,22 @@ private void CreateMethodWriters()
var functionIndex = 0;
foreach (var methodWriter in MethodWriters)
{
// A methodWriter can be null if ComVisible is false
if (methodWriter is not null)
{
methodWriter.FunctionIndex = functionIndex;
methodWriter.VTableOffset = VTableOffsetUserMethodStart + (index * IntPtr.Size);
DispatchIdCreator!.RegisterMember(methodWriter);
functionIndex += methodWriter.IsValid ? 1 : 0;
}
else
{
// In case of ComVisible false, we need to increment the the next free DispId
// This offset is needed to keep the DispId in sync with the VTable and the behavior of tlbexp.
DispatchIdCreator!.GetNextFreeDispId();
}

// Increment the index for the VTableOffset
index++;
}
}
Expand Down

0 comments on commit 5589399

Please sign in to comment.