Search code examples
c#visual-studiovisual-studio-2019vsix

Getting Nested Hierarchy returns invalid status code


Basically I am trying to get the nested hierarchy of a folder in a project. Down below you can see a simplified version of my code, where I take the first node of the Hierarchy which is a folder.

CurrentHierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_FirstVisibleChild, out var tempNode);

if (IsFolder)
{
    var guid = typeof(IVsHierarchy).GUID;

    var result = CurrentHierarchy.GetNestedHierarchy((uint)(int)tempNode, ref guid, out var hierarchyObjectPointer, out var nested); // Returns invalid status code

    var hierarchy = (IVsHierarchy)Marshal.GetObjectForIUnknown(hierarchyObjectPointer);
    Marshal.Release(hierarchyObjectPointer);
}

Anyhow the variable result contains the value -2147483648 or int.MinValue, which is of course not valid. I wonder what I am doing wrong here. I looked at a couple of web pages: GitHub 1, GitHub 2 and Hot Examples, but they seem to be the same what I am doing.

Note that the CurrentHierarchy represents the projects hierarchy and IsFolder's value is actually correct e.g. The node is actually a folder.


Edit

After digging some more around I noticed that GetNestedHierarchy is actually returning VSConstants.E_FAIL which means:

If the requested interface is not supported on the hierarchy object, Microsoft.VisualStudio.VSConstants.E_NOINTERFACE is returned. The caller would then treat this node as if it had no children, if the requested interface had been essential (as is often the case when the requested interface is IID_IVsUIHierarchy).

Which even confuses me more, because it the node I am passing to the method is indeed a folder.


Edit 2

It seems like the code works fine, if the IVsHierarchy represents an IVsSolution, but as soon as I try to get the nested hierarchy of a nested hierarchy, it won't work anymore.


Solution

  • This whole thing is actually a lot easier than first expected. I actually miss understood the the use of GetNestedHierarchy and the whole principle overall. So let me explain for everyone else.

    At first glance you might think that a folder and any node that is expandable is a Hierarchy, but no!

    What is a Hierarchy even then? Actually from my tests a hierarchy is only a Solution and a Project and nothing else. So if you want to get an item in a folder of project you'll need to get each child by calling GetProperty over and over.

    hierarchy.GetProperty(folderItemId, (int)__VSHPROPID.VSHPROPID_FirstChild, out var folderItem);
    hierarchy.GetProperty(folderItem, (int)__VSHPROPID.VSHPROPID_FirstChild, out var folderItemItem);
    

    Basically the project IVsHierarchy hosts all the items in the project.