Late Binding

The standard methodology of DISPATCH is called "Late Binding", because nothing is done in advance.  No method definitions.  No Interface signatures.  You can pretty much just start writing code:

LOCAL DispVar AS IDISPATCH
LET DispVar = NEWCOM "DispProgID"
OBJECT CALL DispVar.Method1(x&, y$)

It's just that easy.  The first line declares an object variable which assumes the DISPATCH interface, while the second line creates an object and assigns a reference to DispVar.  The third line just executes a method on the new object.

The OBJECT statement is always used to execute methods on a DISPATCH interface.  This differentiates it from direct access, so PowerBASIC can handle your request in the appropriate manner.

It's important to note that this version of PowerBASIC relaxes the strict type checking of Dispatch parameters.  While DISPATCH interfaces require that all parameters and return values be passed as a VARIANT variable, this version of PowerBASIC relaxes that requirement for you. You may substitute any COM-compatible variable, and PowerBASIC will convert them automatically to and from Variant variables as an integral part of the OBJECT statement.  How could it get easier?

So, how does this work internally?

Well, each method name is assigned a positive integer number as its Dispatch ID (or DispID), to differentiate it from the other methods. In a similar fashion, each parameter is numbered from 0 - n to identify each of them uniquely.  When you execute a statement like:

OBJECT CALL DispVar.Method1(x&, y$)

PowerBASIC packages up the Method Name (Method1) and the names of any named parameters (none in this example - more about that later), and passes them to a special DISPATCH function.  After a bit of time for lookup, the Dispatch ID (let's say the number 77) is returned. PowerBASIC then converts the two parameters to Variants and packages the whole thing up to call another special Dispatch function.  This tells the server to execute Method number 77 using the two enclosed parameters.  Finally, it returns with an hResult code to indicate success or failure.  That's classic "Late Binding" Dispatch.

"Late Binding" is flexible and easy to use because everything is resolved at run-time.  That flexibility comes at a price -- it's the slowest form of COM.  

 

See Also

What is an object, anyway?

Just what is COM?

What is DISPATCH?

ID Binding