Under the hood

So far, we have seen that with a mere handful of lines of BASIC code, we can create an instance of an Object ready to interact with.  While this appears to be a very simple procedure, PowerBASIC is doing an enormous amount of work for us behind the scenes.  The following discussion is optional reading, but is recommended to give a perspective on what happens under the hood when creating a reference to an instance of a COM Object.  It should be noted that this is a simplistic overview, since the actual implementation is very complex and beyond the scope of this document.

At the beginning of the process to create a reference to an Object, PowerBASIC communicates with the Object through an Interface in the Object called the Unknown Interface (IUnknown).  All COM Objects must contain this Interface.

The IUnknown Interface is considered as the "building block" Interface of all COM components, and this is inherited by all other Interfaces provided by a COM component.  Using a lollipop diagram, we can demonstrate how the intrinsic IUnknown Interface is represented:

You may also recall we mentioned earlier that a COM Object knows a lot about itself - it is through this IUnknown Interface that an Object provides details about itself to it controller (the COM client application).  Naturally, PowerBASIC also makes use of this Interface to query the availability of IDispatch and other Interfaces.

If the COM component can provide an IDispatch Interface for late-binding purposes, PowerBASIC continues to initialize the component; otherwise, the component is released.

The lollipop diagram for an Object that supports the IDispatch Interface is represented like this:

If PowerBASIC obtains a reference to the IDispatch Interface, PowerBASIC retains the reference.  In the case of late binding, this reference is placed in the DISPATCH object variable.  All subsequent late-bound Interface calls are channeled through this IDispatch reference, first to obtain a reference to the target Method or Property, and then to access the actual target member (through a single entry-point called Invoke) using the reference ID obtained from IDispatch.

In the case of early-binding, PowerBASIC can skip the IDispatch Interface to query Interface, Methods, and Properties of the Object.  This is because the target member reference (ID) is specified in the INTERFACE block (see the early-binding example above), so PowerBASIC can access the target Interface member through Invoke, without first asking for the reference through IDispatch.

To explain it a different way: with early-binding, PowerBASIC can skip the initial Interface and Interface member query at run-time because the relevant ID's for the target Interface and members have been predetermined during compilation instead.  This results in a much-improved run-time performance, as compared to late-binding.

If an Object supports an Interface named "IDatabase", the lollipop diagram for that object can be represented like this:

Once PowerBASIC has an Interface reference, PowerBASIC application code is guaranteed access to all Methods and Properties (members) of that Interface.  We'll demonstrate how to use these Interface members in the next section.

 

See Also

COM Programming Introduction

What is a Dispatch Interface?

Early-binding and Late-binding

Methods and Properties