Share via


ClassWizard: Automation Support

OverviewHow Do I

ClassWizard supports the following Automation features:

  • Add classes that support Automation. This makes your application an Automation server. These classes will be exposed to Automation clients such as Microsoft Visual Basic and Microsoft Excel.

  • Add methods and properties to your classes that support Automation.

  • Create a C++ class for another Automation object on your system, such as Microsoft Excel. This makes your application an Automation client.

    You must call in InitInstance to initialize Automation for your application.

    For more information, see the article ClassWizard: Accessing Automation Servers.

An Automation class has a programming interface that other applications use to manipulate objects that your application implements. This is referred to as a "dispatch interface." For more information about dispatch interfaces, see in the Win32 SDK.

ClassWizard's Add Class button now allows you to create classes supporting Automation. Click this button on the Automation tab to display a dialog box in which you can choose to create a new class or import an existing class. The New Class dialog box appears if you create a new class so you can specify the name of your class, the base class, the filenames in which your class is implemented, and the dialog display resource for the base class, if necessary. You can also choose whether your class supports Automation, and whether to add the new class to the Gallery.

To add an Automation class

  1. On the View menu, click ClassWizard.

  2. In ClassWizard, click the Add Class button. On the list that drops down, click New. The New Class dialog box appears.

  3. Type a name for the class.

  4. Select a base class. Typically, new automation classes are derived from CCmdTarget.

  5. If you do not like the filenames specified by ClassWizard, click Change. The Change Files dialog box appears.

    Type new names for the .h and .cpp files. Alternately, you can use the Browse buttons to select files. Then click OK to return to the New Class dialog box.

  6. Click the Automation option to expose this class to Automation clients. If you click Creatable by type ID, you can specify a Type ID for this automation object. The automation client will create an object of this class using the Type ID.

  7. When you have entered all the necessary information, click OK to create the necessary code and add it to your project.

To continue defining the dispatch interface for your Automation class, see the article ClassWizard: Adding Automation Properties and Methods.

A class that is OLE Creatable allows other applications to create a stand-alone object of the class, for example by using the Visual Basic function CreateObject, and to incorporate that automation object into their application. In general, you should make only your top-level classes, such as documents, creatable from other OLE applications. Classes that are parts of these top-level classes are usually not OLE Creatable and are instead accessed from a member function in the top-level class.

For example, consider a list document, which contains a list of items. It's a good idea to make the top-level document class OLE Creatable, because this allows other applications to create a list from nothing. You then add/enumerate the items in the list, but you cannot create them as stand-alone objects because they depend on their position inside the list. For this reason, only stand-alone objects should be OLE Creatable. Note that if you selected Creatable By Type ID in AppWizard your document class is OLE Creatable. In other words, it can be accessed by automation clients.

Note   OLE insertable items are made possible by class . When you choose OLE support in AppWizard, a COleTemplateServer object manages your documents. Such documents can be created by Automation.

The following articles explain the details of using ClassWizard's OLE support: