Condividi tramite


Creating a COM Interface (Visual C++)

 

The latest version of this topic can be found at Creating a COM Interface (Visual C++).

Visual C++ 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 perform the following three common tasks:

  • Adding 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 multiple interfaces.

  • Creating 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.

  • Adding 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 have added ATL support, as described above. Additionally, if you select ATL Control in the Add Class dialog box, and you have not yet added ATL support to your MFC project, Visual Studio displays a dialog box confirming adding ATL support to your MFC project.

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

Once you have 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 by right-clicking the object's control class in Class View and clicking Implement Interface.

Note

Visual Studio does not provide a wizard to add an interface to a project. You can add an interface to an ATL project or to an Adding 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 {  
};  
  

See Implementing an Interface and Adding Objects and Controls to an ATL Project for more information.

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.

See Also

Creating Desktop Projects By Using Application Wizards
Adding Functionality with Code Wizards