|
COM Binding Tutorial
Early Binding, Late Binding, Interfaces, Oh My!The method you choose to bind to your external objects can make a huge difference on code size and execution speed. Naturally, early binding is much faster then late binding, but did you know that there is different ways to Early Bind? Did you realize one way is almost as slow as Late Binding? Well this tutorial will help you to understand the various ways to connect to external objects. InterfacesFirst of all, before you can understand binding, we should take a brief look at the COM interfaces and how they interact with your program. All COM objects must expose the Interface IUnknown, IUnknown is the base interface for all COM interfaces exposed, and reside behind IUnknown in the interface hierarchy. IUnknown manages the lifespan of the COM object and handles client requests for interface access. To manage the COM object's lifespan, IUnknown maintains what is known as a reference count. The reference count is a tally of the number of active clients of a particular interface. Only when all of the reference counts for all of the interfaces exposed by a COM object reach zero, can the object be deleted and its memory safely freed. IUnknown.QueryInterface, is used to handle client access to COM objects. Clients gain access to a COM object by retrieving a pointer to one of the interfaces implemented by the object. To gain access to a particular interface, a client calls QueryInterface and sets the riid parameter to the identifier of the desired interface. So, in other words, the client calles QueryInterface to ask an object about the features it supports, and ask for pointers to specific interfaces. A large benefit of QueryInterface, is that without successfully calling QueryInterface first, you cannot possibly ask an object to perform any operation expressed through any interface. That is, in order to call an interface member function, you have to have a pointer to that interface. The only way to obtain such a pointer is by calling QueryInterface or by calling a creation function, which implicitly calls QueryInterface. If the object doesn't support the interface you request, it returns a NULL pointer, and you cannot make calls through a NULL pointer. Therefore, the object is always protected from malignant clients who think that they can bully objects into doing things the objects are not capable of doing. Late BindingNow that you understand how IUnknown works, let’s look at how Late Binding effect your application. Consider the following Late Binding Code: Early BindingEarly Binding is the exact opposite of Late Binding, you tell the compiler exactly what to expect before compiling. You create a reference to the ProgID by setting a reference in the Project References menu. Consider the following Early Binding Code: K&K Consulting's VB Guru, June 2000
|
Send mail to WebMaster with
questions or comments about this web site.
|