Create a COM interface

Visual Studio provides wizards and templates to create projects that use COM defining interfaces and dispinterfaces for your COM objects and automation classes.

You can use these wizards to do the following three common tasks:

  • Add ATL support to your MFC project.

    Add ATL support to an MFC application after you create an MFC project using the MFC application wizard and then running the Add ATL Support to MFC code wizard. This support applies only to simple COM objects added to an MFC executable or DLL project. These ATL objects may have more than one interface.

  • Create an MFC ActiveX control.

    Open the MFC ActiveX control wizard to create an ActiveX control with a dispinterface and an event map defined in the .idl file and the control class, respectively.

  • Add an ATL control.

    Use a combination of the ATL project wizard and the ATL control wizard to create an ATL ActiveX control.

    You can also add an ATL control to an MFC project to which you've added ATL support, as described above. Additionally, if you select ATL Control in the Add Class dialog box, and you haven't yet added ATL support to your MFC project, Visual Studio displays a dialog box that confirms adding ATL support to your MFC project.

    This wizard generates IDL source and a COM map in the project classes.

Once you've an ATL project open, the Add Class dialog box gives you the choice of additional wizards and templates to add COM interfaces to your project. The following wizards allow you to establish one or more interfaces for the object:

Additionally, you can implement new interfaces on your COM control. Just right-click the object's control class in Class View and choose Implement Interface.

Note

Visual Studio doesn't provide a wizard to add an interface to a project. You can add an interface to an ATL project or to an Add ATL support to your MFC project by adding a simple object using the ATL simple object wizard. Alternately, open the project's .idl file and create the interface by typing:

interface IMyInterface {
};

For more information, see Implement an interface and Add objects and controls to an ATL project.

Visual C++ provides several ways to view and edit the COM interfaces defined for your projects. Class View displays icons for any interface or dispinterface defined in an .idl file in your C++ project.

For ATL-based COM object classes, Class View reads the COM map in the ATL class to display the relationship between the ATL class and any interfaces it implements.

In Class View and its shortcut menus, you can work with interfaces as follows:

  • Add ATL objects to an MFC-based application.
  • Add methods, properties, and events.
  • Jump directly to an item's interface code by double-clicking the item.

In this section

Edit a COM interface

By using commands from the Class View shortcut menu, you can define new methods and properties for the COM interfaces in your Visual Studio C++ projects. From the Toolbox, you can also define events for ActiveX controls.

For ATL- and MFC-based COM object classes, you can edit the class implementation at the same time that you edit the interface.

Note

For interfaces that you've defined outside of the Add Class dialog box, Visual C++ adds the methods or properties to the .idl file, and it add stubs to the classes that implement methods, even when the interfaces are added manually.

The following three wizards help you customize existing interfaces. They're available from Class View:

Wizard Project type
Add property wizard ATL or MFC projects supporting ATL. Right-click the interface to which you want to add the property.

Visual C++ detects the project type and modifies the options in the Add Property Wizard as necessary:

- For dispinterfaces in projects created by using the MFC application wizard, invoking the Add Property Wizard provides options specific to MFC.
- For MFC ActiveX control interfaces, the Add Property Wizard provides a list of stock methods and properties that you can use as provided or customize for your control.
- For all other interfaces, the Add Property Wizards provide options useful in most situations.
Add method wizard ATL or MFC projects supporting ATL. Right-click the interface to which you want to add the method.

Visual C++ detects the project type and modifies the options in the Add Method Wizard as necessary:

- For dispinterfaces in projects created by using the MFC application wizard, using the Add Method Wizard provides options specific to MFC.
- For MFC ActiveX control interfaces, the Add Method Wizard provides a list of stock methods and properties that you can use as provided or customize for your control.
- For all other interfaces, the Add Method wizards provide options useful in most situations.

Additionally, you can implement new interfaces on your COM control. Just right-click the object's control class in Class View and choose Implement Interface.