Automation makes it possible for one application to manipulate objects implemented in another application, or to expose objects so they can be manipulated.
Index (in this page)
What is OLE Automation?
A Microsoft technology that enables applications to expose their functionality to other applications. Automation, formerly known as OLE Automation, is based on the Component Object Model (COM) and allows run-time binding of components. Automation is used exclusively by scripting languages, such as Microsoft Visual Basic for Applications (VBA), Microsoft Visual Basic Scripting Edition (VBScript), and Microsoft JScript, to access COM components that support Automation.
The advantage of Automation is that it allows various languages to access COM components at run time. The drawbacks to Automation are that it is slow and that compile-time data type checking cannot be performed.
An application that exposes its functionality through Automation is called an Automation server. An application that communicates with the server through Automation is called an Automation client.
Automation Server
An Automation server is an application that exposes programmable objects (called Automation objects) to other applications (called Automation clients). Automation servers are sometimes called Automation components.
Exposing Automation objects enables clients to automate certain procedures by directly accessing the objects and functionality the server makes available. Exposing objects this way is beneficial when applications provide functionality that is useful for other applications. For example, a word processor might expose its spell-checking functionality so that other programs can use it. Exposure of objects thus enables vendors to improve their applications’ functionality by using the ready-made functionality of other applications.
These Automation objects have properties and methods as their external interface. Properties are named attributes of the Automation object. Properties are like the data members of a C++ class. Methods are functions that work on Automation objects. Methods are like the public member functions of a C++ class.
Example of Automation Server
Here’s an example of an Automation server: a word processing program that can expose its spell-checking functions so that Automation controllers can access them. This allows the functionality of one program (the Automation server) to be used by other programs (the Automation clients or controllers).
Support for Automation Servers
Visual C++ and the MFC framework provide extensive support for Automation servers. They handle much of the overhead involved in making an Automation server, so you can focus your efforts on the functionality of your application.
The framework’s principal mechanism for supporting Automation is the dispatch map, a set of macros that expands into the declarations and calls needed to expose methods and properties for OLE. A typical dispatch map looks like this:C++Copy
BEGIN_DISPATCH_MAP(CMyServerDoc, COleServerDoc)
DISP_PROPERTY(CMyServerDoc, "Msg", m_strMsg, VT_BSTR)
DISP_FUNCTION(CMyServerDoc, "SetDirty", SetDirty, VT_EMPTY, VTS_I4)
END_DISPATCH_MAP()
The Class Wizard and Class View assist in maintaining dispatch maps. When you add a new method or property to a class, Visual Studio adds a corresponding DISP_FUNCTION
or DISP_PROPERTY
macro with parameters indicating the class name, external and internal names of the method or property, and data types.
The Add Class dialog box also simplifies the declaration of Automation classes and the management of their properties and operations. When you use the Add Class dialog box to add a class to your project, you specify its base class. If the base class allows Automation, the Add Class dialog box displays controls you use to specify whether the new class should support Automation, whether it is “OLE creatable” (that is, whether objects of the class can be created on a request from a COM client), and the external name for the COM client to use.
The Add Class dialog box then creates a class declaration, including the appropriate macros for the OLE features you have specified. It also adds the skeleton code for implementation of your class’s member functions.
The MFC Application Wizard simplifies the steps involved in getting your automation server application off the ground. If you select the Automation check box from the Advanced Features page, the MFC Application Wizard adds to your application’s InitInstance
function the calls required to register your Automation objects and run your application as an Automation server.
Automation Client
Automation Client is a client that accesses the functionality of an Automation server. Automation (OLE Automation) is a way for one application to manipulate the exposed objects (properties and methods) of another application.
Automation controllers are client applications that can manipulate the exposed objects of another application called an Automation server. Examples of Automation controllers include Microsoft Word, Microsoft Excel, and Microsoft Visual Basic.
There are two kinds of Automation clients:
- Clients that have static information about the properties and methods of the Automation server bound to them at compile time.
- Clients that dynamically obtain information about the properties and methods of the Automation server at run time. These clients do this by querying the IDispatch interface of the Automation server.
Clients of the second kind acquire information about the server’s methods and properties by querying the OLE system’s IDispatch
mechanism. Although it is adequate to use for dynamic clients, IDispatch
is difficult to use for static clients, where the objects being driven must be known at compile time. For static bound clients, the Microsoft Foundation classes provide the COleDispatchDriver class.
Static bound clients use a proxy class that is statically linked with the client application. This class provides a type-safe C++ encapsulation of the server application’s properties and operations.
The class COleDispatchDriver
provides the principal support for the client side of Automation. Using the Add New Item dialog box, you create a class derived from COleDispatchDriver
.
You then specify the type-library file describing the properties and functions of the server application’s object. The Add Item dialog box reads this file and creates the COleDispatchDriver
-derived class, with member functions that your application can call to access the server application’s objects in C++ in a type-safe manner. Additional functionality inherited from COleDispatchDriver
simplifies the process of calling the proper Automation server.
Handling Events in Automation Clients
If you want to handle events in your automation client, you need to add a sink interface. MFC provides wizard support to add sink interfaces for ActiveX controls, but not support for other COM servers.
Automation Clients: Using Type Libraries
Automation clients must have information about server objects’ properties and methods if the clients are to manipulate the servers’ objects. Properties have data types; methods often return values and accept parameters. The client requires information about the data types of all of these in order to statically bind to the server object type.
This type information can be made known in several ways. The recommended way is to create a type library.
For information on MkTypLib, see the Windows SDK.
Visual C++ can read a type-library file and create a dispatch class derived from COleDispatchDriver. An object of that class has properties and operations duplicating those of the server object. Your application calls this object’s properties and operations, and functionality inherited from COleDispatchDriver
routes these calls to the OLE system, which in turn routes them to the server object.
Visual C++ automatically maintains this type-library file for you if you chose to include Automation when the project was created. As part of each build, the .tlb file will be built with MkTypLib.
How to create a dispatch class from a type-library (.tlb) file
- In either Class View or Solution Explorer, right-click the project and click Add and then click Add Class on the shortcut menu.
- In the Add Class dialog box, select the Visual C++/MFC folder in the left pane. Select the MFC Class From TypeLib icon from the right pane and click Open.
- In the Add Class From Typelib Wizard dialog box, select a type library from the Available type libraries drop-down list. The Interfaces box displays the interfaces available for the selected type library. To select interfaces, double-click them or click the Add button. When you do so, names for the dispatch classes will appear in the Generated classes box. You can edit the class names in the
Class
box. The File box displays the file in which the class will be declared. (you can edit this file name as well). You can also use the browse button to select other files, if you prefer to have the header and implementation information written in existing files or in a directory other than the project directory. - Click Finish.The wizard will then write the code for your dispatch classes using the specified class and file names.