Controlling an Automation Server Using a Dispatch Interface

From RAD Studio
Jump to: navigation, search

Go Up to Writing Client Code Based On Type Library Definitions

Typically, you use the dual interface to control the Automation server. However, you may find a need to control an Automation server with a dispatch interface because no dual interface is available.

To call the methods of a dispatch interface,

  1. Connect to the server, using the global CreateOleObject function.
  2. Use the as operator to cast the IDispatch interface returned by CreateOleObject to the dispinterface for the CoClass. This dispinterface type is declared in the TypeLibName_TLB unit.
  3. Control the Automation server by calling methods of the dispinterface.Another way to use dispatch interfaces is to assign them to a Variant. By assigning the interface returned by CreateOleObject to a Variant, you can take advantage of the Variant type's built-in support for interfaces. Simply call the methods of the interface, and the Variant automatically handles all IDispatch calls, fetching the dispatch ID and invoking the appropriate method. The Variant type includes built-in support for calling dispatch interfaces, through its var.
  V: Variant;
  V:= CreateOleObject("TheServerObject");
  V.MethodName; { calls the specified method }

An advantage of using Variants is that you do not need to import the type library, because Variants use only the standard IDispatch methods to call the server. The trade-off is that Variants are slower, because they use dynamic binding at run time.

See Also