What the heck is a Vtable?

A Vtable is a structure in the header of every class object that contains the memory addresses of the actual code associated with the properties and methods implemented in the interface.

Let's look at the IEnumVARIANT interface as it would appear if it were implemented in a VB class

Option Explicit

Implements IEnumVARIANT

Private Sub IEnumVARIANT_Next(ByVal cElements As Long, aVariants As Variant, ByVal lpcElementsFetched As Long)
End Sub

Private Sub IEnumVARIANT_Skip(ByVal cElements As Long)
End Sub

Private Sub IEnumVARIANT_Reset()
End Sub

Private Sub IEnumVARIANT_Clone(lppIEnum As IEnumVARIANT)
End Sub

IEnumVARIANT is derived from the IUnknown interface which has three methods, QueryInterface, AddRef and Release.  These methods appear in the Vtable before the methods in the IEnumVARIANT interface.  Below is what the Vtable for IEnumVARIANT looks like.

Address for IUnknown :: QueryInterface

Address for IUnknown :: AddRef

Address for IUnknown :: Release

Address for IEnumVARIANT :: Next

Address for IEnumVARIANT :: Skip

Address for IEnumVARIANT :: Reset

Address for IEnumVARIANT :: Clone

We can cause the caller to call any routine we want by replacing the address for any one of the above table entries with the address of another function somewhere in our project..



vbVision Home | Super Collections