Platform SDK: Active Directory, ADSI, and Directory Services

Context Menu Extension Tutorial

Using the conventional MMC extension snap-in mechanisms, only the All Tasks and Create New context menus can be extended.

  1. Register the node type for the object class whose context menu you want to extend.

    Note that the node type that you want the snap-in to extend must be registered in the MMC registry key. For more information on registering a node type for nodes (objects of a specific class) in Active Directory Manager, see Registering Node Types for Active Directory Manager.

  2. Create a new project using the ATL COM AppWizard with the server type Dynamic Link Library (DLL).

    For this tutorial, type MyExtensionSnapin as the Project Name.

    To create a new project, choose New from the File menu, select the Projects tab, select ATL COM AppWizard, type the project name in the Project Name box, and click OK. The ATL COM AppWizard page appears. Select Dynamic Link Library (DLL) and click Finish. For this tutorial, do not select the other options in wizard page. The New Project Information dialog box appears. Click OK.

  3. Add an MMC Snapin object by choosing New ATL Object from the Insert menu.
  4. In the ATL Object Wizard dialog box, select Objects from the Category list and MMC SnapIn from the Objects list. Click Next.
  5. In the ATL Object Wizard Properties dialog box, select the Names tab and type the control name in the Short Name box. The other boxes are filled in based on this name. For this tutorial, type MyContextMenu in the Short Name box.
  6. Select the MMC Snapin tab and make the following settings:
    1. Check the Extension box, click on the browse (…) button for Extends Node, and select the node type from the Snap-In Nodes dialog box. For this tutorial, select BF967ABA-0DE6-11D0-A285-00AA003049E2 in the Snap-In Nodes dialog box so that this snap-in extends the user class.

      Note that the node type that you want the snap-in to extend must be registered in the MMC registry key. For more information on registering a node type for nodes (objects of a specific class) in Active Directory Manager, see Registering Node Types for Active Directory Manager.

    2. Make sure that the IComponentData, IComponent, ISnapInAbout, and IExtendContextMenu are checked. If you want to add a property sheet or control bar, check those options as well. For this tutorial, just select the specified four items: IComponentData, IComponent, ISnapInAbout, and IExtendContextMenu.
    3. Clear the Supports Persistence box. If you want to save the state of your snap-in, you can use the persistence interfaces. For this tutorial, clear the Supports Persistence box.
  7. Click OK. The CMyContextMenuExtData class is created. This class implements the extension snap-in. A menu resource is also created.

    Note that the following classes are also created: CMyContextMenuData (implements IComponentData, which interacts with scope pane), CMyContextMenuComponent (implements IComponentData, which interacts with result pane), and CMyContextMenuAbout (implements ISnapinAbout, which controls About box information and some information in the Add/Remove Snap-ins dialog box).

  8. Open the menu resource by clicking on the Resource tab, expanding the MyExtensionSnapin resources folder, expanding the Menu folder, and double-clicking the menu resource IDR_MYCONTEXTMENU_MENU. The menu has four menu items: TOP, NEW, TASK, and VIEW.

    Note that an extension snap-in can only add menu items to the NEW and TASK menus.

  9. Add a menu item to the TASK menu by clicking on TASK and double-clicking on the empty menu item that appears beneath TASK. The Menu Item Properties box appears.
  10. In the Caption box, type the text for the menu item.

    For this tutorial, type the following: Do Some Extension Thing.

  11. In the Prompt box, type the description for the menu item that you want to display in the status bar when the menu item is selected.

    For this tutorial, type the following: Does some extension thing.

  12. 12. In the ID box, type the command ID for the menu item. You can leave this blank if you want an ID to be automatically generated.

    For this tutorial, type the following: ID_DO_SOMETHING.

  13. Close the Menu Item Properties dialog box.
  14. Open the header file containing the class definition of CMyExtensionSnapinExtData. Click the ClassView tab and then double-click CMyExtensionSnapinExtData.
  15. Find the following two lines:
    BEGIN_SNAPINCOMMAND_MAP(CMyExtensionSnapinExtData ExtData, FALSE)
    END_SNAPINCOMMAND_MAP() 
  16. Change the FALSE parameter to TRUE to indicate that the snap-in is an extension. The line should look like this:
    BEGIN_SNAPINCOMMAND_MAP(CMyExtensionSnapinExtData, TRUE) 
  17. Add the SNAPINCOMMAND_ENTRY macro between the two lines and specify the command ID for the menu item as the first parameter and the name of the method that serves as the command handler for the command ID as the second. The snap-in command map should now look like this:
    BEGIN_SNAPINCOMMAND_MAP(CMyExtensionSnapinExtData, TRUE)
        SNAPINCOMMAND_ENTRY(ID_DO_SOMETHING, OnDoSomeThing)
    END_SNAPINCOMMAND_MAP() 
  18. Add the command handler as a method in the class. The command handler must take the following form:
    HRESULT MethodName (bool& bHandled,
                  CSnapInObjectRootBase* pObj)

    For this tutorial, add the following method to the class:

    HRESULT OnDoSomeThing (bool& bHandled,
        CSnapInObjectRootBase* pObj)
    {
        ::MessageBoxW( NULL, L" OnDoSomeThing ", L" CMyExtensionSnapinExtData ", MB_OK);
        return S_OK;
    } 
  19. Build the project.
  20. Load the Active Directory Manager snap-in into MMC or open the DSA.MSC console file. Open the Active Directory Manager, then the domain node, then the Users container, right-click on a user such as Administrator, select All Tasks. The menu item Do Some Extension Thing should appear. Choose the menu item. The OnDoSomeThing method is called and the message box appears.