From 55893991b6f487b17c33995db3591dda9367a0ee Mon Sep 17 00:00:00 2001 From: Mark Lechtermann Date: Fri, 19 Apr 2024 17:55:40 +0200 Subject: [PATCH] fix: Added DispID offset in case of ComVisible false methods (#246) * fix: Added DispID offset in case of ComVisible false methods or properties * fix: Convert CRLF to LF --- src/dscom.test/tests/MemIdTest.cs | 8 ++++++++ src/dscom/DispatchIdCreator.cs | 2 +- src/dscom/writer/InterfaceWriter.cs | 9 +++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/dscom.test/tests/MemIdTest.cs b/src/dscom.test/tests/MemIdTest.cs index 06e3350..2534d2f 100644 --- a/src/dscom.test/tests/MemIdTest.cs +++ b/src/dscom.test/tests/MemIdTest.cs @@ -460,6 +460,7 @@ 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(); @@ -467,6 +468,7 @@ public void IDispatchInterfaceWith3MethodsAndOneComVisibleFalse_MemIdAndVTableSi 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] @@ -491,6 +493,7 @@ 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(); @@ -498,6 +501,7 @@ public void IDispatchInterfaceWith3PropertiesAndOneComVisibleFalse_MemIdAndVTabl 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] @@ -522,6 +526,7 @@ 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(); @@ -529,6 +534,7 @@ public void DualInterfaceWith3MethodsAndOneComVisibleFalse_MemIdAndVTableSizeAre 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] @@ -553,6 +559,7 @@ 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(); @@ -560,6 +567,7 @@ public void DualInterfaceWith3PropertiesAndOneComVisibleFalse_MemIdAndVTableSize 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] diff --git a/src/dscom/DispatchIdCreator.cs b/src/dscom/DispatchIdCreator.cs index aebbc9a..db04a1f 100644 --- a/src/dscom/DispatchIdCreator.cs +++ b/src/dscom/DispatchIdCreator.cs @@ -80,7 +80,7 @@ private void AddDispatchId(uint idGenerated, uint? idFromAttribute, MemberInfo m } } - private uint GetNextFreeDispId() + internal uint GetNextFreeDispId() { var dispId = _currentDispId; _currentDispId++; diff --git a/src/dscom/writer/InterfaceWriter.cs b/src/dscom/writer/InterfaceWriter.cs index e9fc371..19865fa 100644 --- a/src/dscom/writer/InterfaceWriter.cs +++ b/src/dscom/writer/InterfaceWriter.cs @@ -126,6 +126,7 @@ 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; @@ -133,6 +134,14 @@ private void CreateMethodWriters() 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++; } }