Skip to content

Commit 5589399

Browse files
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
1 parent f1754d6 commit 5589399

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/dscom.test/tests/MemIdTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,13 +460,15 @@ public void IDispatchInterfaceWith3MethodsAndOneComVisibleFalse_MemIdAndVTableSi
460460
using var method1 = testInterface!.GetFuncDescByName("Method1");
461461
method1.Should().NotBeNull();
462462
method1!.Value.oVft.Should().Be(0);
463+
method1!.Value.memid.Should().Be((int)Constants.BASE_OLEAUT_DISPID);
463464

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

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

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

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

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

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

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

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

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

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

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

565573
[Theory]

src/dscom/DispatchIdCreator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private void AddDispatchId(uint idGenerated, uint? idFromAttribute, MemberInfo m
8080
}
8181
}
8282

83-
private uint GetNextFreeDispId()
83+
internal uint GetNextFreeDispId()
8484
{
8585
var dispId = _currentDispId;
8686
_currentDispId++;

src/dscom/writer/InterfaceWriter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,22 @@ private void CreateMethodWriters()
126126
var functionIndex = 0;
127127
foreach (var methodWriter in MethodWriters)
128128
{
129+
// A methodWriter can be null if ComVisible is false
129130
if (methodWriter is not null)
130131
{
131132
methodWriter.FunctionIndex = functionIndex;
132133
methodWriter.VTableOffset = VTableOffsetUserMethodStart + (index * IntPtr.Size);
133134
DispatchIdCreator!.RegisterMember(methodWriter);
134135
functionIndex += methodWriter.IsValid ? 1 : 0;
135136
}
137+
else
138+
{
139+
// In case of ComVisible false, we need to increment the the next free DispId
140+
// This offset is needed to keep the DispId in sync with the VTable and the behavior of tlbexp.
141+
DispatchIdCreator!.GetNextFreeDispId();
142+
}
143+
144+
// Increment the index for the VTableOffset
136145
index++;
137146
}
138147
}

0 commit comments

Comments
 (0)