Visual Studio 2015 Extension - How to get workspace of current Solution - visual-studio

I am developing an extension to Visual Studio 2015. How do I get workspace object for the current solution loaded in the IDE so that my extension can work on it?
A lot of samples seem to load a project or a solution as below, however, I want to get the workspace of the Solution loaded in the IDE so that my extension can access to it.
Dim workspace = New AdhocWorkspace()
Dim solution = workspace.CurrentSolution
Dim project = solution.AddProject("projectName", "assemblyName", LanguageNames.VisualBasic)
Dim document = project.AddDocument("name.vb", "...some code")

Roslyn defines several types of workspaces but the one you are interested in is a VisualStudioWorkspace.
You can get to it via MEF from the constructor of your vsix:
[ImportingConstructor]
public Ctor([Import(typeof(SVsServiceProvider), AllowDefault = true)] IServiceProvider vsServiceProvider, [Import(typeof(VisualStudioWorkspace), AllowDefault = true)] Workspace vsWorkspace)
Or by using the Component Service:
IComponentModel componentModel = this.serviceProvider.GetService(typeof(SComponentModel)) as IComponentModel;
var workspace = componentModel.GetService<Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace>();
You may find this question and this question useful as well.

Related

How to see the value of a parameter inside a With-Block while debugging?

When debugging in a Visual Basic Project in Visual Studio 2019, I can't see the values of parameters inside a With-Block when I'm hovering over it with the mouse.
I have Resharper Ultimate installed, but it seems like it doesn't provide a function to show it either.
When using a With-Block the values of ".Name", ".URL", etc.
aren't shown when hovering over them in debug mode:
Private Sub AddCustomer()
Dim theCustomer As New Customer
With theCustomer
.Name = "Coho Vineyard"
.URL = "http://www.cohovineyard.com/"
.City = "Redmond"
End With
With theCustomer.Comments
.Add("First comment.")
.Add("Second comment.")
End With
End Sub
When it is this way, the debugger shows the values just as usual:
Private Sub AddCustomer()
Dim theCustomer As New Customer
theCustomer.Name = "Coho Vineyard"
theCustomer.URL = "http://www.cohovineyard.com/"
theCustomer.City = "Redmond"
theCustomer.Comments.Add("First comment.")
theCustomer.Comments.Add("Second comment.")
End Sub
How can I see the values? Or is there a way to convert the With-Blocks automatically to regular expressions?
Works for me:
Are you sure that you are talking about VB6? Resharper Ultimate sounds more like an extension for Visual Studio.
I can't see the values of parameters inside a With-Block when I'm
hovering over it with the mouse.
Do you use DataTips?
I test it in VS2019 16.1(Community and Professional Edition). In a VB.net console app using your example code, we can use DataTips to monitor variable value in debug mode.
Hover over the thecustomer variable and we can get its details during debugging.If this option not work in your side, try repairing VS or update it to latest VS version.
Hope it helps:)

Visual Studio custom wizard - add additional configuration

I am writing a visual studio custom wizard for creating C++ project.
I need to define additional build configuration, that inherits the debug configuration.
I googled a lot, but couldn't find anything.
I guess this should be done in the JScript file (default.js), AddConfig function, by calling proj.Object.AddConfiguration. But I couldn't find examples, nor syntax rules.
The only thing I found is : http://www.codeproject.com/Articles/200039/A-Visual-Studio-Wizard-to-add-more-project-configu but it is way too complicated, and I couldn't figure it out.
Can you please help?
Found it. It can be done using C# code:
solution.SolutionBuild.SolutionConfigurations.Add("Conf", "Debug", true);
//set Conf to be active build configuration
solution.SolutionBuild.SolutionConfigurations.Item("Conf").Activate();
//Get project
VCProject pro = solution.Projects.Item(1).Object;
//Get comiler tool for project
VCCLCompilerTool tool = pro.Configurations.item("Conf").Tools("VCCLCompilerTool");
//set Prprocessor definition for Conf
tool.PreprocessorDefinitions = "NEW";

How do I programmatically refresh/reload a VS project after modifying the underlying file?

I am developing a Visual Studio package and I have written some code that will make a file in Solution Explorer dependant upon another file.
What this means is that it gives them the same relationship as code-behind files or designer files, where they appear nested under the parent file with a plus/minus icon.
+ MainForm.cs
- MainForm.cs
MainForm.Designer.cs
MainForm.resx
The code that I have written successfully and correctly modifies the underlying project file, however the change is not reflected in Solution Explorer until the project is closed and re-opened.
I'm looking for some code that will refresh or reload the project so that the change is visible in Solution Explorer immediately.
Further Information...
Here is the sudo code that demonstrates the mechanism by which I create the dependant file.
IVsBuildPropertyStorage vsBuildPropertyStorage = GetBuildPropertyStorage();
vsBuildPropertyStorage.SetItemAttribute(projectItemIdentifier, "DependentUpon", parentFileName);
I have also tried adding this in an attempt to get the project to reload, but it doesn't have any effect.
project.Save();
VSProject obj = project.Object as VSProject;
obj.Refresh();
AFAIK the only way of doing this is via automation of the Solution Explorer tool-window:
EnvDTE.DTE dte = ...;
string solutionName = Path.GetFileNameWithoutExtension(dte.Solution.FullName);
string projectName = project.Name;
dte.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Activate();
((DTE2)dte).ToolWindows.SolutionExplorer.GetItem(solutionName + #"\" + projectName).Select(vsUISelectionType.vsUISelectionTypeSelect);
dte.ExecuteCommand("Project.UnloadProject");
dte.ExecuteCommand("Project.ReloadProject");
Note that, if the project hasn't been saved, the user will get a dialog box prior to the "Project.UnloadProject" call.
Here is my code (with reactivating the old window):
public void RefreshSolutionExplorer(EnvDTE.Project activeProject, string captionOfActiveWindow)
{
DTE2 dte2 = activeProject.DTE as DTE2;
string solutionName = Path.GetFileNameWithoutExtension(dte2.Solution.FullName);
string projectName = activeProject.Name;
// Activate SolutionExplorer window
dte2.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate();
// Select your project to be updated
dte2.ToolWindows.SolutionExplorer.GetItem(solutionName + #"\" + projectName).Select(vsUISelectionType.vsUISelectionTypeSelect);
// Refresh SolutionExplorer window
dte2.ExecuteCommand("View.Refresh", String.Empty);
// Reactivate your old window
dte2.Windows.Item(captionOfActiveWindow).Activate();
}

How Can I Add an "ATL Simple Object" to Old ATL DLL Project Upgraded to VS 2010?

We have a DLL project which has existed for a long time (maybe as far back as Visual Studio 6) which has been updated for each new version of VS. The project contains a number COM classes implemented using ATL.
After upgrade to VS 2010, the project still builds fine. However, if I try to right-click the project and choose Add -> Class... -> ATL Simple Object, I get an error box that says this:
ATL classes can only be added to MFC EXE and MFC Regular DLL projects or projects with full ATL support.
This worked in VS 2008.
When I look at the project properties, Use of MFC was set to Use Standard Windows Libraries and Use of ATL was set to Not Using ATL. I changed these to Use MFC in a Shared DLL and Dynamic Link to ATL respectively, but still get the same error.
I know how to add new ATL objects without using the wizard, and I could try to recreate the project from scratch using VS 2010 to make it happy. But does anyone know of any easy way to get VS to allow me to use the ATL Simple Object wizard with a project that it doesn't recognize as a project "with full ATL support"?
Check this thread out.
It seems that adding this fragment info your ATL C++ code make it work. You don't need to actually build the project, just remove this stuff away after you are done with the wizard (provided that solution works for you).
// Added fake code begins here
class CAppModule :
public CComModule
{
};
// Added fake code ends here, below is regular ATL project stuff
CAppModule _Module;
This is where it all comes from, in $(VisualStudio)\VC\VCWizards\1033\common.js:
/******************************************************************************
Description: Returns a boolean indicating whether project is ATL-based.
oProj: Project object
******************************************************************************/
function IsATLProject(oProj)
{
try
{
var oCM = oProj.CodeModel;
oCM.Synchronize();
// look for global variable derived from CAtlModuleT
var oVariables = oCM.Variables;
for (var nCntr = 1; nCntr <= oVariables.Count; nCntr++)
{
var oVariable = oVariables(nCntr);
var strTypeString = oVariable.TypeString;
if (strTypeString == "ATL::CComModule" || strTypeString == "ATL::CAutoThreadModule")
{
return true;
}
Same problem here, but the project source already had CComModule _Module;
Fixed it, based on the IsATLProject script shown above, by changing it to
**ATL::**CComModule _Module;

Programatic navigation in Visual SourceSafe

Maybe this is a very simple problem, but I just can't figure it out. Is there any way to navigate to a certain folder in MS Visual SourceSafe from an external application?
Maybe some sort of command line parameter? (of course that would only work if VSS is closed). Or is there a solution that would also work if VSS is already opened? (COM?)
Thanks!
Here is VBS sample code to start programmatically control VSS:
const SS_INI_PATH = "с:\db\vss\srcsafe.ini"
const SS_LOGIN = "login"
const SS_PASSWORD = "password"
set obj = CreateObject("SourceSafe")
obj.Open SS_INI_PATH, SS_LOGIN, SS_PASSWORD
set objPrj = obj.VSSItem("$/project1")
' call below any objPrj methods
Help on object interfaces you can find here:
http://msdn.microsoft.com/en-US/library/microsoft.visualstudio.sourcesafe.interop(v=vs.80).aspx

Resources