Adding a Method to an Interface or Non-MFC Dispinterface

OverviewHow Do I ... Topics

When you use the Add Method dialog box to define a new method for an ATL or user-defined interface, ClassView defines two default method attributes for you: id and helpstring. The id attribute assigns a DISPID. The helpstring is a placeholder for text that can be used in the interface, for example in a status bar or ToolTip.

To add a method to an interface or non-MFC dispinterface

  1. First save the .idl file for the class.

    This enables the parser to correctly identify the location of all elements in the file.

  2. In ClassView, rest your cursor on the interface or dispinterface and click the right mouse button to invoke the shortcut menu.

    Note   For ATL classes, the dispinterface (event source interface) is identified with the name I<MyObject>Events.

  3. Click Add Method.

  4. If you want to change the return type for a custom interface, select from the Return Type list.

    For dual interfaces, the return type is always HRESULT.

    Note   HRESULT is the recommended return type, even for custom interfaces, since it provides a standard way to return error codes.

  5. In the Method Name box, type a name for this method (do not include a semi-colon or parentheses).

    The Implementation area reflects the default id and helpstring attributes, and the method return type.

  6. In the Parameters box, enter any parameters, separated by a comma, that you want defined for this method.

    The Implementation area reflects the parameters you enter here.

  7. Click the Attributes button to specify additional method attributes, or to modify the default attributes.

  8. In the Edit Attributes dialog box, for every attribute you wish to define:

    • Select an attribute from the Name list.

    • Specify its value (if any) by typing it into the Value text box.

      Only four attributes take a value: id, call_as, helpstring, and helpcontext. For more information, see the Attributes Definition Table.

  9. Click OK to return to the Add Method dialog box.

    The Implementation area now displays any attributes you have just specified. You can continue to add, delete, or modify attributes by returning to the Edit Attributes dialog box, until you close the Add Method dialog box.

  10. When you are satisfied with the method definition, click OK to exit the Add Method dialog box.

The method is immediately visible as an icon in ClassView, under the interface where it is defined.

By double-clicking the method icon, you can examine the .idl or .odl file to see the new method definition.

For ATL interfaces, a stub method implementation is added to the .cpp file, and a reference to the method is added to the .h file. For ATL dispinterfaces (event source interfaces), the stub functions are added to the <ObjectName>cp.h file that gets created when you implement a connection point for the object. (You must first compile the .idl file.) For more information, see .

What do you want to do?

Add a method to an MFC dispinterface

Add a property to an interface or non-MFC dispinterface

Add a method, property, or event to an MFC ActiveX control