How to access current ProjectNode

Feb 4, 2011 at 7:26 AM
Edited Feb 14, 2011 at 2:14 PM

When you integrate a button in the IDE, e.g., in the context menu of the project, how do you determine the selected projectnode in the MenuItemCallback method? I am using a static List<ProjectNode> in the ProjectPackage and do some string compare with the properties I can extract from the the IVsHierarchy com object. Seems stupid, so is there a better way?

[openProjects is a static list maintained by the ProjectNode class, the following code is within my ProjectPackage]

private void MenuItemCallback(object sender, EventArgs e)
    OleMenuCommand menuCommand = sender as OleMenuCommand;

    if (menuCommand != null)
        IntPtr hierarchyPtr, selectionContainerPtr;
        uint projectItemId;
        IVsMultiItemSelect mis;
        IVsMonitorSelection monitorSelection = (IVsMonitorSelection)Package.GetGlobalService(typeof(SVsShellMonitorSelection));
        monitorSelection.GetCurrentSelection(out hierarchyPtr, out projectItemId, out mis, out selectionContainerPtr);
        IVsHierarchy hierarchy = Marshal.GetTypedObjectForIUnknown(hierarchyPtr, typeof(IVsHierarchy)) as IVsHierarchy;

        if (hierarchy != null)
            object projName;
            hierarchy.GetProperty(projectItemId, (int)__VSHPROPID.VSHPROPID_ProjectName, out projName);
            object projPath;
            hierarchy.GetProperty(projectItemId, (int)__VSHPROPID.VSHPROPID_ProjectDir, out projPath);
            String IVPath = Path.Combine((String) projPath, (String) projName + ".myextension");
            foreach(MyProjectNode hpn in openProjects) {
                if (hpn.Url.Equals(IVPath, StringComparison.InvariantCultureIgnoreCase))
                    // do something with the selected node

Feb 15, 2011 at 1:29 PM

Okay, I see that the code in ProjectNode::GetSelectedNodes() does the trick and is much nicer. Still it requires a static variable to access a projectNode. Any better way than that?