Microsoft Windows Architecture for Developers Training Kit

The IUnknown Interface

A COM object can have multiple interfaces in order to provide different functionality for different types of clients . All COM objects support a standard interface called IUnknown. The IUnknown interface manages these other interfaces for the COM object. The IUnknown interface provides three key methods : AddRef , Release , and QueryInterface .

Figure 5.2 The IUnknown Interface

Example

This Visual Basic code example executes the AddRef and Release methods for a COM object:

'Call the AddRef method Set MyObject = CreateObject("Word.Application") 'Call the Release method Set MyObject = Nothing

The IDispatch Interface

IDispatch is an automation interface for controllers that do not use COM interfaces directly. An executable file or DLL that uses IDispatch is known as an Automation server. When accessing an object through IDispatch, late-binding is used because it occurs at run time. IDispatch supports the following key methods:

This method maps a single member name and an optional set of argument names to a corresponding set of integer dispatch identifiers (DISPIDs), which can then be used on subsequent calls to Invoke .

At run time, clients pass the string name of the property or method they want to call into the IDispatch . GetIDsOfNames method. If the property or method exists on the object, the dispatch ID of the corresponding function is returned to the client. The dispatch ID can then be used to invoke the method or property using the IDispatch.Invoke method. The functions GetTypeInfoCount and GetTypeInfo obtain information from the component's type library about the interfaces, methods, and properties that it supports.

Example

This example uses the CreateObject function in a Set statement to create a new object in Visual Basic that utilizes late binding and the IDispatch interface.

Dim MyObject as Object Set MyObject = CreateObject("Word.Application")

Because a generic object variable is used, late binding occurs. This late binding technique allows you to use the same variable for different objects or for objects that only support late binding.

Virtual Tables

Automation allows an ActiveX client to call a method or property function directly. This approach, called virtual table (VTBL) binding , does not use the IDispatch interface. Using the virtual table is known as early binding in Visual Basic. Early binding requires type information provided in the form of a type library. A client obtains type information from the type library at compile time and then calls the methods and functions directly. This type information allows Visual Basic to perform compile-time syntax and type checking. At run time, this type of binding is faster, because the memory location of the Automation server is already known, the data types and syntax have already been verified , and the access is direct because no calls are made through IDispatch.

Clients use pointers to reference interface instances, obtaining them either when the object is instantiated or by querying the object. An interface pointer points to an array of function pointers known as a virtual table (VTBL). The functions that are pointed to by the members of the VTBL are called the methods, or member functions, of the interface. By convention, interface names begin with an "I."

Figure 5.3 Using virtual tables

The VTBL lists the addresses of all the properties and methods that are members of an object, including the member functions of the interfaces that it supports. The first three members of the VTBL are the members of the IUnknown interface. Subsequent entries are members of the other supported interfaces. OLE provides a number of useful interfaces (which generally begin with "IOle"), but because anyone can define custom interfaces, you can implement your own interfaces as you deploy component-based applications.

Категории