Skip to content

Commit 02735ab

Browse files
committed
Add an explicit null check for casting to the event interface.
1 parent fa50478 commit 02735ab

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Rubberduck.VBEEditor/SafeComWrappers/SafeEventedComWrapper.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Runtime.InteropServices;
1+
using System;
2+
using System.Runtime.InteropServices;
23
using System.Runtime.InteropServices.ComTypes;
34
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
45

@@ -44,8 +45,14 @@ public void AttachEvents()
4445
var g = typeof(TEventInterface).GUID;
4546
icpc.FindConnectionPoint(ref g, out _icp);
4647

47-
// Pass a pointer to the host to the connection point
48-
_icp.Advise(this as TEventInterface, out _cookie);
48+
var sink = this as TEventInterface;
49+
50+
if (sink == null)
51+
{
52+
throw new InvalidOperationException($"The class {this.GetType()} does not implement the required event interface {typeof(TEventInterface)}");
53+
}
54+
55+
_icp.Advise(sink, out _cookie);
4956
}
5057
}
5158

0 commit comments

Comments
 (0)