| only for RuBoard - do not distribute or recompile |
As mentioned previously,
_Animal
is directly
derived from an interface called
IDispatch
. Interfaces
derived from
IDispatch
often have the
[dual]
attribute and appropriately are called
dual interfaces
. This
is because the interface supports vtable binding (binding at
compile time) and late binding (binding at run-time). The
'Late binding Cow
Dim cow1 As Object
Set cow1 = CreateObject("Animals.Cow")
MsgBox cow1.Noise
CreateObject uses the ProgID for the component and maps it to a CLSID. This allows an instance of the component to be created. Internally, this is done by calling the CoCreateInstanceEx API. Once the component is loaded, a call to QueryInterface is made and a pointer to an IDispatch interface is returned. The generic Object datatype really means IDispatch . Then late binding is used to make the call to the Noise method.
Late binding is
GetTypeInfoCount
GetTypeInfo
GetIDsOfNames
Invoke
GetTypeInfoCount returns either a or a 1, depending on the availability of type information. Since we know the Animals component contains type information, an implementation of GetTypeInfoCount written in Visual Basic might be as simple as this:
Private Sub GetTypeInfoCount( ) As Long GetTypeInfoCount = 1 End Sub
GetTypeInfo
returns an
ITypeInfo
interface pointer, which provides the means for accessing
information in a type library.
ITypeInfo
is useful when no
prior knowledge of a component is available. Utilities such as
Object Browser and OLE View use
IDispatch::GetTypeInfo
to
get information about a component from a type library. Typically,
you are familiar with the
Refer back to the
_Animal
interface for a moment
and look at the
[id]
attribute that
Invoke is the heart of the IDispatch interface. This method is responsible for making the late binding call to a method or property. Invoke indirectly calls a method or property using the DISPID returned by GetIDsOfNames . The return value, if there is one, is packed into a VARIANT structure and returned to the caller.
Now that we have discussed IUnknown and IDispatch , you should have a better idea of what is going on internally in a component written in Visual Basic. We now know that there is a little more going on in the _Animal interface than was initially apparent. Figure 2.6 shows a graphic depiction of the _Animal interface.
And with this last piece of information, the question "Why
can't we use VB to generate the interfaces we need?" can finally be
| only for RuBoard - do not distribute or recompile |