How can I, for any two given objects, determine at runtime whether those two objects implement the same interface?
Given an interface IStackExchange
and the two classes StackOverflow
and CodeReviewSE
that both implement IStackExchange
, I know I can do stuff like this:
Dim SO As IStackExchange
Dim CRSE As IStackExchange
Set SO = New StackOverflow
Set CRSE = New CodeReviewSE
Debug.Print TypeName(SO) ' StackOverflow
Debug.Print TypeOf SO Is IStackExchange ' True
Debug.Print TypeOf SO Is StackOverflow ' True
Debug.Print TypeOf SO Is CodeReviewSE ' False
Debug.Print TypeName(CRSE) ' CodeReviewSE
Debug.Print TypeOf CRSE Is IStackExchange ' True
Debug.Print TypeOf CRSE Is StackOverflow ' False
Debug.Print TypeOf CRSE Is CodeReviewSE ' True
So, if I know what interface to cast my objects to, I can verify they implement that specific interface. But VBA won't allow me to do things like TypeOf SO Is TypeOf CRSE
or Debug.Print TypeOf SO
. And TypeName()
is not helpful when it comes to interfaces.
How can I obtain more information about an object's implemented interfaces without hard-coding the interfaces' names? Solutions involving additional libraries (as long as shipped with Windows >=7) are more than welcome.
I've seen someone mention casting the objects to an IUnknown
type, but that only helps in determining whether two object variables internally reference the same object - it does not tell anything about the implemented interfaces.