COM Interfaces

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 .

click to view at full size.

Figure 5.2 The IUnknown Interface
  • AddRef

    This method provides the actual creation of the COM object. As each instance of the object is created, the usage count for the object is incremented. In Visual Basic, the AddRef method is invoked when using the CreateObject function.

  • Release

    This method is used to destroy an object and subtract from the usage count. In Visual Basic this can be done by setting an object to Nothing .

  • QueryInterface

    This method provides a list of interfaces that the object supports. For example, a COM object could have separate interfaces for an employee and for a manager (IEmployee and IManager). When a client contains an object variable that points to a valid COM interface, it can query any interfaces provided by the object with this method. Visual Basic provides this capability automatically through the Set statement.

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:

  • GetIDsOfNames
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 .
  • GetTypeInfo
  • This method retrieves the type information for an object.

  • GetTypeInfoCount
  • This method retrieves the number of type information interfaces that an object provides (0 if no type information is available and 1 if there is).

  • Invoke
  • This method provides access to properties and methods exposed by an object.

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."

click to view at full size.

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.



Microsoft Windows Architecture Training
Microsoft Windows Architecture for Developers Training Kit
ISBN: B00007FY9D
EAN: N/A
Year: 1998
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net