Pablo Galiano : Wednesday, August 27, 2008 - Posts

Subscriptions

<September 2010>
SuMoTuWeThFrSa
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

News

Subscribe to Pablo Galiano by Email

Post Categories

Wednesday, August 27, 2008 - Posts

How do I get a Project from a IVsHierarchy and viceversa

My thirteenth How do I is up.

Scenario

This is a common thing when working with the EnvDTE and Shell Interop worlds. Sometimes we want to access properties that doesn't exist in one of the worlds (like the guid property of my previous How do I) and viceversa. The focus of this How do I tackles this scenario.

 

Interfaces and classes needed

 

Code snippet

    public Project GetProject(IVsHierarchy hierarchy)

    {

        object project;

 

        ErrorHandler.ThrowOnFailure

            (hierarchy.GetProperty(

                VSConstants.VSITEMID_ROOT,

                (int)__VSHPROPID.VSHPROPID_ExtObject,

                out project));

 

        return (project as Project);

    }

 

    public IVsHierarchy GetHierarchy(IServiceProvider serviceProvider, Project project)

    {

        var solution =

            serviceProvider.GetService(typeof(SVsSolution)) as IVsSolution;

 

        IVsHierarchy hierarchy;

 

        solution.GetProjectOfUniqueName(project.FullName, out hierarchy);

 

        return hierarchy;

    }

Assemblies needed

  • Microsoft.VisualStudio.Shell.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
  • EnvDTE, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

 

Stay tuned,

Pablo

posted Wednesday, August 27, 2008 8:57 AM by pga with 1 Comments

How do I obtain a project GUID

My twelfth How do I is up.

Scenario

Let's say that we have a DTE project instance and we want to get the project GUID. If we look carefully the EnvDTE.Project interface doesn't have a guid property. To obtain it we need to first obtain the corresponding IVsHierarchy for the project and then query the guid property. The focus of this How do I tackles this scenario.

 

Interfaces and classes needed

 

Code snippet

    public Guid GetProjectGuid(IServiceProvider serviceProvider, Project project)

    {

        var solution = serviceProvider.GetService(typeof(SVsSolution)) as IVsSolution;

 

        IVsHierarchy hierarchy;

 

        solution.GetProjectOfUniqueName(project.FullName, out hierarchy);

 

        if(hierarchy != null)

        {

            Guid projectGuid;

 

            ErrorHandler.ThrowOnFailure(

                hierarchy.GetGuidProperty(

                    VSConstants.VSITEMID_ROOT,

                    (int)__VSHPROPID.VSHPROPID_ProjectIDGuid,

                    out projectGuid));

 

            if(projectGuid != null)

            {

                return projectGuid;

            }

        }

 

        return Guid.Empty;

    }

Assemblies needed

  • Microsoft.VisualStudio.Shell.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

 

Stay tuned,

Pablo

posted Wednesday, August 27, 2008 6:07 AM by pga with 1822 Comments

How do I get the Visual Studio command line switches

My eleventh How do I is up.

Scenario

Let's say that we want to obtain the command line switches for the running Visual Studio instance. In this specific sample we want to know if Visual Studio is running in RANU mode. The focus of this How do I tackles this scenario.

Interfaces and classes needed

Code snippet

    public bool IsInRANUMode(IServiceProvider serviceProvider)

    {

        IVsAppCommandLine cmdLineService = serviceProvider.GetService(typeof(SVsAppCommandLine)) as IVsAppCommandLine;

        int present;

        string value;

 

        ErrorHandler.ThrowOnFailure(cmdLineService.GetOption("ranu", out present, out value));

 

        return Convert.ToBoolean(present);

    }

 

Assemblies needed

  • Microsoft.VisualStudio.Shell.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

Stay tuned,

Pablo

posted Wednesday, August 27, 2008 5:20 AM by pga with 1 Comments