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
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
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