The use of interfaces is crucial to efficient COM solutions, as it enables the use of early binding to be maximised in a number of situations, and the benefits to be reaped where otherwise late binding would have to be used.
• Interfaces allow early binding to be used when the exact component to be created is not known at design time.
• Interfaces can enforce implementation of a set of standard properties and methods across different classes.
• Interfaces allow different programming paradigms and function sets to be exposed by the same class.
Interfaces are best described with reference to the A-Shell ActiveX SDK itself.
Consider the implementation of Visual Basic XCALLs. Clearly there must be some standard way of coding these if A-Shell is to be able to dynamically access such an XCALL at run time. This is achieved by stating that each XCALL be implemented as a class with two methods – Ping and Execute – implemented. The first is used to check the availability and readiness of the XCALL to be called, and the second to execute an actual call to it.
Under normal circumstances calls to these methods would have to be late bound, since the actual XCALL to be called (identified by its ProgID) is not known until run-time. However this is resolved, and early binding used, by implementing the two methods on an interface – the IXcall interface, which all XCALL classes must implement with the Visual Basic line:
Implements ASHW32Lib.IXcall
Because the IXcall interface is clearly defined (by A-Shell), and it is known that all created XCALLs implement this interface, A-Shell is able to use early binding to access them. This is one of the chief reasons why Visual Basic XCALLs are able to be executed so fast.
Although A-Shell is written in C/C++, the code sequence it uses to call a Visual Basic XCALL can be paraphrased as:
Dim oXcall As Object
Dim oXcall2 As
ASHW32Lib.IXcall
’
’ Late bound call to load and create the XCALL
Set
oXcall = CreateObject(”MyLib.MyXcall”)
’
’ early bound call to execute the
XCALL
Set oXcall2 = oXcall
If oXcall2.Ping() Then
oXcall2.Execute…
A-Shell will cache the IXcall interface references to each COM XCALL accessed, so the late bound call to load and create an instance of the XCALL class is only executed once.
The Visual Basic technique to obtain a pointer to a specific interface supported by a class is to declare an object pointer of the interface type, and to set it equal to the reference of an already created object of a class supporting the interface. This is demonstrated in the code segment above, and is also the technique used to gain access to the IMiameLib interface of the A-Shell Application object. This exposes a second set of methods which can be accessed:
Dim oAshell As Ashw32Lib.Application
Dim oMiame As
Ashw32Lib.IMiameLib
’
Set oAshell = New Ashw32Lib.Application
Set
oMiame = oAshell