FAQ 34.31 What are dual interfaces?

graphics/new_icon.gif

A great way to have your cake and eat it, too.

A dual interface is an interface that combines a dispatch interface and a direct vtable interface.

One advantage of dual interfaces is that they let the caller choose the binding mechanism it wants to use. Scripting languages can use the dispatch interface while C++ callers can use the direct vtable interface. This means that objects that export dual interfaces are more useful since they can be used by a wider range of callers. IDispatch offers support for late binding, whereas vtable binding offers much higher performance because the method is called directly instead of going through IDispatch::Invoke.

IDL supports the definition of dual interfaces. This is done by deriving the interface from IDispatch and attaching the [dual] attribute to the interface statement (notice that the dispinterface statement is not used). Here is the IDL declaration of IStack suitable for a dual interface.

 [ uuid(FC3B3F51-BCED-11D1-91FE-E1CBED988F66) ] library StackLib {     importlib("stdole32.tlb");     [   object, dual,         uuid(FC3B3F51-BCEC-11D1-91FE-E1CBED988F66)     ]     interface IStack : IDispatch     {         [id(1)] HRESULT push([in] long value);         [id(2)] HRESULT pop([out, retval] long* value);         [id(3)] HRESULT empty([out, retval] long* pVal);     }; }; 

Since dual interfaces are COM interfaces, they must abide by the rules for COM interfaces.

  • A dual interface must have an IID, and it is declared using the [uuid] attribute.

  • The return type for a method is HRESULT, and all other results need to be returned through the parameter list (see pop and empty).

Since dual interfaces are dispatch interfaces, they must abide by the rules for dispatch interfaces.

  • The declarations for the IDispatch interface are loaded from the Automation type library using the statement importlib("stdole32.tlb");.

  • Each method has a DISPID, which is assigned using the [id] attribute.

  • The types of the parameters for each method must be Automation- compatible data types.

  • Dual interfaces are able to use type library marshaling (see FAQ 34.29) so that the interface can be accessed across process boundaries or across machine boundaries automatically (this is not automatically true for all custom-defined vtable interfaces). This is done using the type library for the interface in conjunction with a generic universal proxy/stub implementation.

A dual interface inherits from IDispatch and also implicitly inherits from IUnknown (since IDispatch inherits from IUnknown). At a binary level, the first three entries in the vtable are the members of IUnknown, the next four entries are the members of IDispatch, and the subsequent entries are pointers to the custom methods of the dual interface. Thus Automation controllers can access the IDispatch interface (including GetIDsOfNames and Invoke) since the entries for the dispatch interface are at the same locations in the vtable whether or not this is a dual interface. Other callers who are aware of dual interfaces can bind directly to the custom methods in the COM interface. Information about the physical layout of the vtable is obtained from a type library or a header file.

Direct vtable binding can be two to five times faster than Automation binding for in-process calls.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

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