Article ID: 138138 - Last Review: December 9, 2003 - Revision: 2.0 INFO: Late, ID, Early Binding Types Possible in VB for AppsThis article was previously published under Q138138 On This PageSUMMARY
There are three kinds of binding that Visual Basic for Applications
supports:
MORE INFORMATION
OLE Automation controllers can use the OLE IDispatch interface to gain
access to objects that implement this interface. Although programmers know
object members (methods and properties) by name, IDispatch keeps track of
them internally by a number -- the Dispatch ID (DISPID). Before an OLE
Automation controller can find a method or property, it must have the
DISPID that maps to the name of the member.
Once it has the DISPID, it can then call IDispatch::Invoke to locate the property or invoke the method, packaging the parameters for the property or method into one of the IDispatch::Invoke parameters. The object's implementation of IDispatch::Invoke must then unpack the parameters, call the property or method, and handle any errors that might occur. When the property or method returns, the object passes its return value back to the controller through an IDispatch::Invoke parameter. Late BindingDISPIDs are available at run time and, in some circumstances, at compile time. At run time, controllers get DISPIDs by calling the IDispatch::GetIDsOfNames function. This is called late binding (or late name binding) because the controller binds to the property or method at run time.Late Binding is the slowest and easiest to support, and it is the only mechanism that Visual Basic version 3.0 supports. The target object must support the IDispatch OLE interface for this to work. A typelib (a repository of object property, object method, and object method parameter information) is not required for the object as IDispatch::GetIDsOfNames is first called at run time to bind the name to its DISPID and then IDispatch::Invoke is called again at run time on the target object to actually execute the property or method. As a result, there is no compile- time type checking. You get errors like "name not found" only at run time. Here's an example of Visual Basic code that implements late binding: ID BindingThe DISPID of each property or method is fixed and is part of the object's type description. If the object is described in a type library, an OLE Automation controller can read the DISPIDs from the type library at compile time, and thus avoid calling IDispatch::GetIDsOfNames. This is called ID binding. Because it requires only one call to IDispatch (that is, the call to invoke) rather than the two calls required by late binding, it is generally about twice as fast.ID binding is actually a form of early binding, as GetIDsOfNames is logically called at compile time, so a typelib is required. The target object must support the OLE IDispatch interface, so that Invoke can be called at run time on the target object. The advantages of this are that performance is better because no binding is needed at run time, you get better compile-time type checking, and you receive more informative error messages. Early BindingIn early binding (also known as VTable binding), the target object need not necessarily support IDispatch; it can support any custom interface. If the object does support IDispatch, early binding works because the object now supports a dual interface (a combination of an IDispatch and a VTable interface). Early binding does not use DISPIDs and will not work on a straight IDispatch interface implementation. A typelib for the object is required in order get the DISPIDs. A compile-time binding generates code to call any of the object's methods or properties through that object's VTable.The advantages of early binding are that you get compile-time type checking benefits and the run-time performance is better, especially when an in-process OLE Server is involved. This is because there is no need to indirect the call to a member through an implementation of IDispatch::Invoke. Basically, with VTable binding on an in-process server, function calls are direct function calls. If the OLE server is an .exe file, then it will be slower due to the remoting code that has to be accounted for. Here is an example of Visual Basic code that implements early binding: | Article Translations
|
Back to the top
